yonganxie
驱动牛犊
驱动牛犊
  • 注册日期2002-03-07
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分88分
  • 威望13点
  • 贡献值0点
  • 好评度6点
  • 原创分0分
  • 专家分0分
阅读:1379回复:1

Linux下串口问题

楼主#
更多 发布于:2004-11-03 19:21
我编了一个简单的串口程序,是两台PC机间COM1通信,PC上都装的redhat8.0,发送方能正常往串口发送数据,但是接受方程序一运行到read()时就停在那儿了,如下:
[root@gyh hit]# ./readtest1
open /dev/ttyS0: Success
ready for receiving data ...

相关地方程序语句:
printf("ready for receiving data ...\n");
retv=read(spfd,rbuf,1);
if(retv==-1) perror("read");

可见运行到上面第二句就过不去了,并不是不能读的错误。
不知道问题出在哪儿,不会是串口坏了吧。
为了便于理解,我把整个接受程序附在后面:
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <math.h>
int spfd;
int main()
{
char fname[16],hd[16],*rbuf;
int retv,i,ncount=0;
struct termios oldtio;
int realdata=0;
int j,tens;
spfd=open("/dev/ttyS0",O_RDWR | O_NOCTTY);
perror("open /dev/ttyS0");
if (spfd<0) return -1;
tcgetattr(spfd,&oldtio);
cfmakeraw(&oldtio);
cfsetispeed(&oldtio,B19200);
cfsetospeed(&oldtio,B19200);
tcsetattr(spfd,TCSANOW,&oldtio);
rbuf=hd;
printf("ready for receiving data ...\n");

retv=read(spfd,rbuf,1);

if(retv==-1) perror("read");
while (*rbuf!='\0')
{
ncount+=1;
rbuf++;
retv=read(spfd,rbuf,1);
printf("the number received is %d\n",retv);
if (retv==-1) perror("read");
}
for (i=0;i<ncount;i++)
{
for(j=0;j<ncount-i-1;j++)
{
tens=1;
tens*=10;
}
realdata+=(hd-48)*tens;
}
printf("complete receiving the data %d \n",realdata);
close(spfd);
return 0;
}
bodajingshen000
驱动牛犊
驱动牛犊
  • 注册日期2004-04-07
  • 最后登录2009-01-04
  • 粉丝0
  • 关注0
  • 积分1分
  • 威望10点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-12-30 15:00
Michael Sweet: Serial Programming Guide for POSIX Operating Systems是Linux串口编程的经典文章。但其参数配置中有个小错误,我具体忘记了,大概是CSIZE的问题。
游客

返回顶部