3

I need to communicate with Python over a USB to RS-232 converter, to a device. The application minicom connects seamlessly, so if I could run that in non-interactive mode, everything would would be great.

Reading the documentation there are some interesting options, but I'm not fully understanding any one of them:

 -t   Terminal type. With this flag, you can override the environment 
      TERM variable. 

 -S   script.   Run  the named script at startup.

How do I pipe data between my application and minicom? Should I use something else?

1
  • 1
    I've never tried this but I think you can just connect to /dev/ttySUSB0 directly from python. You don't need minicom. Is there something specific that it gives you over a direct connection? Also you can use screen to connect to /dev/ttySUSB0 too. See here: cyberciti.biz/hardware/…. Let me know if this works for you and I can write it up more formally as an answer.
    – slm
    Sep 2, 2013 at 21:32

3 Answers 3

6

minicom is great for interactive use, but it's not the right tool for programmatic I/O.

Your local Python program should simply open the /dev node for the serial port. It works just like writing to a file:

fd = os.open('/dev/ttyUSB0', os.O_RDWR)
fd.write(...)

The only tricky bit is setting up the bit rate and such. For that, use Python's termios library:

attr = termios.tcgetattr(fd)
attr[5] = attr[6] = termios.B9600
termios.tcsetattr(fd, termios.TCSANOW, attr)

The Python docs for this pretty much assume you've used this API from C and just need help translating to Python. So, if you go down this path, you should use classics like Stevens and Rago's Advanced Programming in the Unix Environment.

A higher-level library that gets you above the termios level and gives you portability to non-*ix type systems is pySerial.

1
  • I did it with pySerial and it worked like a charm. Just wanted to note that after Ctrl+A, Z in minicom, there is an option to write file to the serial port, or write to a file from the port. Those files could be read with tail -f.
    – Vorac
    Sep 18, 2013 at 8:25
1

Warren Young's answer above is correct, however in the interest of lazyness there's a better answer:

cat $file > /dev/ttyUSB0 works as well. As for the tc(g|s)etattr calls, those are handled by the stty command (e.g. stty -F /dev/ttyUSB0 9600)!

1
  • 1
    I just want to note that this doesn't always work! On my machine, trying to both cat and echo at the same time leads to the USB port being flooded with data!
    – iFreilicht
    Apr 8, 2021 at 8:59
1

Use miniterm.py! It's included with the pySerial module that Warren mentioned in his answer.

The output is always on stdout, so you can pipe it, tee it, grep it, awk it, sed it, print it, zip, unzip it.

Install it with pip for your user, and the miniterm.py should be placed in your ~/.local/bin, so you can just execute it:

$ pip3 install pySerial
$ miniterm.py /dev/ttyUSB0
--- Miniterm on /dev/ttyUSB0  9600,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---

If that doesn't work you can also just run the serial module:

$ python3 -m serial /dev/ttyUSB0
--- Miniterm on /dev/ttyUSB0  9600,8,N,1 ---
--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H ---

Remember to use Ctrl+] to quit!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.