Anomymous 发表于 2005-5-25 17:55:01

读串口失败,求助!

今天把编译通过的读串口程序在linux下运行,但在pc串口上接好gps后却没什么反应,关键在read上,用到read(fd,buff,1)程序就没反应了,连之前的printf都没反应,把此句注释掉后所有printf都能输出。我是用虚拟机vmare里的linux的,是程序原因还是我的虚拟机不支持穿口啊???请大虾们多多帮助。

大致程序如下:
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<stdlib.h>
#include<termios.h>
#include<fcntl.h>
int status;
char ca,tmp;
int quart(int fp);
main()
{
int fp;
int max=80;
int i;
unsigned char wd,jd,Data;
unsigned char Time,PositionX,PositionY,Date;
unsigned char cc;
struct termios opt;
fd=open("/dev/ttyS0",O_RDWR);//以读写方式打开串口1
//设置串口:波特率19200,8位数据位,奇校验、1位停止位
if (fd<0)
    {perror("/dev/ttyS0");
   exit(-1);}          
tcgetattr(fd,&opt);
cfsetispeed(&opt,B19200);   
cfsetospeed(&opt,B19200);
opt.c_cflag &= ~PARENB;
opt.c_cflag &= ~PARODD;
opt.c_cflag &= ~CSTOPB;
opt.c_cflag &= ~CSIZE;
opt.c_cflag |= ~CS8;
tcsetattr(fd,TCSANOW,&opt);
printf("please wait");
while(!quart(fp)) ; //等待帧标志:
for(i=0;i {read(fp,cc,1);
Data=cc;
printf("%c",Data);}
}

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

limingth 发表于 2005-5-26 00:30:06

可能是 vmware 的问题吧,你试试在 vmware 里面 echo "test" > /dev/ttyS0
然后接另一台机器看看能否接收到 test 字符串? :neutral:
页: [1]
查看完整版本: 读串口失败,求助!