当前位置: 技术问答>linux和unix
[求助!!!]UART 串口通信,收到的数据有时是乱码
来源: 互联网 发布时间:2016-06-23
本文导语: 我从网上找到一个串口通信的程序,自己做了一些修改,编码如下: #include #include #include #include #include #include #include #include #include /*uart_open_function*/ int open...
我从网上找到一个串口通信的程序,自己做了一些修改,编码如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*uart_open_function*/
int open_port(int fd,int comport)
{
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;
if(comport==1)//uart1
{
fd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
printf("ttyS0 n");
if(-1==fd){
perror("Can't Open Serial Port1!");
return(-1);
}
}
else if(comport==2)//uart2
{
fd=open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);
printf("ttyS1 n");
if(-1==fd){
perror("Can't Open Serial Port2!");
return(-1);
}
}
else if(comport==3)//uart3
{
fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY);
printf("ttyS2 n");
if(-1==fd){
perror("Can't Open Serial Port3!");
return(-1);
}
}
/*return uart status is block*/
if(fcntl(fd,F_SETFL,0)
#include
#include
#include
#include
#include
#include
#include
#include
#include
/*uart_open_function*/
int open_port(int fd,int comport)
{
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;
if(comport==1)//uart1
{
fd=open("/dev/ttyS0",O_RDWR|O_NOCTTY|O_NDELAY);
printf("ttyS0 n");
if(-1==fd){
perror("Can't Open Serial Port1!");
return(-1);
}
}
else if(comport==2)//uart2
{
fd=open("/dev/ttyS1",O_RDWR|O_NOCTTY|O_NDELAY);
printf("ttyS1 n");
if(-1==fd){
perror("Can't Open Serial Port2!");
return(-1);
}
}
else if(comport==3)//uart3
{
fd=open("/dev/ttyS2",O_RDWR|O_NOCTTY);
printf("ttyS2 n");
if(-1==fd){
perror("Can't Open Serial Port3!");
return(-1);
}
}
/*return uart status is block*/
if(fcntl(fd,F_SETFL,0)