0

Serial comm over USB with CPX
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Serial comm over USB with CPX

by adavid7 on Wed Feb 03, 2021 3:41 pm

Is it possible to use the Serial extension in MakeCode beta to communicate with a program running on the host computer over the USB cable connected to the CPX for power and downloads?

Or does Serial only work with a separate USB data cable connected to RX/TX pads (A6/A7)?

All of the tutorials I perused only show the connection being made with a USB serial converter cable or board.

Thanks.

adavid7
 
Posts: 68
Joined: Fri Jul 04, 2014 7:32 pm

Re: Serial comm over USB with CPX

by mikeysklar on Thu Feb 04, 2021 5:47 pm

@adavid7,

I'm under the impression you can use the "console" function to write output over the USB connected CPX to your PC.

The Adafruit Circuit Playground Express can read data from and write data to another computer or device with a serial connection using USB. To use serial communication between your board and MakeCode, you need the MakeCode for Adafruit app for Windows 10 and a USB cable.

You can also write data to an output log with the console functions without having to use a serial connection


https://makecode.adafruit.com/reference/console

Screen Shot 2021-02-04 at 1.44.07 PM.png
Screen Shot 2021-02-04 at 1.44.07 PM.png (87.83 KiB) Viewed 398 times

mikeysklar
 
Posts: 4976
Joined: Mon Aug 01, 2016 8:10 pm

Re: Serial comm over USB with CPX

by adavid7 on Fri Feb 05, 2021 1:50 pm

Right. Yes, with the Chrome web USB feature that allows you to pair the CPX with MakeCode to automatically download code to the board, you can also have MakeCode receive the console log messages from the board and display them in the browser.

OK, so let me reframe my question:

Has anyone used a program on a Mac other than MakeCode in the browser to receive the console messages?

For instance, this tutorial https://learn.adafruit.com/welcome-to-circuitpython/advanced-serial-console-on-mac-and-linux talks about using the "screen" utility to do that, but I have been unable to get that to work.

Ultimately, I would like to use https://editor.p5js.org to interact with the CPX using its serial library.

adavid7
 
Posts: 68
Joined: Fri Jul 04, 2014 7:32 pm

Re: Serial comm over USB with CPX

by mikeysklar on Mon Feb 08, 2021 1:21 pm

@adavid7,

I use the screen utility to connect to CircuitPython programs quite a bit from my mac.

Can you show me the output of the following when your CPX is plugged into your machine via USB:

Code: Select all | TOGGLE FULL SIZE
ls -lrt /dev/tty.usb*


Are you using the serial write command to output your data to the console?

https://maker.makecode.com/reference/serial

mikeysklar
 
Posts: 4976
Joined: Mon Aug 01, 2016 8:10 pm

Re: Serial comm over USB with CPX

by adavid7 on Mon Feb 08, 2021 7:35 pm

Even before trying any code, here is the serial port info:

With the CPX in download mode (all NeoPixels green after a click of the reset button):

Code: Select all | TOGGLE FULL SIZE
andy:~$ ls -lrt /dev/tty.usb*
crw-rw-rw-  1 root  wheel   20,   4 Feb  8 15:14 /dev/tty.usbmodem40131101

But when an app is running (after another click of the reset button), no device is online:

Code: Select all | TOGGLE FULL SIZE
andy:~$ ls -lrt /dev/tty.usb*
ls: /dev/tty.usb*: No such file or directory

This is with just the USB cable plugged in to the CPX (the standard one for power and downloading).

I have been able to use the screen utility, but only with the USB to Serial cable: https://www.adafruit.com/product/954, not over the regular USB cable. Here is code that does that and works with screen: https://makecode.com/_EEF3yDT5d9j7. If that cable is plugged in, here is its port:

Code: Select all | TOGGLE FULL SIZE
andy:~$ ls -lrt /dev/tty.usb*
crw-rw-rw-  1 root  wheel   20,   4 Feb  8 15:28 /dev/tty.usbserial-4013130

and then screen displays all of the console log messages issued from MakeCode.

So, my question is whether it is actually possible to connect to a serial program on the Mac over the regular USB cable?

Thanks!

adavid7
 
Posts: 68
Joined: Fri Jul 04, 2014 7:32 pm

Re: Serial comm over USB with CPX

by adavid7 on Mon Feb 08, 2021 8:41 pm

PS: I'm not using serial write, but "serial attach to console"

adavid7
 
Posts: 68
Joined: Fri Jul 04, 2014 7:32 pm

Re: Serial comm over USB with CPX

by mikeysklar on Tue Feb 09, 2021 1:13 pm

@adavid7,

Thanks for confirming that screen is working fine with the USB console cable breakout option. Also the /dev/tty.usb* is present unless in bootloader mode.

I do see people using the micro:bit to get a real serial console with OS/X and accessing it via screen. They are using "serial write". Do you think if you try serial write that the /dev/tty.usbmodem* device might reappear as the code is executing (rather than just in bootloader mode)? Seems worth a try since it works on a related piece of hardware.

https://support.microbit.org/support/so ... a-computer

mikeysklar
 
Posts: 4976
Joined: Mon Aug 01, 2016 8:10 pm

Re: Serial comm over USB with CPX

by adavid7 on Tue Feb 09, 2021 2:08 pm

I'll give it a try, thanks.

adavid7
 
Posts: 68
Joined: Fri Jul 04, 2014 7:32 pm

Re: Serial comm over USB with CPX

by adavid7 on Thu Feb 18, 2021 6:40 pm

After an enormous amount of time trying and testing many options for serial comm using MakeCode, here is my conclusion. I am wondering if you might be able to confirm this diagnosis, @mikeysklar?

While I can successfully use the serial extension in MakeCode with the separate USB to TTL Serial Cable (https://www.adafruit.com/product/954) connected to the CPX RX/TX pins (A4/A5), I have completely failed at doing that same operation over the normal USB cable connected to the laptop for power and downloading.

I can do this exact operation in the Arduino IDE with the CPX over the host USB connection. In the Arduino IDE, however, you must close the serial monitor window in order to do this. Otherwise the host computer "owns" that serial port and the host app that tries to read/write serial data (say p5.js, or even a simple app like the Unix "Screen" utility in a macOS Terminal shell) is unable to connect because the IDE has that serial port attached to it. Once you close the Serial Monitor window, the port is released, and another app is free to use it.

My hypothesis is that in MakeCode, there is no way to have that browser app release the ownership (attachment) of the serial port. I believe this is because it needs to retain the connection so that when the CPX is paired with MakeCode using the WebUSB feature of Chrome, it can show the "Console Log DEVICE" in addition to the "Console Log SIMULATOR."

I don't know enough about browser code dev to know how to find out what serial ports are "owned" by a browser tab, but if there is a way to do that, I could likely verify my hypothesis.

If there is no way to disable the "Console Log DEVICE" feature of MakeCode, and thus have the browser release the serial port connected to the CPX, then I do not think that serial comm over the regular USB cable is possible.

Does that make sense? Can you confirm that?

Thanks,

.Andy

adavid7
 
Posts: 68
Joined: Fri Jul 04, 2014 7:32 pm

Re: Serial comm over USB with CPX

by mikeysklar on Fri Feb 19, 2021 2:56 pm

Andy,

Your summary seems accurate to me.

Perhaps this is worth creating an issue for and let the experts chime in on options to release the console.

https://github.com/microsoft/pxt-adafruit/issues

mikeysklar
 
Posts: 4976
Joined: Mon Aug 01, 2016 8:10 pm

Re: Serial comm over USB with CPX

by adavid7 on Fri Feb 19, 2021 7:06 pm

Will do.

adavid7
 
Posts: 68
Joined: Fri Jul 04, 2014 7:32 pm

Please be positive and constructive with your questions and comments.