我正在研究 FTDI 芯片(VNC2 评估板)。我想通过我的程序从芯片中读取串行数据。我现在拥有的读取串行数据的代码是
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <signal.h>
#include <sys/time.h>
#include <signal.h>
void main()
{
int portfd,rd;
int n,f,len=0,t;
char buf,*s;
struct termios oldtio,newtio;
portfd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if (portfd<0) printf("Error opening port\n");
else if(portfd>0)
{
printf("Serial Port open . portfd=%d\n",portfd);
tcgetattr(portfd,&oldtio);
bzero(&newtio, sizeof(newtio));
cfsetispeed(&newtio, B115200);
cfsetospeed(&newtio, B115200);
newtio.c_cflag |= (CLOCAL | CREAD);
newtio.c_cflag &= ~PARENB; // set parity to no
newtio.c_cflag &= ~CSTOPB; /*set one stop bit */
newtio.c_cflag &= ~CSIZE; /* Mask the character size bits */
newtio.c_cflag |= CS8; /* 8 bit data */
newtio.c_cflag &= ~(ICANON | ECHO | ECHOE |ISIG);
newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
newtio.c_oflag &= ~OPOST;
newtio.c_cflag &= ~CRTSCTS; // no flow control
newtio.c_lflag = 0; // no signaling chars, no echo, no canonical processing
newtio.c_oflag = 0; // no remapping, no delays
newtio.c_cc[VMIN] = 0; // read doesn't block
newtio.c_cc[VTIME] = 5;
tcflush(portfd, TCIFLUSH);
if(tcsetattr (portfd, TCSANOW, &newtio) != 0)
{
printf("Error from tcsetattr\n");
}
n = fcntl(portfd, F_GETFL, 0);
(void)fcntl(portfd, F_SETFL, n & ~O_NDELAY);
while(1){
rd= read(portfd,&buf,sizeof(char));
//sleep(3);
if(rd==-1) printf("Reading error rd:%d\n",rd);
else if(!rd) printf("No Data to read rd:%d\n",rd);
else
printf(" data read %c %d\n",buf,(int)buf);
}
tcsetattr(portfd, TCSANOW, &oldtio);
}
close(portfd);
}
但我仍然无法读取数据。请帮我继续。
谢谢你。