求助!会者不难,难者不会
时间:2010-10-28
来源:互联网
我照书上写了一个简单的linux串口通信问题,可是结果总是很奇怪。我用一个串口连续的向PC发送“hello,wlp ”的字符串(最后有个空格)。 在终端运行我的程序的时候,结果如下:
1、先是显示
fcntl=0
isatty success!
fd-open=3
set done!
2、然后等几秒后才显示(select函数怎么会等待这么长时间?)
llo,wlp hello,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp llo,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp p llo,wlp hehello,wp lp llo,wp hello,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp p llo,wlp hlp hello,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp hello,wlp p
3、我想要的结果是
hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp
这是在windows下测试的结果,发送肯定没有问题 。我想知道这是为什么?
linux的串口的缓冲有多大?它是怎么工作的?为什么有时候会出现he被p 替代的情况?
我的代码如下:
1、先是显示
fcntl=0
isatty success!
fd-open=3
set done!
2、然后等几秒后才显示(select函数怎么会等待这么长时间?)
llo,wlp hello,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp llo,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp p llo,wlp hehello,wp lp llo,wp hello,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp p llo,wlp hlp hello,wlp p llo,wlp hello,wlp p llo,wlp hello,wlp hello,wlp p
3、我想要的结果是
hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp hello,wlp
这是在windows下测试的结果,发送肯定没有问题 。我想知道这是为什么?
linux的串口的缓冲有多大?它是怎么工作的?为什么有时候会出现he被p 替代的情况?
我的代码如下:
代码:
#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;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
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)//串口1
{ fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
else if(comport==2)//串口2
{ fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
else if (comport==3)//串口3
{
fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
/*恢复串口为阻塞状态*/
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,nwrite,i;
char buff[12];
fd_set rd;
if((fd=open_port(fd,1))<0){
perror("open_port error");
return;
}
if((i=set_opt(fd,115200,8,'N',1))<0){
perror("set_opt error");
return;
}
FD_ZERO(&rd);
FD_SET(fd,&rd);
while(1){
if(select(fd+1,&rd,NULL,NULL,NULL)<0)
perror("select");
else{
while( (nread = read(fd,buff,12)) >0)
{buff[12]='\0';
printf("%s",buff);
}
}
}
return 0;
}
#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;
case 460800:
cfsetispeed(&newtio, B460800);
cfsetospeed(&newtio, B460800);
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)//串口1
{ fd = open( "/dev/ttyS0", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
else if(comport==2)//串口2
{ fd = open( "/dev/ttyS1", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
else if (comport==3)//串口3
{
fd = open( "/dev/ttyS2", O_RDWR|O_NOCTTY|O_NDELAY);
if (-1 == fd){
perror("Can't Open Serial Port");
return(-1);
}
}
/*恢复串口为阻塞状态*/
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,nwrite,i;
char buff[12];
fd_set rd;
if((fd=open_port(fd,1))<0){
perror("open_port error");
return;
}
if((i=set_opt(fd,115200,8,'N',1))<0){
perror("set_opt error");
return;
}
FD_ZERO(&rd);
FD_SET(fd,&rd);
while(1){
if(select(fd+1,&rd,NULL,NULL,NULL)<0)
perror("select");
else{
while( (nread = read(fd,buff,12)) >0)
{buff[12]='\0';
printf("%s",buff);
}
}
}
return 0;
}
作者: wlp822 发布时间: 2010-10-28
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28