|
|
我是linux编程方面的新手,正在学习linux串口方面的,下面是我读串口的代码
void read_com(int com_fd)
{
struct HEAD *head;
char buffer[1024];
int totalread=0;
int readbyte,remainbyte=0;
while(1)
{
while((readbyte=read(com_fd,buffer+totalread,HEAD_SIZE))>0)
{
totalread+=readbyte;
if(totalread==HEAD_SIZE)
{
head=(struct HEAD *)buffer;
if((head->mag_num==MAGNUM)&&(head->len==totalread))
{
printf("读取到一个完整的包,");
printf("包的各个结构成员如下:\n");
printf("\tmag_num: %08x\n",head->mag_num);
printf("\tvendor_info: %08x\n",head->info);
printf("\ttype: %08x\n",head->type);
printf("\tlen: %08x\n",head->len);
printf("\tseq: %08x\n",head->seq);
printf("\tchesum: %08x\n",head->chesum);
printf("\tmag_end: %08x\n",head->end);
printf("\n\n");
if(head->type==SWREQ_START)
{
write_com(com_fd,3);
}
totalread=0;
}
else
{
printf("接收到的字符为:%x\n",buffer);
printf("fd is :%d\n",fd);
printf("魔术字出错,还是包的判断算法出错?\n");
totalread=0;
continue;
}
}
}
}
}
我的本意是:
自己定义了一个数据结构,然后通过冲串口接收从一个设备发过来的数据,然后打印出来,对于特定的消息则发送回应过去。现在的问题是,如果设备在本程序之前启动,那么一切正常,但是,如果先启动本程序,然后再启动设备(也就是本程序在设备之前运行),那么就读不到任何数据(该硬件设备在关闭的时候发送00,开启的时候也会发送几个干扰字符),我向请问各位,这到底是怎么回事呢?是串口控制问题还是设备读写判断有问题呢?
不知道我的意思说清楚了没? |
|