Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Receive commands from the computer via USB
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Receive commands from the computer via USB

by Nahan on Fri Jan 18, 2019 6:32 pm

I recently purchased a Trinket M0. From my understanding it comes equipped with circuitpython. I currently have code in arduino to retrieve commands sent from the computer as follows:
Code: Select all | TOGGLE FULL SIZE
if (Serial.available() > 1) {  // when serialport is attached and sending values
    byte input1[2];                 
    Serial.readBytes(input1, 2);  // 2 values received through serial port
    x = int(input1[0]);   // stores values
    y = int(input1[1]); 


I was curious what command circuitpython has that is similar to arduinos Serial.readBytes(), Serial.write()... exc
I thought I read something online that said this isn't possible with circuitpython? I wanted to check here though because I feel like that is such a basic function to leave out.

Thanks!

Nahan
 
Posts: 2
Joined: Fri Jan 18, 2019 6:23 pm

Re: Receive commands from the computer via USB

by tannewt on Tue Jan 22, 2019 2:44 pm

It's trickier in CircuitPython because the serial connection is used by CircuitPython as well. To output you simply `print()`. To read do `input()` but beware that this can't handle all byte values. CircuitPython will interpret ctrl-C and ctrl-D itself to allow you to cancel a running script. input is also blocking but you can use `supervisor.runtime.serial_bytes_available` to check to see if there is buffered input. Make sure you are using the latest version of CircuitPython to get the supervisor bit. Good luck!

tannewt
 
Posts: 1724
Joined: Thu Oct 06, 2016 8:48 pm

Re: Receive commands from the computer via USB

by kevinjwalters on Sat Jan 26, 2019 6:55 am

Not much beyond what's been discussed here, but there's some example code on similar discussion: C# call circuitpython function.

kevinjwalters
 
Posts: 674
Joined: Sun Oct 01, 2017 3:15 pm

Re: Receive commands from the computer via USB

by Nahan on Wed Jan 30, 2019 7:19 pm

For the mean time I decided to just upload my arduino code to the board while I learn how to use CircuitPython and convert my code over. I followed the steps and installed the adafruit boards and drivers through the Arduino IDE, I was able to upload the "Blink" sketch to the board. However, from that code above, I get the error:

size_t readBytes(char *buffer, size_t length);
^

exit status 1
invalid conversion from 'byte* {aka unsigned char*}' to 'char*' [-fpermissive]

Do you know why that might be? It is all able to compile when arduino boards are selected but not when the Trinket M0 is selected. Does it have to do with the serial connection issue you mentioned?

Nahan
 
Posts: 2
Joined: Fri Jan 18, 2019 6:23 pm

Re: Receive commands from the computer via USB

by tannewt on Sat Feb 02, 2019 4:25 am

I'd suggest reposting the Arduino portion of your question as a new topic. CircuitPython folks may not know Arduino well enough to help.

tannewt
 
Posts: 1724
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.