串口通信 ,程序打开可以接收到数据,一段时间后接收不到

ypj44 2014-07-30 07:40:41
如题,按照一个字节读取循环读取数据,某次数据到来时,比如发送端发送了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\r\n", com));
E_FREE( core );
return NULL;
}

Comm_GetData( &core->comm.hcom, &dcb );
dcb.BaudRate = e_atol( baud );
if( dcb.BaudRate == 0 ){
dcb.BaudRate =115200;
}

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\r\n",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......\r\n"));
sprintf( logBuff, "开始:core com start running......%d-%d-%d %d:%d:%d\r\n",\
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\r\n",\
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\r\n", 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\r\n",\
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\r\n",\
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;
}
...全文
459 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复

69,364

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧