Chinaunix
标题:
linux串口编程,write函数不能发送数据
[打印本页]
作者:
rudyfly
时间:
2012-02-05 21:45
标题:
linux串口编程,write函数不能发送数据
本人最近在写linux下的串口编程,是一个有串口的台式机和STM32的单片机板子的串口通信,板子上的串口通信是可行的,因为在window下的串口调试工具测试了,收发数据都是没有问题的,可是在linux下,read函数可以收到板子发来的数据,可是却不能向板子发送数据,板子中的程序是这样的:当收到数据是,做适当的延时后,把收到的数据再发给台式机。
read函数可以收到数据在终端显示,但是write函数将缓冲区的数据发送,返回值是要发的数据总的字节数,但是板子上却没有收到,部分代码如下:
void Uart_Set_Speed(int fd, int speed)
{
int i;
int status;
struct termios Opt;
tcgetattr(fd, &Opt);
for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++)
{
if (speed == name_arr[i])
{
tcflush(fd, TCIOFLUSH);
cfsetispeed(&Opt, speed_arr[i]);
cfsetospeed(&Opt, speed_arr[i]);
status = tcsetattr(fd, TCSANOW, &Opt);
if (status != 0)
perror("tcsetattr fd1");
return;
}
tcflush(fd,TCIOFLUSH);
}
}
复制代码
int Uart_Set_Parity(int fd,int databits,int stopbits,char parity)
{
struct termios options;
if ( tcgetattr( fd,&options) != 0)
{
perror("SetupSerial 1");
return(false);
}
options.c_cflag &= ~CSIZE;
switch (databits)
{
case 7:
options.c_cflag |= CS7;
break;
case 8:
options.c_cflag |= CS8;
break;
default:
fprintf(stderr,"Unsupported data size\n");
return (false);
}
switch (parity)
{
/*ÎTD£Ñé*/
case 'n':
case 'N':
options.c_cflag &= ~PARENB; /* Clear parity enable */
options.c_iflag &= ~INPCK; /* Enable parity checking */
break;
/*ÆæD£Ñé*/
case 'o':
case 'O':
options.c_cflag |= (PARODD | PARENB);
options.c_iflag |= INPCK; /* Disable parity checking */
break;
/*żD£Ñé*/
case 'e':
case 'E':
options.c_cflag |= PARENB; /* Enable parity */
options.c_cflag &= ~PARODD;
options.c_iflag |= INPCK; /* Disable parity checking */
break;
case 'S':
case 's': /*as no parity*/
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
break;
default:
fprintf(stderr,"Unsupported parity\n");
return (false);
}
switch (stopbits)
{
case 1:
options.c_cflag &= ~CSTOPB;
break;
case 2:
options.c_cflag |= CSTOPB;
break;
default:
fprintf(stderr,"Unsupported stop bits\n");
return (false);
}
options.c_cflag &= ~CRTSCTS;
options.c_lflag &= ~(ECHO|ICANON|IEXTEN|ISIG);
options.c_iflag &= ~(BRKINT|ICRNL|IXON|ISTRIP);
options.c_oflag &= ~(OPOST);
options.c_cc[VTIME] = 50; // 5 seconds
options.c_cc[VMIN] = FrameLength;
tcflush(fd,TCIOFLUSH);
/* Update the options and do it NOW */
if (tcsetattr(fd,TCSANOW,&options) != 0)
{
perror("SetupSerial 3");
return (false);
}
return (true);
}
复制代码
int Uart_OpenDev(char *Dev)
{
int fd = open( Dev, O_RDWR | O_NOCTTY ); //| O_NOCTTY | O_NDELAY | O_NONBLOCK
if (-1 == fd)
{
perror("Can't Open Serial Port");
return -1;
}
else
return fd;
}
复制代码
上面是串口配置的基本函数
int Uart_SendFrame(int fd)
{
int i,nwrite;
nwrite = write(fd,Send_Buf,FrameLength);
printf("\nSend %d Bytes\n",nwrite);\
for(i=0;i<FrameLength;i++)
printf("%d ",(u8)Send_Buf[i]);
printf("\n");
return nwrite;
}
复制代码
int Uart_RecvFrame(int fd)
{
int i,nread;
nread = read(fd,Recv_Buf,FrameLength);
printf("\nReceive %d Bytes\n",nread);
for(i=0;i<FrameLength;i++)
printf("%d ",(u8)Recv_Buf[i]);
printf("\n");
return nread;
}
复制代码
上面两个是串口接收和发送的两个函数,问题就是在write函数,不能发送,求大神们来帮帮小弟吧,头都要炸了T_T
作者:
449569708
时间:
2012-02-06 16:15
你可以检查一下返回值nwrite是否正确?
作者:
rudyfly
时间:
2012-02-09 13:24
正确的,返回值是要发送的字符数目的
回复
2#
449569708
作者:
snow888
时间:
2012-02-09 14:52
应该不会有问题啊。
你确定你的用户对那个设备文件有写权限么?
作者:
snow888
时间:
2012-02-09 14:53
一般就是 open 函数打开那个设备文件,然后用 write 函数写啊。
作者:
linux.sz
时间:
2012-02-09 15:25
串口有三条线,是否都通?
先别自己写软件,用串口助手这种工具试下。
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2