void *workThread(void *pObject) { int rv = 0; int cnt; bool bActivity = true; short nPollCnt = 0; char szResponse[ 32 ]; printf("can232obj in workThread\n"); CCAN232Obj * pcan232obj = (CCAN232Obj *) pObject; if (NULL == pcan232obj) { pthread_exit(&rv); } while (pcan232obj->m_can232obj.m_bRun) { /////////////////////////////////////////////////////////////////////// // Receive /////////////////////////////////////////////////////////////////////// LOCK_MUTEX(pcan232obj->m_can232ObjMutex); // Noting to do if we should end... if (!pcan232obj->m_can232obj.m_bRun) continue; int cnt; unsigned char c; //printf("workThread - Receive\n"); c = pcan232obj->m_can232obj.m_comm.readChar(&cnt); while (-1 != cnt) { bActivity = true; if (CAN232_STATE_NONE == pcan232obj->m_can232obj.m_state) { if (('t' == c) || ('T' == c) || ('r' == c) || ('R' == c)) { pcan232obj->m_can232obj.m_state = CAN232_STATE_MSG; pcan232obj->m_can232obj.m_receiveBuf[ 0 ] = c; pcan232obj->m_can232obj.m_cntRcv = 1; } } else if (CAN232_STATE_MSG == pcan232obj->m_can232obj.m_state) { pcan232obj->m_can232obj.m_receiveBuf[ pcan232obj->m_can232obj.m_cntRcv++ ] = c; if (0x0d == c) { // One full message received If there is place in the queue // add message to it if (pcan232obj->m_can232obj.m_rcvList.nCount < CAN232_MAX_RCVMSG) { PCANALMSG pMsg = new canalMsg; pMsg->flags = 0; if (NULL != pMsg) { dllnode *pNode = new dllnode; if (NULL != pNode) { printf("workThread R - m_receiveBuf = [%s]\n", pcan232obj->m_can232obj.m_receiveBuf); int cnt = pcan232obj->m_can232obj.m_cntRcv; printf("workThread R - m_receiveBuf ["); for (int i = 0; i < cnt; i++) { printf("%02X ", pcan232obj->m_can232obj.m_receiveBuf[i]); } printf("]\n"); if (!can232ToCanal(pcan232obj->m_can232obj.m_receiveBuf, pMsg)) { pNode->pObject = pMsg; dll_addNode(&pcan232obj->m_can232obj.m_rcvList, pNode); // Update statistics pcan232obj->m_can232obj.m_stat.cntReceiveData += pMsg->sizeData; pcan232obj->m_can232obj.m_stat.cntReceiveFrames += 1; printf("workThread R - RcvFrames = [%ld]\n\n", pcan232obj->m_can232obj.m_stat.cntReceiveFrames); } else { // Failed to translate message printf("workThread R - Receive Failed to translate message\n"); delete pMsg; delete pNode; } } else { delete pMsg; } } } pcan232obj->m_can232obj.m_state = CAN232_STATE_NONE; } if (pcan232obj->m_can232obj.m_cntRcv > sizeof( pcan232obj->m_can232obj.m_receiveBuf)) { // Problems start all over again pcan232obj->m_can232obj.m_state = CAN232_STATE_NONE; } } c = pcan232obj->m_can232obj.m_comm.readChar(&cnt); } // while ( 0 != cnt ) UNLOCK_MUTEX(pcan232obj->m_can232ObjMutex); /////////////////////////////////////////////////////////////////////// // Transmit /////////////////////////////////////////////////////////////////////// //printf("workThread - Transmit\n"); LOCK_MUTEX(pcan232obj->m_can232ObjMutex); // Is there something to transmit // while ( ( NULL != pcan232obj->m_can232obj.m_sndList.pHead ) && // ( NULL != pcan232obj->m_can232obj.m_sndList.pHead->pObject ) ) { if ((NULL != pcan232obj->m_can232obj.m_sndList.pHead) && (NULL != pcan232obj->m_can232obj.m_sndList.pHead->pObject)) { char buf[ 80 ]; canalMsg msg; bActivity = true; memcpy(&msg, pcan232obj->m_can232obj.m_sndList.pHead->pObject, sizeof( canalMsg)); dll_removeNode(&pcan232obj->m_can232obj.m_sndList, pcan232obj->m_can232obj.m_sndList.pHead); // Must be a valid standard id if (!(msg.flags & CANAL_IDFLAG_EXTENDED) && (msg.id > 0x7ff)) { msg.id &= 0x7ff; }; // Must be a valid extended id if ((msg.flags & CANAL_IDFLAG_EXTENDED) && (msg.id > 0x1fffffff)) { msg.id &= 0x1fffffff; } // Currently there is a bug in ice old asm can232 tranceiver, and allow only small hex digits if (msg.flags & CANAL_IDFLAG_EXTENDED) { sprintf(buf, "T%8.8lx%i", msg.id, msg.sizeData); } else { sprintf(buf, "t%3.3lx%i", msg.id, msg.sizeData); } if (msg.sizeData) { char hex[5]; for (int i = 0; i < msg.sizeData; i++) { sprintf(hex, "%2.2X", msg.data[i]); //sprintf( hex, "%02.2x", msg.data[i] ); strcat(buf, hex); } strcat(buf, "\r"); } // Send the data pcan232obj->m_can232obj.m_comm.comm_puts(buf, strlen(buf), true); printf("workThread T - Write ["); for (int i = 0; i < strlen(buf); i++) { if (buf[i] == 0x0D) { printf("[CR]"); } else { printf("%c", buf[i]); } } printf("]\n"); pcan232obj->m_can232obj.m_comm.comm_gets(szResponse, sizeof( szResponse), 10000); printf("workThread T - Read ["); for (int i = 0; i < strlen(szResponse); i++) { if (szResponse[i] == 0x0D) { printf("[CR]"); } else { printf("%c", szResponse[i]); } } printf("]\n\n"); // needed !! At least at 19200 baud SLEEP(100); // Update statistics pcan232obj->m_can232obj.m_stat.cntTransmitData += msg.sizeData; pcan232obj->m_can232obj.m_stat.cntTransmitFrames += 1; //} // while there is something to transmit } // if there is something to transmit // If not in autopoll mode we do a poll for all frames first nPollCnt++; if (!pcan232obj->m_can232obj.m_bAuto && (nPollCnt > 5)) { char szCmd[5]; sprintf(szCmd, "A\r"); pcan232obj->m_can232obj.m_comm.comm_puts(szCmd, strlen(szCmd), true); nPollCnt = 0; } UNLOCK_MUTEX(pcan232obj->m_can232ObjMutex); if (!bActivity) SLEEP(100); bActivity = false; } // while( pcan232obj->m_can232obj.m_bRun ) pthread_exit(&rv); }
void workThread( void *pThreadObject ) { bool bRun = true; bool bActivity = true; short nPollCnt = 0; DWORD errorCode = 0; HANDLE can232ObjMutex; // Must hav a valid work object if ( NULL == pThreadObject ) ExitThread( errorCode ); // Get the object pointer in place struct _can232obj *pcan232obj = (struct _can232obj *)pThreadObject; can232ObjMutex = OpenMutex( MUTEX_ALL_ACCESS, false, CAN232_OBJ_MUTEX ); while ( pcan232obj->m_bRun ) { /////////////////////////////////////////////////////////////////////// // Receive /////////////////////////////////////////////////////////////////////// WaitForSingleObject( can232ObjMutex, INFINITE ); int cnt; unsigned char c; c = pcan232obj->m_comm.readChar( &cnt ); while ( 0 != cnt ) { bActivity = true; if ( CAN232_STATE_NONE == pcan232obj->m_state ) { if ( ('t' == c ) || ( 'T' == c ) || ('r' == c ) || ( 'R' == c ) ) { pcan232obj->m_state = CAN232_STATE_MSG; pcan232obj->m_receiveBuf[ 0 ] = c; pcan232obj->m_cntRcv = 1; } } else if ( CAN232_STATE_MSG == pcan232obj->m_state ) { pcan232obj->m_receiveBuf[ pcan232obj->m_cntRcv++ ] = c; if ( 0x0d == c ) { // One full message received // If there is place in the queue // add message to it if ( pcan232obj->m_rcvList.nCount < CAN232_MAX_RCVMSG ) { PCANALMSG pMsg = new canalMsg; pMsg->flags = 0; if ( NULL != pMsg ) { dllnode *pNode = new dllnode; if ( NULL != pNode ) { if ( can232ToCanal( pcan232obj->m_receiveBuf, pMsg ) ) { pNode->pObject = pMsg; dll_addNode( &pcan232obj->m_rcvList, pNode ); // Update statistics pcan232obj->m_stat.cntReceiveData += pMsg->sizeData; pcan232obj->m_stat.cntReceiveFrames += 1; } else { // Failed to translate message delete pMsg; delete pNode; } } else { delete pMsg; } } } pcan232obj->m_state = CAN232_STATE_NONE; } if ( pcan232obj->m_cntRcv > sizeof( pcan232obj->m_receiveBuf ) ) { // Problems start all over again pcan232obj->m_state = CAN232_STATE_NONE; } } c = pcan232obj->m_comm.readChar( &cnt ); } // while ReleaseMutex( can232ObjMutex ); /////////////////////////////////////////////////////////////////////// // Transmit /////////////////////////////////////////////////////////////////////// WaitForSingleObject( can232ObjMutex, INFINITE ); // Is there anything to transmit if ( ( NULL != pcan232obj->m_sndList.pHead ) && ( NULL != pcan232obj->m_sndList.pHead->pObject ) ) { char buf[ 80 ]; canalMsg msg; bActivity = true; memcpy( &msg, pcan232obj->m_sndList.pHead->pObject, sizeof( canalMsg ) ); dll_removeNode( &pcan232obj->m_sndList, pcan232obj->m_sndList.pHead ); // Must ve a valid standard id if ( !( msg.flags & CANAL_IDFLAG_EXTENDED ) && ( msg.id > 0x7ff ) ) { msg.id &= 0x7ff; }; // Must ve a valid extended id if ( ( msg.flags & CANAL_IDFLAG_EXTENDED ) && ( msg.id > 0x1fffffff ) ) { msg.id &= 0x1fffffff; } if ( msg.flags & CANAL_IDFLAG_EXTENDED ) { sprintf( buf, "T%08.8lX%i", msg.id, msg.sizeData ); } else { sprintf( buf, "t%03.3lX%i", msg.id, msg.sizeData ); } if ( msg.sizeData ) { char hex[5]; for ( int i= 0; i< msg.sizeData; i++ ) { sprintf( hex, "%02.2X", msg.data[i] ); strcat( buf, hex ); } } // Send the data char szResponse[ 32 ]; pcan232obj->m_comm.write( buf, true, true ); pcan232obj->m_comm.readBuf( szResponse, sizeof( szResponse ), -1 ); // Update statistics pcan232obj->m_stat.cntTransmittData += msg.sizeData; pcan232obj->m_stat.cntTransmittFrames += 1; // If not in autopoll mode if ( 0x0d == *szResponse ) { } // If in autopoll mode extended message else if ( ( msg.flags & CANAL_IDFLAG_EXTENDED ) && ( 'Z' == *szResponse ) ) { } // If in autopoll mode standard message else if ( !( msg.flags & CANAL_IDFLAG_EXTENDED ) && ( 'z' == *szResponse ) ) { } } // If not in autopoll mode we do a poll for all frames first nPollCnt++; if ( !pcan232obj->m_bAuto && ( nPollCnt > 5 ) ) { pcan232obj->m_comm.write( "A", true, true ); nPollCnt = 0; } ReleaseMutex( can232ObjMutex ); if ( !bActivity ) Sleep ( 100 ); bActivity = false; } // Release the mutex for other threads to use ReleaseMutex( can232ObjMutex ); ExitThread( errorCode ); }