Piping data from pyboard to serial to cat

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
mathieu
Posts: 88
Joined: Fri Nov 10, 2017 9:57 pm

Piping data from pyboard to serial to cat

Post by mathieu » Wed Apr 06, 2022 4:46 pm

I'm writing strings using pyb.USB_VCP() while my pyboard is connected to a laptop running macOS. I would expect that the following code would read this data and allow me to pipe it to another process, but no luck (cat remains silent). Am I doing something wrong? I'm looking for the very simplest method to pipe sensor readings from a pyboard to polt.

Code: Select all

# on the pyboard:
import pyb, time
vcp = pyb.USB_VCP()
while True:
    vcp.write('3.1416\n')
    time.sleep(1)

# on the laptop (there is only one usbmodem* port):
cat /dev/tty.usbmodem2052387C374E1

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Piping data from pyboard to serial to cat

Post by dhylands » Wed Apr 06, 2022 9:49 pm

Try using /dev/cu.usbmodem2052387C374E1 instead of /dev/tty.usbmodem2052387C374E1

The /dev/tty.usbmodem* port is designed for incoming serial connections when connected to an old-fashioned modem and will wait until the DCD signal is asserted before the open proceeds.

The /dev/cu.usbmodem* is designed for outgoing serial connections and doesn't wait for the DCD signal.

Some USB-to-serial adapters will assert DCD and some won't, so your best bet is to use /dev/cu.*

If you were using your own program (instead of cat) you can open the serial port in non-blocking mode and then turn blocking mode back on, something like this code does: https://github.com/dhylands/projects/bl ... #L213-L231 and then you can use /dev/tty.usbmodem* or /dev/cu.usbmodem* and it won't matter.

Most terminal programs (like minicom, picocom, putty, etc) will do the same non-blocking open dance.

User avatar
mathieu
Posts: 88
Joined: Fri Nov 10, 2017 9:57 pm

Re: Piping data from pyboard to serial to cat

Post by mathieu » Thu Apr 07, 2022 7:06 am

That did the trick, and I learned something new. Thanks!

PSA: a combination of cat+polt makes it extremely easy to monitor various sensors on the fly, be it in the lab or in the field, and offers a lot of flexibility (cf polt docs). I wonder if this can somehow be done using Pythonista on iOS...

Thanks again.

User avatar
scruss
Posts: 360
Joined: Sat Aug 12, 2017 2:27 pm
Location: Toronto, Canada
Contact:

Re: Piping data from pyboard to serial to cat

Post by scruss » Thu Apr 07, 2022 9:50 am

You might be able to feed the data directly to polt with something like:

Code: Select all

polt < /dev/cu.usbmodem2052387C374E1

Post Reply