How do you use minicom with stdin and stdout?

7,777

Solution 1

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.

Solution 2

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)!

Solution 3

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!

Share:
7,777

Related videos on Youtube

Vorac
Author by

Vorac

Coding is like computer games except sometimes it's useful.

Updated on September 18, 2022

Comments

  • Vorac
    Vorac over 1 year

    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?

    • Admin
      Admin over 10 years
      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.
  • Vorac
    Vorac over 10 years
    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.
  • iFreilicht
    iFreilicht about 3 years
    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!