Ubuntu 上使用 USB 转串口转换器进行串行通信
我有一个基于 FTDI 芯片的 USB 转串口转换,我将在某一时刻尝试通过 RS232 与电视进行通信。我正在运行 Ubuntu Maverick。
我是串行通信新手,不明白为什么我不能简单地从端口读取字节。
串行环回测试成功。我将 Tx 和 Rx 短接在一起并运行以下 C 程序,屏幕上出现了键盘回声。
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc,char** argv)
{
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
unsigned char c='D';
printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
cfsetospeed(&tio,B115200); // 115200 baud
cfsetispeed(&tio,B115200); // 115200 baud
tcsetattr(tty_fd,TCSANOW,&tio);
while (c!='q')
{
if (read(tty_fd,&c,1)>0) write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
if (read(STDIN_FILENO,&c,1)>0)
{
write(tty_fd,&c,1); // if new data is available on the console, send it to the serial port
}
}
close(tty_fd);
}
然后,我将 USB 端插入 PC 上的 USB (/dev/ttyUSB0) 端口,并将电缆的 9 针端插入串行端口 (/dev/ttyS0)。
我在 /dev/ttyUSB0 上运行了前面的程序,并在另一个终端窗口中键入:
echo "Hello world!" > /dev/ttyS0
什么也没有出现。我还尝试在 /dev/ttyUSB0 和 /dev/ttyS0 上的两个单独的终端中运行该程序,但无法从一个终端到另一个终端进行通信。
有谁知道我在这里做错了什么?
提前致谢!
I have an FTDI chip based USB to serial converted that I will at one point try to communicate to a TV with via RS232. I am running Ubuntu Maverick.
I'm new to serial communications and can't figure out why I can't simply read bytes from the port.
A serial loopback test was successful. I shorted Tx and Rx together and ran the following C program and my keyboard was echoed on the screen.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
int main(int argc,char** argv)
{
struct termios tio;
struct termios stdio;
int tty_fd;
fd_set rdset;
unsigned char c='D';
printf("Please start with %s /dev/ttyS1 (for example)\n",argv[0]);
memset(&stdio,0,sizeof(stdio));
stdio.c_iflag=0;
stdio.c_oflag=0;
stdio.c_cflag=0;
stdio.c_lflag=0;
stdio.c_cc[VMIN]=1;
stdio.c_cc[VTIME]=0;
tcsetattr(STDOUT_FILENO,TCSANOW,&stdio);
tcsetattr(STDOUT_FILENO,TCSAFLUSH,&stdio);
fcntl(STDIN_FILENO, F_SETFL, O_NONBLOCK); // make the reads non-blocking
memset(&tio,0,sizeof(tio));
tio.c_iflag=0;
tio.c_oflag=0;
tio.c_cflag=CS8|CREAD|CLOCAL; // 8n1, see termios.h for more information
tio.c_lflag=0;
tio.c_cc[VMIN]=1;
tio.c_cc[VTIME]=5;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
cfsetospeed(&tio,B115200); // 115200 baud
cfsetispeed(&tio,B115200); // 115200 baud
tcsetattr(tty_fd,TCSANOW,&tio);
while (c!='q')
{
if (read(tty_fd,&c,1)>0) write(STDOUT_FILENO,&c,1); // if new data is available on the serial port, print it out
if (read(STDIN_FILENO,&c,1)>0)
{
write(tty_fd,&c,1); // if new data is available on the console, send it to the serial port
}
}
close(tty_fd);
}
I then plugged the USB side into a USB (/dev/ttyUSB0) port on my PC and the 9-pin side of the cable to the serial port (/dev/ttyS0).
I ran the previous program on /dev/ttyUSB0 and typed in another terminal window:
echo "Hello world!" > /dev/ttyS0
Nothing appears. I also tried running the program in two separate terminals on both /dev/ttyUSB0 and /dev/ttyS0 and I couldn't communicated from one to the other.
Does anyone know what I'm doing wrong here?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要配置 USB 串行端口和 db9 串行端口。 (即波特率、硬件、流量控制等) stty 或setserial 是两个可以完成此操作的Linux 命令,请查看手册页或在线示例。
之后,您应该能够将字符串输入设备并且它应该可以工作。
i think you need to configure both usb serial and db9 serial ports. (ie baud rates, hardware , flowcontrol, etc.) stty or setserial are two linux commands that could accomplish this, check on the man pages or examples online.
After that you should be able to pipe strings into the devices and it should work.