linux下串口程序,有时能接收数据,有时不能

nevergetwin 2010-10-11 10:49:29
刚学linux,按照视频讲座的源代码运行,发现有时候能够接收到串口数据,有时候不能。串口设备不停的发送16进制数据:
3a 34 41 32 50 41 00 03 ...每次发送30字节,计算机串口接收。第一次运行程序./seri ,接收到3字节之后程序退出,正常。然后我再运行一遍./seri 结果就接收不到数据了。之后无论运行多少遍程序,都不能再接收到数据。后来我用windows下的串口调试助手接收一次数据,再运行./seri 就又可以接收到数据了。我用的是cygwin模拟linux运行环境的,请高手给指点一下,为什么这个程序关闭之后再次运行的时候就不能接收串口数据了?
源码如下:
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>

int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio,oldtio;
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;

switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}

switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}

switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
printf("set done!\n");
return 0;
}

int open_port(int fd,int comport)
{
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;
if (comport==1)
{ fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS0 .....\n");
}
else if(comport==2)
{ fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS1 .....\n");
}
else if (comport==3)
{
fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS2 .....\n");
}
if(fcntl(fd, F_SETFL, 0)<0)
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
printf("fd-open=%d\n",fd);
return fd;
}

int main(void)
{
int fd;
int nread,i;
char buff[]="Hello\n";

if((fd=open_port(fd,1))<0){
perror("open_port error");
return;
}
if((i=set_opt(fd,9600,8,'N',1))<0){
perror("set_opt error");
return;
}
printf("fd=%d\n",fd);
// fd=3;
nread=read(fd,buff,3);
printf("nread=%d,%s\n",nread,buff);
close(fd);
return;
}
...全文
913 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
nevergetwin 2010-10-19
  • 打赏
  • 举报
回复
视频是从网上下载的“中嵌教育”嵌入式教程。
串口设备是一个控制器,通过串口和计算机通信的,有固定通信协议。
手机写程序 2010-10-14
  • 打赏
  • 举报
回复
没有测试,不知道你说的视频和连接的串口设备是啥.
我在vmware上和arm板上调过.实在不行你把cygwin换成vmware启动linux或者单启动linux试试.
nevergetwin 2010-10-14
  • 打赏
  • 举报
回复
楼上的eyey1,谢谢你的回答。
但是我试过了还是不行,不知道你是不是测试通过了呢?
你们有没有测试通过的串口收发例程,发到我邮箱里面并告知你的论坛ID谢谢!
mengt@718.ac.cn
手机写程序 2010-10-13
  • 打赏
  • 举报
回复

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <stdlib.h>

struct termios oldtio;//这里

int set_opt(int fd,int nSpeed, int nBits, char nEvent, int nStop)
{
struct termios newtio;//,oldtio;//这里
if ( tcgetattr( fd,&oldtio) != 0) {
perror("SetupSerial 1");
return -1;
}
bzero( &newtio, sizeof( newtio ) );
newtio.c_cflag |= CLOCAL | CREAD;
newtio.c_cflag &= ~CSIZE;

switch( nBits )
{
case 7:
newtio.c_cflag |= CS7;
break;
case 8:
newtio.c_cflag |= CS8;
break;
}

switch( nEvent )
{
case 'O':
newtio.c_cflag |= PARENB;
newtio.c_cflag |= PARODD;
newtio.c_iflag |= (INPCK | ISTRIP);
break;
case 'E':
newtio.c_iflag |= (INPCK | ISTRIP);
newtio.c_cflag |= PARENB;
newtio.c_cflag &= ~PARODD;
break;
case 'N':
newtio.c_cflag &= ~PARENB;
break;
}

switch( nSpeed )
{
case 2400:
cfsetispeed(&newtio, B2400);
cfsetospeed(&newtio, B2400);
break;
case 4800:
cfsetispeed(&newtio, B4800);
cfsetospeed(&newtio, B4800);
break;
case 9600:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
case 115200:
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
break;
default:
cfsetispeed(&newtio, B9600);
cfsetospeed(&newtio, B9600);
break;
}
if( nStop == 1 )
newtio.c_cflag &= ~CSTOPB;
else if ( nStop == 2 )
newtio.c_cflag |= CSTOPB;
newtio.c_cc[VTIME] = 0;
newtio.c_cc[VMIN] = 0;
tcflush(fd,TCIFLUSH);
if((tcsetattr(fd,TCSANOW,&newtio))!=0)
{
perror("com set error");
return -1;
}
printf("set done!\n");
return 0;
}

int open_port(int fd,int comport)
{
char *dev[]={"/dev/ttyS0","/dev/ttyS1","/dev/ttyS2"};
long vdisable;
if (comport==1)
{ fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS0 .....\n");
}
else if(comport==2)
{ fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS1 .....\n");
}
else if (comport==3)
{
fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
else
printf("open ttyS2 .....\n");
}
if(fcntl(fd, F_SETFL, 0)<0)
printf("fcntl failed!\n");
else
printf("fcntl=%d\n",fcntl(fd, F_SETFL,0));
if(isatty(STDIN_FILENO)==0)
printf("standard input is not a terminal device\n");
else
printf("isatty success!\n");
printf("fd-open=%d\n",fd);
return fd;
}

int main(void)
{
int fd;
int nread,i;
char buff[]="Hello\n";

if((fd=open_port(fd,1))<0){
perror("open_port error");
return;
}
if((i=set_opt(fd,9600,8,'N',1))<0){
perror("set_opt error");
return;
}
printf("fd=%d\n",fd);
// fd=3;

sleep(1);//这里

nread=read(fd,buff,3);
printf("nread=%d,%s\n",nread,buff);

tcsetattr(fd,TCSANOW,&oldtio);//这里

close(fd);
return;
}


nevergetwin 2010-10-13
  • 打赏
  • 举报
回复
在cygwin下面串口不能接收数据。
然后我用串口调试助手打开一次串口,再关闭。
再在cygwin下面运行这个程序就又可以接收数据了,为什么?
nevergetwin 2010-10-12
  • 打赏
  • 举报
回复
没人遇到这个问题吗?顶
nevergetwin 2010-10-11
  • 打赏
  • 举报
回复
2楼的,说清楚一点吧,怎么置空缓冲区?读取的长度有什么问题?无论我换成多少字节也还是一样。
cr4315 2010-10-11
  • 打赏
  • 举报
回复
read只读取一次就退出了,再次读的时候把缓冲区置空试下,而且读取的长度也有问题
手机写程序 2010-10-11
  • 打赏
  • 举报
回复
nread=read(fd,buff,3);前加sleep(1)试试.

23,116

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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