#include "simtest.h" #include "skt.h" #include "log.h" #include "manage.h" CSimTest::CSimTest(int index, const char* ate) { _start = 0; _btest = false; _bhome = false; _index = index; strcpy(_ate, ate); m_brun = false; _skt = INVALID_SKT; m_hThread = INVALID_HANDLE; _status = SOCKET_DISCONNECTED; memset(_res, 0, MAX_BUF_LEN); } CSimTest::~CSimTest(void) { Deinit(); } int CSimTest::Init() {//任务开始 RETURN_CHK_NOPRT(!m_brun, 0); m_brun = true; _skt = skt_client("127.0.0.1", 6050); m_hThread = sys_CreateThread(ThreadFun, this); return 0; } int CSimTest::Deinit() {//关闭任务 RETURN_CHK_NOPRT(m_brun, 0); m_brun = false; _status = SOCKET_DISCONNECTED; sys_WaitForSingleObject(m_hThread); sys_CloseHandle(m_hThread); skt_close(_skt); return 0; } int WINAPI CSimTest::ThreadFun(void* param) { CSimTest* p = (CSimTest*)param; return p->Process(); } int CSimTest::Process() { while(m_brun) {//循环 ChkConn(); ChkTest(); ChkHome(); RecvMsg(); } return 0; } //结束符 static const char* _endstr = "$"; static const int _endlen = (int)strlen(_endstr); int CSimTest::FindEnd(char* pszmsg) { char* ptmp = NULL; ptmp = strstr(pszmsg, _endstr); if (!ptmp) return -1; *ptmp = 0; return (int)(ptmp - pszmsg); } void CSimTest::SendMsg(const char *pszFormat, ...) { int len = 0; int ret = 0; va_list args; char buff[1024] = {0}; va_start(args, pszFormat); len = vsnprintf(buff, 1023, pszFormat, args); va_end(args); ZERO_CHK(len > 0); len += sprintf(buff + len, "%s", _endstr); //增加结束符 ret = skt_send(_skt, buff, len); if (ERR_NO_CONNECT == ret) {//已经断开 _status = SOCKET_DISCONNECTED; REPORT("模拟测试治具: %s 已断开连接", _ate); return; } if (ins->testLog()) LOG_INF1("模拟治具%s 发送: %s", _ate, buff); } void CSimTest::Disconnect() { _start = 0; _bhome = false; _btest = false; _status = SOCKET_DISCONNECTED; sprintf(_res, "0"); //失败 } #define PASS 3 void CSimTest::ChkTest() { SYS_TIME tm; uint res = 0; ZERO_CHK(_btest); if (_bsendgr) { SendMsg("GR"); _bsendgr = false; } //SN#Machine#User#Config#Lot#Mode#SiteID#ProjectID#RetryMode#Mes#Mtcp //"SN,Machine,User,Config,Lot,Mode,SiteID,ProjectID,RetryMode,Status,SOCKET ID,Time" if (sys_GetTickCount() > _start + ins->getSimCT() * 1000) {//18s测试 _btest = false; srand((unsigned)time(NULL)); res = ((uint)rand()) % 100; res = (res >= ins->getFail()) ? 1 : 0; //3%概率失败 sys_GetLocalTime(&tm); sprintf(_res, "1#%d#%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%04d/%02d/%02d %02d:%02d:%02d", res, SN.c_str(), Machine.c_str(), User.c_str(), Config.c_str(), Lot.c_str(), Mode.c_str(), SiteID.c_str(), ProjectID.c_str(), RetryMode.c_str(), res ? "PASS" : "FAIL", _ate, tm.wYear, tm.wMonth, tm.wDay, tm.wHour, tm.wMinute, tm.wSecond); } } void CSimTest::ChkHome() { ZERO_CHK(_bhome); if (sys_GetTickCount() > _start + HOMECT) {//60s _bhome = false; } } void CSimTest::ChkConn() { ZERO_CHK(SOCKET_DISCONNECTED == _status); if (SOCKET_DISCONNECTED == skt_status(_skt)) {//未连接 sys_Sleep(50); return; } _status = SOCKET_CONNECTED; REPORT("模拟测试治具%d:%s 已连接.", _index, _ate); SendMsg("Register#%s#%d#%s", _ate, _index, "SN,Machine,User,Config,Lot,Mode,SiteID," "ProjectID,RetryMode,Status,SOCKET ID,Time"); } void CSimTest::RecvMsg() {//接收消息 int total = 0; int nlen = 0; char* pdest = NULL; char buff[1024] = {0}; ZERO_CHK(SOCKET_CONNECTED == _status); total = skt_recv(_skt, buff, 1023 - 1, 100); if (ERR_NO_CONNECT == total) {//已经断开 Disconnect(); REPORT("模拟治具:%s 已断开连接", _ate); return; } ZERO_CHK(total > 0); //error pdest = buff; while(total > 0) { nlen = FindEnd(pdest); //查找结束符 if (nlen < 0) {//未找到结束符 return; } //新消息 ParseMsg(pdest); nlen += _endlen; //加上结束符长度 total -= nlen; pdest += nlen; } } int CSimTest::ParseMsg(char* pszmsg) { char* ptmp = NULL; RETURN_CHK_NOPRT(pszmsg, ERR_INPUT_PARAM); ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); if (!strcmp(ptmp, REG)) {//注册消息 return 0; } if (!strcmp(ptmp, STATUS)) {//治具状态回复 SendMsg("%s#%d", STATUS, isRun() ? 0 : 1); return 0; } if (!strcmp(ptmp, GRIP)) {//夹爪回复 SendMsg("%s#%d", GRIP, isRun() ? 0 : 1); return 0; } if (!strcmp(ptmp, PAUSE)) {//暂停回复 -- 工作中才能暂停 SendMsg("%s#%d", PAUSE, isRun() ? 1 : 0); return 0; } if (!strcmp(ptmp, START)) {//启动测试回复 if (isRun() || ParseStartParam(pszmsg)) {//is run/parse error SendMsg("%s#0", START); return 0; } else {//ok _btest = true; _bsendgr = true; sprintf(_res, "0"); //测试中 _start = sys_GetTickCount(); SendMsg("%s#1", START); } return 0; } if (!strcmp(ptmp, RESET)) {//重置 SendMsg("%s#1", RESET); return 0; } if (!strcmp(ptmp, HOME)) {//启动回原回复 if (!isRun()) {//不在测试中 _bhome = true; _start = sys_GetTickCount(); SendMsg("%s#1", HOME); } else SendMsg("%s#0", HOME); return 0; } if (!strcmp(ptmp, RES)) {//询问结果回复 SendMsg("%s#%s", RES, _res); return 0; } if (!strcmp(ptmp, ERR)) {//错误代码回复 SendMsg("%s#0", ERR); return 0; } if (!strcmp(ptmp, GR)) {//GR请求 char buff[256] = { 0 }; comm_strncpy(buff, pszmsg, 240); REPORT("recv: %s#%s$", ptmp, buff); return 0; } REPORT("模拟治具%s 收到异常指令: %s%s%s", _ate, ptmp, (pszmsg && *pszmsg) ? "#" : "", pszmsg ? pszmsg : ""); return ERR_INPUT_PARAM; } //SN#Machine#User#Config#Lot#Mode#SiteID#ProjectID#RetryMode int CSimTest::ParseStartParam(char* pszmsg) { char* ptmp = NULL; RETURN_CHK_NOPRT(pszmsg, ERR_INPUT_PARAM); ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); SN = ptmp; ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); Machine = ptmp; ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); User = ptmp; ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); Config = ptmp; ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); Lot = ptmp; ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); Mode = ptmp; ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); SiteID = ptmp; ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); ProjectID = ptmp; ptmp = sys_strtok(NULL, SPLIT, &pszmsg); RETURN_CHK_NOPRT(ptmp && *ptmp, ERR_INPUT_PARAM); RetryMode = ptmp; return 0; }