日期:2014-05-16  浏览次数:20650 次

串口报Illegal seek错误(高分求助)
我用的是YC2410-F的板子,板子上串口0和PC机相连,串口1和串口用串口线相连,程序同时打开两个串口,让一个发一个接,
可第二个打开的串口老报Illegal seek,自然也接不到数据,大家帮我看看是怎么回事
程序如下:
int main(int argc, char **argv)
{
int fd1,fd2,i,j,len;
int nread,nwrite;
char read_buff[READ_BUFF];
char write_buff[WRITE_BUFF]={"send...."};
fd1 = open_serial(1);
if (fd1>0)
set_speed(fd1,SPEED);
else
  {
printf("Can't Open Serial Port1!\n");
exit(0);
  }

if (set_Parity(fd1,8,1,'N')== -1)
  {
printf("Set Parity1 Error\n");
exit(1);
  }

fd2 = open_serial(2);
if (fd2>0)
set_speed(fd2,SPEED);
else
  {
printf("Can't Open Serial Port2!\n");
exit(0);
  }
if (set_Parity(fd2,8,1,'N')== -1)
  {
printf("Set Parity2 Error\n");
exit(1);
  }
 
len=strlen(write_buff);
printf("Sending:");
for(i=0;i<len;i++)
{
nwrite = write(fd1,&write_buff[i],1);
if(nwrite!=1)
{
printf("Write error!\n");
exit(0);
}
else printf("%c,",write_buff[i]);
for(j=0;j<0x5fff;j++) j=j;
}
printf("\n");

  nread = read(fd2,read_buff,1);
if(nread>0)
{
printf("The read length is: %d\n",nread);
read_buff[nread+1]='\0';
printf("\n%s\n",read_buff);
}
else printf("Read nothing!\n");

close(fd1);
close(fd2);

exit(0);

------解决方案--------------------
"串口1和串口用串口线相连"
是不是少打字了

楼主把代码"nread = read(fd2,read_buff,1); "去掉试试看

再把串口1给关闭了后再加上 nread = read(fd2,read_buff,1); 代码试试看

网上说好像是内核问题,用USB转串口,或者升级内核,还有改驱动..
------解决方案--------------------
int status;
ioctl(fd2, FIONREAD, &status);
if(status>0)
nread = read(fd2,read_buff,1); 

这样读应该就可以了.
------解决方案--------------------
第二个串口驱动加载了没?
------解决方案--------------------
你那个串口2也是板子上的吧?
你怎么把板子的两个串口连上了?
------解决方案--------------------
你的串口1是不是有问题, 其他串口没事吧.
------解决方案--------------------
串口驱动有问题吧.
------解决方案--------------------
我确定,因为都试过
现在程序别裁减成这样了,可还是打不开第二个串口 


----------------------------------
这样就是说,你单独用一个串口都好用,但同时用就出问题?
那你这样在实验一下,用两个程序,一个打开串口1,一个打开串口2,同时运行,看情况如何.
或则你open时参数换成O_RDWR|O_NOCTTY看一下.
一般程序不是默认的都用/dev/ttyS0,/dev/ttyS1当串口啊,怎么你们的用/dev/tts/0这样的呀