串口通信 ,程序打开可以接收到数据,一段时间后接收不到 如题,按照一个字节读取循环读取数据,某次数据到来时,比如发送端发送了100个字节,接收时在接收的第10个字节时程序停止在ReadFile函数。 打开串口: core_t* core_com_create( char *com, char *baud, char* crc, char *data, char* stop ) { core_t *core = NULL; com_dcb_t dcb = {0}; com_timeouts_t timeouts = {0}; E_MALLOC( ALIGN(sizeof(core_t)),core,core_t ); if( core == NULL ) return NULL; BZERO( core, core_t ); // open com, check success or not if( Comm_Open( &core->comm.hcom, com ) == FALSE ){ DMSG((STDOUT,”Core Com: open system com: %s, failed ”, com)); E_FREE( core ); return NULL; } Comm_GetData( &core->comm.hcom, &dcb ); dcb.BaudRate = e_atol( baud ); if( dcb.BaudRate == 0 ){ dcb.BaudRate =; } if( Comm_SetData( &core->comm.hcom, &dcb) ){ timeouts.ReadIntervalTimeout = 3000; timeouts.ReadTotalTimeoutMultiplier = 3000; timeouts.ReadTotalTimeoutConstant = 3000; timeouts.WriteTotalTimeoutMultiplier = 3000; timeouts.WriteTotalTimeoutConstant = 3000; Comm_SetTimeOuts( &core->comm.hcom, &timeouts ); } sprintf( core->comm.com, “%s”, com ); sprintf( core->comm.baud, “%s”, baud ); sprintf( core->comm.crc, “%s”, crc ); sprintf( core->comm.data, “%s”, data ); sprintf( core->comm.stop, “%s”, stop ); core->comm.dcb = dcb; core->comm.state = TRUE; core->comm.bexit = FALSE; H_createmutex( &core->comm.lock ); DMSG((STDOUT,”success to init core com, %s:%d ”,com, dcb.BaudRate)); return core; } 读取串口数据: core_com_run( core_t *core ) { command_t command = {0}; int count, len = CORE_BUFFER_LEN, n = 0; e_uint8 buffer[CORE_BUFFER_LEN]; char logBuff[MAX_PATH] = {0}; system_time_t stime = {0}; H_GetLocalTime(&stime); if( core_com_state(core) == FALSE || core->comm.bexit == TRUE ) return FALSE; DMSG((STDOUT, “core com start running…… ”)); sprintf( logBuff, “开始:core com start running……%d-%d-%d %d:%d:%d ”, stime.year, stime.month, stime.day, stime.hour, stime.minute, stime.second ); n = strlen( logBuff ); WriteComLog( logBuff, n, “D:\ComLog.txt”); while( TRUE ){ H_GetLocalTime(&stime); sprintf( logBuff, “开始接收:start recv……%d-%d-%d %d:%d:%d ”, stime.year, stime.month, stime.day, stime.hour, stime.minute, stime.second ); n = strlen( logBuff ); WriteComLog( logBuff, n, “D:\ComLog.txt”); count = Comm_Read( &core->comm.hcom, (e_uint8*)buffer, len ); H_GetLocalTime(&stime); sprintf( logBuff, “接收完成:recv end…%d…%d-%d-%d %d:%d:%d ”, count, stime.year, stime.month, stime.day, stime.hour, stime.minute, stime.second ); n = strlen( logBuff ); WriteComLog( logBuff, n, “D:\ComLog.txt”); if( core->comm.bexit == TRUE ){ H_GetLocalTime(&stime); sprintf( logBuff, “准备退出:core com ready exit……%d-%d-%d %d:%d:%d ”, stime.year, stime.month, stime.day, stime.hour, stime.minute, stime.second ); n = strlen( logBuff ); WriteComLog( logBuff, n, “D:\ComLog.txt”); break; } if( count<=0 ){ // H_Delay(100); H_Delay(2); H_GetLocalTime(&stime); sprintf( logBuff, “runing……%d-%d-%d %d:%d:%d ”, stime.year, stime.month, stime.day, stime.hour, stime.minute, stime.second ); n = strlen( logBuff ); WriteComLog( logBuff, n, “D:\ComLog.txt”); continue; } } e_uint32 Comm_Read( com_t *com, e_uint8 *buffer, e_uint32 blen ) { e_uint32 sum, dwBytesTransferred; e_uint8 bbyte; char logBuff[MAX_PATH] = {0}; int n = 0; system_time_t stime = {0}; // 1 check comm state if( !Comm_State(com) ) return FALSE; // 2 read message data from comm sum = 0; do{ // read one data H_GetLocalTime( &stime ); sprintf( logBuff, “开始接收11:start recv……%d-%d-%d %d:%d:%d”, stime.year, stime.month, stime.day, stime.hour, stime.minute, stime.second ); n = strlen( logBuff ); WriteComLog( logBuff, n, “D:\ComLog.txt”); ReadFile( com->priv, &bbyte, 1, &dwBytesTransferred, 0x0); H_GetLocalTime( &stime ); sprintf( logBuff, “开始接收22:start recv……%d-%d-%d %d:%d:%d”, stime.year, stime.month, stime.day, stime.hour, stime.minute, stime.second ); n = strlen( logBuff ); WriteComLog( logBuff, n, “D:\ComLog.txt”); // check success or not if(dwBytesTransferred == 1){ // more than buffer, finished, try again if( sum>blen ){ buffer[sum] = ‘0’; return sum; } // write data to buffer buffer[sum] = bbyte; sum++; }else{ // no data if( sum>0 ){ buffer[sum] = ‘0’; return sum; } return FALSE; } }while(TRUE); return sum; }
2024最新激活全家桶教程,稳定运行到2099年,请移步至置顶文章:https://sigusoft.com/99576.html
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。 文章由激活谷谷主-小谷整理,转载请注明出处:https://sigusoft.com/88474.html