当前位置: 技术问答>linux和unix
linux串口读写问题
来源: 互联网 发布时间:2016-08-06
本文导语: 我的代码如下,请各位帮我看看为什么接受数据的时候分段接收,我给它发的是“0003468055”他的接受结果是这样的 open ttyS0... nread=8 buff=0003468 nread=6 buff=005 我想应该是我程序的问题请大家帮忙看看,下面我把程序给...
我的代码如下,请各位帮我看看为什么接受数据的时候分段接收,我给它发的是“0003468055”他的接受结果是这样的
open ttyS0...
nread=8
buff=0003468
nread=6
buff=005
我想应该是我程序的问题请大家帮忙看看,下面我把程序给大家发一下:
int speed_arr[]={B38400,B19200,B9600,B4800,B2400,B1200,B300,B38400,B19200,B9600,B4800,B2400,B1200,B300,B115200};
int name_arr[]={38400,19200,9600,4800,2400,1200,300,38400,19200,9600,4800,2400,1200,300,115200};
int open_port(int fd,int comport)
{
if(comport==0)
{
fd=open("/dev/ttyS0",O_RDONLY|O_NOCTTY);
if(-1==fd)
{
perror("Can't open serial port0 ");
return (-1);
}
else
printf("open ttyS0........n");
}
else if(comport==1)
{
fd=open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can`t open serial port1 ");
return (-1);
}
else
printf("open ttyS1......n");
}
else if(comport==2)
{
fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can`t open serial port2");
return -1;
}
else
printf("open ttyS2.......n");
}
else if(comport==3)
{
fd=open("/dev/tts/3",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can't open serial port3");
return -1;
}
else
printf("open ttyS3........n");
}
else if(comport==4)
{
fd=open("/dev/tts/4",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can`t open serial port3");
return -1;
}
else
printf("open ttyS4.....n");
}
return fd;
}
int port_init(int fd,int speed,int databits,int stopbits,int parity)
{
int i;
int status;
struct termios opt;
tcgetattr(fd,&opt);
for(i=0;i
open ttyS0...
nread=8
buff=0003468
nread=6
buff=005
我想应该是我程序的问题请大家帮忙看看,下面我把程序给大家发一下:
int speed_arr[]={B38400,B19200,B9600,B4800,B2400,B1200,B300,B38400,B19200,B9600,B4800,B2400,B1200,B300,B115200};
int name_arr[]={38400,19200,9600,4800,2400,1200,300,38400,19200,9600,4800,2400,1200,300,115200};
int open_port(int fd,int comport)
{
if(comport==0)
{
fd=open("/dev/ttyS0",O_RDONLY|O_NOCTTY);
if(-1==fd)
{
perror("Can't open serial port0 ");
return (-1);
}
else
printf("open ttyS0........n");
}
else if(comport==1)
{
fd=open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can`t open serial port1 ");
return (-1);
}
else
printf("open ttyS1......n");
}
else if(comport==2)
{
fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can`t open serial port2");
return -1;
}
else
printf("open ttyS2.......n");
}
else if(comport==3)
{
fd=open("/dev/tts/3",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can't open serial port3");
return -1;
}
else
printf("open ttyS3........n");
}
else if(comport==4)
{
fd=open("/dev/tts/4",O_RDWR|O_NOCTTY|O_NDELAY);
if(-1==fd)
{
perror("can`t open serial port3");
return -1;
}
else
printf("open ttyS4.....n");
}
return fd;
}
int port_init(int fd,int speed,int databits,int stopbits,int parity)
{
int i;
int status;
struct termios opt;
tcgetattr(fd,&opt);
for(i=0;i