linuxc中的rs232串行通信
我是Linux新手。我面临着串行通信的问题。 我有两台计算机,一台运行超级终端,另一台编写C程序与超级终端通信。两台PC都通过RS232连接。 我已经写了一些代码,我能够将数据发送到超级终端,但我不确定如何在Linux PC上用C编写的程序从超级终端读取数据 有什么建议吗 提前谢谢 下面是我的代码:linuxc中的rs232串行通信,c,linux,C,Linux,我是Linux新手。我面临着串行通信的问题。 我有两台计算机,一台运行超级终端,另一台编写C程序与超级终端通信。两台PC都通过RS232连接。 我已经写了一些代码,我能够将数据发送到超级终端,但我不确定如何在Linux PC上用C编写的程序从超级终端读取数据 有什么建议吗 提前谢谢 下面是我的代码: int main(int argc, char **argv) { struct termios options; int fd; fd = open("/dev/ttyS
int main(int argc, char **argv)
{
struct termios options;
int fd;
fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if(fd == -1)
{
printf("Could not open port /dev/ttyS0\n");
return 1;
}
tcgetattr(fd, &options); //get port options
cfsetispeed(&options, B9600); //set input baud rate
cfsetospeed(&options, B9600); //set output baud rate
options.c_cflag |= (CLOCAL | CREAD); //enable receiver and set local mode
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS; //disable hardware flow control
options.c_cflag &= ~(ICANON | ECHO | ISIG); //set raw input
options.c_cflag |= IXOFF; //disable software flow control
tcsetattr(fd, TCSANOW, &options); //set new port options
sleep(1);
int rc,count;
int size = 8;
unsigned char buf[10];
FILE *fp = NULL;
char ch;
int i=0;
printf("enter the data you want to send");
while((ch=getchar())!='\n')
{
write(fd,&ch,1);
}
close(fd);
printf("Finished.\n");
return 0;
}
?您能给我们看一下与超级终端通信的部件的代码吗?部分是您试图读取数据的部分。@Glenn感谢您的回复……我给您的是能够与超级终端进行通信的功能。i、 e单向通信。当我运行此代码时,我能够将数据从linux box发送到超级终端…当我将数据从超级终端发送到linux PC时,我面临如何获取数据的问题。我将使用文件描述符上的读取[link]从超术语获取数据,类似于您必须发送数据的写入。我认为读取应该阻塞,直到有数据。如果没有,您可以使用select来确定是否有数据要读取。read()有什么问题?您也可以将picocom的代码作为参考。