/* * This method is used by several methods to try and start the * Listener thread. There are a few steps to it, so it's nice to * have it in one place as opposed to copying code. */ void CKIRCProtocol::startListener() { // we have to have a listener to operate on... if (mListener != NULL) { // ...and to start it it should not be running if (!mListener->isRunning()) { // OK... start it... mListener->start(); // now check to see that it actually got started int cnt = 0; while ((cnt < 6) && !mListener->isRunning()) { // wait for 0.25 sec to see if it's alive mmsleep(250); cnt++; } // make sure it's running if (!mListener->isRunning()) { std::ostringstream msg; msg << "CKIRCProtocol::startListener() - the Listener for this " "instance could not be started. This is a serious threading " "problem."; throw CKException(__FILE__, __LINE__, msg.str()); } } } }
/* * This method is used by several methods to try and stop the * Listener thread. There are a few steps to it, so it's nice to * have it in one place as opposed to copying code. */ void CKIRCProtocol::stopListener() { // we have to have a listener to stop... if (mListener != NULL) { // and it only makes sense if it's currently running if (mListener->isRunning()) { // OK, let it know it's time to stop... mListener->setTimeToDie(true); // ...and check to see that it does int cnt = 0; while ((cnt < 6) && mListener->isRunning()) { // wait for DEFAULT_IRC_READ_TIMEOUT/4 to see if it's dead mmsleep((unsigned int)DEFAULT_IRC_READ_TIMEOUT * 1000 / 4); cnt++; } // make sure it's stopped if (mListener->isRunning()) { std::ostringstream msg; msg << "CKIRCProtocol::stopListener() - the Listener for this " "instance could not be stopped. This is a serious threading " "problem."; throw CKException(__FILE__, __LINE__, msg.str()); } } } }
int GatherMsgs(PMsg msg) { mmsleep(10); if (EnabledMsg & MSG_TYPE_TIMER) { TTime t; BYTE TimerType; GetTime(&t); if(t.tm_sec!=gCurTime.tm_sec) { TimerType = (gCurTime.tm_min == t.tm_min ? Timer_Second : Timer_Minute); if ((TimerType == Timer_Minute) && (gCurTime.tm_hour != t.tm_hour)) { TimerType = Timer_Hour; } if (TimerType == Timer_Hour) { DBPRINTF("t=%d:%d:%d, gCurTime=%d:%d:%d\n", t.tm_hour, t.tm_min, t.tm_sec, gCurTime.tm_hour, gCurTime.tm_min, gCurTime.tm_sec); } //printf("get_sec = %d,gcur_sec = %d\n",t.tm_sec,gCurTime.tm_sec); ConstructMSG(msg, MSG_TYPE_TIMER, 0, 0); memcpy(&gCurTime, &t, sizeof(TTime)); //GetTime(&gCurTime); Timer_count(); return MSG_TYPE_TIMER; } } //232串口 if ((gOptions.RS232On || gOptions.RS485On) && (!gOptions.IsSupportModem || !gOptions.IsConnectModem) && (gOptions.CT232On != 2)) { RS232Check(&st232); } //484串口 if((gOptions.RS232On||gOptions.RS485On) && gOptions.IsSupportModem) { RS232Check(&ttl232); } EthBoradcastCheck(); EthCommTCPCheck(); if(1 == shared_Comm_stuff->CreateMainCmd) { ConstructMSG(msg, shared_Comm_stuff->Cmd, shared_Comm_stuff->NewsType, (int)shared_Comm_stuff); shared_Comm_stuff->CreateMainCmd = 0; return shared_Comm_stuff->Cmd; } return 0; }
int RS232WaitRespose(serial_driver_t *rs,int DeviceID, char *OutBuf,int TimeoutMS) { char Sender[SENDER_LEN] = {0}; int waitingChars = 0; char buf[MaxLen]; memset(buf,0x00,MaxLen); BYTE *p=buf; int size = 0; U32 timeval = 0; U32 timeout = 0; PProtoHeader pchd = NULL; timeval = GetTickCount(); while(timeout<TimeoutMS) { size=rs->poll(); if(size>=1) { *p = rs->read(); if(*p == PROTO_START_TAG)//校验到头字节合适,开始接受下面的数据 { break; } else { //BYTE ch = *p; //printf("Noise Data:[%x]\n",ch); } }else { mmsleep(10); } timeout = GetTickCount() - timeval; } if(size<=0) { printf("--- time out wait for data\n"); return -1; } else { rs->read_buf((char*)p+1, size-1); //已读一个字节 pchd = (PProtoHeader)p; if(pchd->StartTag == PROTO_START_TAG && pchd->DestAddr == DeviceID) { int curLen = size; int len = sizeof(TProtoHeader) + pchd->DataSize + 3; //2个校验字节+1个包尾 = 3 printf("get len = %d\n",len); //if((curLen)<len) { timeval = GetTickCount(); timeout = 0; mmsleep(10); while(timeout<TimeoutMS) { size=rs->poll(); if(size>0) { if(size>=(MaxLen-curLen)) { size=(MaxLen-curLen); } rs->read_buf(p +curLen, size); curLen+=size; timeval = GetTickCount(); //mmsleep(10); //printf("---get-size %d\n",size); if(curLen>=MaxLen) { memcpy(OutBuf,p,MaxLen); return MaxLen; } }else { mmsleep(10); } timeout = GetTickCount() - timeval; } memcpy(OutBuf,p,curLen); //printf("WaitTerminalRead==WAIT_TIMEOUT,recived len=%d,total len=%d\n",curLen,len); return curLen; } //else { printf("---- %2x %d %d\n",pchd->StartTag, pchd->DestAddr, DeviceID ); rs->flush_input(); return -2; } } else { //printf("zhc no2 ------- %2x %d %d\n",pchd->StartTag, pchd->DestAddr, DeviceID ); printf("--BadData(%d)-\n",size); rs->flush_input(); return -3; } } }
/* * The sleep() and usleep() methods on Solaris are iffy and can be a real * problem. So we have created these routines to take their place. The * first sleeps for a given number of seconds and the second for a given * number of milliseconds. */ void msleep( long secs ) { mmsleep( secs * 1000 ); }