照着书上写的串口驱动测试程序,现在有问题了。
问题出现在下面这个程序当中!
series.c文件
#include "series.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;
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;
}
先在PC机上读写串口
lcw@series_driver>>> sudo ./gcc_write
fcntl = 0
isatty success!
fd-open = 3
set done!
fd=3
nwrite=8
lcw@series_driver>>>
lcw@series_driver>>> sudo ./gcc_read
fcntl = 0
isatty success!
fd-open = 3
set done!
nread = 8, Hello!
在PC机上可以读写取,现将读取函数交叉编译下载到板子上出现了问题
[root@lcw /home]#./arm_gcc_read
fcntl = 0
isatty success!
fd-open = 3
SetupSerial 1: Input/output error
set_opt error: Illegal seek
[root@lcw /home]#
[root@lcw /home]#./arm_gcc_write
fcntl = 0
isatty success!
fd-open = 3
SetupSerial 1: Input/output error
set_opt error: Illegal seek
[root@lcw /home]#
读取写入都出现了错误,据我分析问题是当下载到板子上时,读写函数在打开secies.c文件中的
tcgetattr( fd, &oldtio )时返回了负值,但是在PC机上是正确是,这是怎么回事呢?
请牛人们帮帮忙吧!
检查一下串口驱动有没有mount起来。PC也跑linux?看来革命的很彻底呀。板子是什么ARM平台?
linux最好到linux/Unix社区,或者直接到那些专业的Linux论坛。
看来service.c驱动应该是已经正常加载了,不然它也无法调到set_opt这函数里进来
关键是你返回错误的函数,看不到相关的代码也不知道会是什么原因?在tcgetattr这函数里跟踪一下看看吧