linux串口编程,write函数不能发送数据

rudyfly 2012-02-05 09:50:03

本人最近在写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
...全文
924 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
rudyfly 2012-02-09
  • 打赏
  • 举报
回复
谢谢你,问题已经解决了,数据是发出去了,接收端有点问题而已[Quote=引用 1 楼 falloutmx 的回复:]

cat /proc/tty/driver/serial
看看是不是数据真的发出去了。如果发出去了,八成就是配置配不对了,用示波器抓波形吧
[/Quote]
rudyfly 2012-02-09
  • 打赏
  • 举报
回复
谢谢你,问题已经解决了,数据是发出去了,接收端有点问题而已[Quote=引用楼主 rudyfly 的回复:]
本人最近在写linux下的串口编程,是一个有串口的台式机和STM32的单片机板子的串口通信,板子上的串口通信是可行的,因为在window下的串口调试工具测试了,收发数据都是没有问题的,可是在linux下,read函数可以收到板子发来的数据,可是却不能向板子发送数据,板子中的程序是这样的:当收到数据是,做适当的延时后,把收到的数据再发给台式机。
read函数可以收到数据在终端显示,但是write函……
[/Quote]
falloutmx 2012-02-06
  • 打赏
  • 举报
回复
cat /proc/tty/driver/serial
看看是不是数据真的发出去了。如果发出去了,八成就是配置配不对了,用示波器抓波形吧

21,597

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧