LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 569|回复: 0

串口编程遇到的问题

[复制链接]
发表于 2004-10-26 19:18:23 | 显示全部楼层 |阅读模式
我编了一个简单的串口程序,是两台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;
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表