当前位置: 技术问答>linux和unix
串口读数据问题,读到了7位数据,可显示所读取数据时候为空,不知原因?
来源: 互联网 发布时间:2015-06-22
本文导语: 代码如下: int opencom(char com[],int band) { int fd; struct termios cum0; fd=open(com,O_RDWR|O_NOCTTY); if(fd>=0) { bzero(&cum0,sizeof(struct termios)); cum0.c_cflag=B9600|~CRTSCTS|CS8|CLOCAL|CREAD; cum0.c_...
代码如下:
int opencom(char com[],int band)
{
int fd;
struct termios cum0;
fd=open(com,O_RDWR|O_NOCTTY);
if(fd>=0)
{
bzero(&cum0,sizeof(struct termios));
cum0.c_cflag=B9600|~CRTSCTS|CS8|CLOCAL|CREAD;
cum0.c_cflag|=(PARENB);
cum0.c_oflag=0;
cum0.c_lflag=0;
cum0.c_cc[VMIN]=0;
cum0.c_iflag&=~(IXON|IXOFF);
cfsetispeed(&cum0,band);
cfsetospeed(&cum0,band);
tcsetattr(fd,TCSANOW,&cum0);
}
return fd;
}
int readcom(int fd,char buf[])
{
int bytes;
ioctl(fd,FIOREAD,&bytes);
if(fd>=0)
{
if(bytes>0)
{
bytes=read(fd,buf,bytes);
return bytes;//读取成功
}
else
return 0;//读取失败
}
else
return -1;
}
int writecom(int fd,char buf[],int len)
{
int bytes;
if(fd>=0)
{
bytes=write(fd,buf,len);
return bytes;
}
else
return -1;
}
main()
{
gchar buf_send[6];
gchar buf_recieve[9];
gchar cc[3];
int comm;
int bytes=0;
comm=opencom("/dev/ttyS0"),B9600);
strcpy(buf_send,"$02M/r");
bytes=writecom(comm,buf_send,strlen(buf_send));
usleep(20000);
bytes=readcom(comm,buf_recieve); //此时bytes数值为7
if(bytes>1){
e_show_message("recieve",buf_recieve,E_NO_FUNC); //弹出空白窗体,不知为何,根据bytes,buf_recieve应该有7位数呀,请教,谢谢
}
}
int opencom(char com[],int band)
{
int fd;
struct termios cum0;
fd=open(com,O_RDWR|O_NOCTTY);
if(fd>=0)
{
bzero(&cum0,sizeof(struct termios));
cum0.c_cflag=B9600|~CRTSCTS|CS8|CLOCAL|CREAD;
cum0.c_cflag|=(PARENB);
cum0.c_oflag=0;
cum0.c_lflag=0;
cum0.c_cc[VMIN]=0;
cum0.c_iflag&=~(IXON|IXOFF);
cfsetispeed(&cum0,band);
cfsetospeed(&cum0,band);
tcsetattr(fd,TCSANOW,&cum0);
}
return fd;
}
int readcom(int fd,char buf[])
{
int bytes;
ioctl(fd,FIOREAD,&bytes);
if(fd>=0)
{
if(bytes>0)
{
bytes=read(fd,buf,bytes);
return bytes;//读取成功
}
else
return 0;//读取失败
}
else
return -1;
}
int writecom(int fd,char buf[],int len)
{
int bytes;
if(fd>=0)
{
bytes=write(fd,buf,len);
return bytes;
}
else
return -1;
}
main()
{
gchar buf_send[6];
gchar buf_recieve[9];
gchar cc[3];
int comm;
int bytes=0;
comm=opencom("/dev/ttyS0"),B9600);
strcpy(buf_send,"$02M/r");
bytes=writecom(comm,buf_send,strlen(buf_send));
usleep(20000);
bytes=readcom(comm,buf_recieve); //此时bytes数值为7
if(bytes>1){
e_show_message("recieve",buf_recieve,E_NO_FUNC); //弹出空白窗体,不知为何,根据bytes,buf_recieve应该有7位数呀,请教,谢谢
}
}
|
用 printf("%02x", buf_recieve[0]) 看看到底是什么,可能是不可见字符