简体   繁体   English

在Linux中如何通过USB到RS232线与设备通信?

[英]how to communicate with devices via a USB-to-RS232 wire in Linux?

i have a project about communicate with particular devices via a RS-232 wire recently. 我最近有一个关于通过RS-232线与特定设备进行通信的项目。 since my computer has no serial port, i use a USB-to-RS232 wire to be the intermediate between devices and my computer. 由于我的计算机没有串行端口,因此我使用USB到RS232电缆作为设备和计算机之间的中间设备。 but i am new in Linux drivers, so it's hard for me catch the idea of it. 但是我是Linux驱动程序的新手,所以我很难理解它。

i am working under Debian with a default working driver in it and i have tested whatever could be done on hardware side and found everything just fine. 我在Debian下使用默认的工作驱动程序进行工作,并且我已经测试了可以在硬件方面进行的任何操作,发现一切都很好。 now the case comes to be how to send commands to the devices and receive corresponding data in C. i've read some books and i think maybe mapping the usb port to be a tty device would work. 现在的情况是如何将命令发送到设备并在C中接收相应的数据。我已经读了几本书,我认为也许将usb端口映射为tty设备就可以了。

am i thinking in the right way? 我在以正确的方式思考吗? i am kind of confused. 我有点困惑。 i downloaded the source code of "minicom" and read some lines of it, but i still don't quite get it. 我下载了“ minicom”的源代码并阅读了几行,但是我还是不太明白。 Please help me out. 请帮帮我。

Your USB-to-RS232 is probably using an FTDI chip. 您的USB转RS232可能正在使用FTDI芯片。 The idea in Linux is that any drivers added to the kernel can by accessed by manipulating an entry in the /dev filesystem. Linux中的想法是,可以通过操纵/ dev文件系统中的条目来访问添加到内核的任何驱动程序。

The very first thing you need to know when working with Linux (or any UNIX variant) is that everything is a file. 使用Linux(或任何UNIX变体)时,您需要了解的第一件事是所有内容都是文件。 So unlike Windows (where a manufacturer creates a dll and tells you which functions to call) in Linux you use standard file system functions (note to the purists: I am leaving out ioctl for simplicity reasons) 因此,与Windows(制造商在其中创建一个dll并告诉您要调用的函数)不同,在Linux中,您使用标准的文件系统函数(纯粹主义者注意:为简单起见,我省略了ioctl)

So look in the dev directory and see what entries appear when you plug your "wire" in and what disappears when you take it out. 因此,查看dev目录,看看插入“电线”时会出现什么条目,取出时会消失什么。 As roderigo mentioned, the device file is most probably called ttyUSB0, but ttyS0 is not impossible. 如roderigo所述,设备文件很可能称为ttyUSB0,但ttyS0并非不可能。

In your program you then open this "file": fd = open("/dev/ttyUSB0", O_RDWR) You can use the functions write and read to send and receive characters from your com port. 然后在程序中打开此“文件”: fd = open("/dev/ttyUSB0", O_RDWR)可以使用writeread函数从com端口发送和接收字符。 When you're finished close the port with close(fd) 完成后,使用close(fd)关闭端口

To set your line parameters search either the minicom source or the Linux documentation for the termios structure. 要设置线路参数,请在minicom源代码或Linux文档中搜索termios结构。

Get a hold of the book "Linux Programming Unleashed" by Kurt Wall, et al. 抓住Kurt Wall等人的书“ Linux Programming Unleashed”。 I think it is a must have for anyone writing C code for applications running on Linux. 我认为这是为在Linux上运行的应用程序编写C代码的任何人的必备条件。

Good luck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM