+ -
当前位置:首页 → 问答吧 → 【求助】读串口时没反应(有程序)

【求助】读串口时没反应(有程序)

时间:2005-05-25

来源:互联网

我现在在做ARM S3C4510B的嵌入式开发,写了个读串口的程序,但是一用到read(fd,buff,1)程序就没反应了,连之前的printf都没反应,把此句注释掉后所有printf都能输出。我是用虚拟机vmare里的linux的,是程序原因还是我的虚拟机不支持穿口啊???请大虾们多多帮助。

主体程序如下://#####################gps.c############################
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<termios.h>
#include<fcntl.h>
int status;
char ca[8],tmp;
int quart(int fp);  
main()
{
  int fp;
  int max=80;
  int i;
  unsigned char wd,jd,Data[512];
  unsigned char Time[32],PositionX[32],PositionY[32],Date[32];
  unsigned char cc[32];
  struct termios opt;
  fp=open("/dev/ttyS0",O_RDWR);//以读写方式打开串口1
//设置串口:波特率19200,8位数据位,奇校验、1位停止位
  tcgetattr(fp,&opt);
  tcflush(fp,TCIOFLUSH);
  cfsetispeed(&opt,B19200);
  cfsetospeed(&opt,B19200);
  opt.c_iflag=1;
  opt.c_oflag=0;
  opt.c_cflag=3261;
  opt.c_lflag=0;
  opt.c_line=' ';
  tcsetattr(fp,TCSANOW,&opt);
  tcflush(fp,TCIOFLUSH);
  printf("please wait");
  while(!quart(fp)) ;     //等待帧标志:
  for(i=0;i<max;i++)
  {read(fp,cc,1);
   Data=cc[0];
   printf("%c",Data);}
}

  int quart(int fp)  //fp是IO口句柄,如果收到标志,返回1,否则0;
{ status=0;
  read(fp,ca,1);
  tmp=ca[0];
  if(tmp=='$')  //帧头,SectionID为逗号计数器/
    status=1;
    return status;
}      

作者: 小样82   发布时间: 2005-05-25

应该是虚拟机的原因。运行read(fd,buf,1)导致前面的输出都没有了是因为你前面的输出没有“\n”回车换行,前面的字符在缓冲区中来不及打印。      

作者: 风雪狂客   发布时间: 2005-06-03

如何设置串口全双工方式工作呢?输入和输出使用同一个缓冲区吗?      

作者: newabc   发布时间: 2005-11-03