当前位置: 技术问答>linux和unix
串口通信 有时得到正确的值,有时又接收不到,为什么?谢了!!!
来源: 互联网 发布时间:2016-06-24
本文导语: 求助各位高手!! 我现在在写一个串口程序,程序如下: #include #include #include #include #include #include #include #include #include /*uart_open_function*/ int open_port(int fd,...
求助各位高手!!
我现在在写一个串口程序,程序如下:
#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)