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

12,561

Your USB-to-RS232 is probably using an FTDI chip. The idea in Linux is that any drivers added to the kernel can by accessed by manipulating an entry in the /dev filesystem.

The very first thing you need to know when working with Linux (or any UNIX variant) is that everything is a file. 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)

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. As roderigo mentioned, the device file is most probably called ttyUSB0, but ttyS0 is not impossible.

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. When you're finished close the port with close(fd)

To set your line parameters search either the minicom source or the Linux documentation for the termios structure.

Get a hold of the book "Linux Programming Unleashed" by Kurt Wall, et al. I think it is a must have for anyone writing C code for applications running on Linux.

Good luck.

Share:
12,561
Jason Hu
Author by

Jason Hu

Programming world is not fair. http://hustmphrrr.github.io/

Updated on June 04, 2022

Comments

  • Jason Hu
    Jason Hu almost 2 years

    i have a project about communicate with particular devices via a RS-232 wire recently. since my computer has no serial port, i use a USB-to-RS232 wire to be the intermediate between devices and my computer. but i am new in Linux drivers, so it's hard for me catch the idea of it.

    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. 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.

    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. Please help me out.