Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

UART on grand central
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

UART on grand central

by jordan314 on Thu Jun 25, 2020 5:10 pm

Hi,
I'm having trouble reading serial in both circuitpython and arduino coming from a MIDI DIN breakout board.
In circuit python, I have the following code:
Code: Select all | TOGGLE FULL SIZE
import board
import busio
import digitalio

led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT

uart = busio.UART(board.TX, board.RX, baudrate=31250, timeout=10, receiver_buffer_size=64)
while True:
    data = uart.read(3)  # read up to 3 bytes

    if data is not None:
        print(data[0], data[1], data[2])
        if data[0] == 144:
            print("Note on")


I do get some "note on" messages but they're about once every five seconds. The 144 is not always byte 0. When I print the raw data I also see a lot of gibberish such as:
b'\xfe\xfe\xfe'
b'\xfe\xfe\x90'
b'E>@'
b'iE\x00'
b'>d<'
b'R@\x00'
b';c<'
b'\x00>\x00'
b'<D;'
b'\x00>V'
b'@u<'
b'\x00>\x00'
b'E_@'
b'\x00E\x00'
b'\xfe\xfe\xfe'
b'\xfe\xfe\xfe'
b'\x90@i'

The x90 is the note on, but I'm seeing byte-order marks and other stuff that's confusing my program. I've tried adjusting the timeout but I still only get occasional notes every few seconds.

Then I tried arduino, and running a sketch that I've gotten working on a arduino uno:
https://github.com/ubldit/MIDI_Breakout ... xample.ino
It takes MIDI input and plays the equivalent frequency on a piezo buzzer.
I hooked up the grand central in the same way:
Midi Breakout VCC: Grand Central 3.3V
Midi Breakout Ground: Grand Central Ground
Midi Breakout RX (actually TX, I've tried both): Grand Central RX
Grand Central Digital 7: Piezo +
Grand Central Ground: Piezo -

When I load the same sketch, it does its initial test beep, but then doesn't respond to the keyboard.
I'm wondering if it's Serial.read() -- I read that in Adafruit M0/M4 Core Serial maps to SerialUSB. How do I read from the hardware serial then?
Thanks--
jordan314
 
Posts: 49
Joined: Thu Jan 10, 2013 8:11 pm

Re: UART on grand central

by tomjennings on Tue Jul 07, 2020 7:40 pm

I'm not familiar with circuitPython, but I am familiar with serial devices, and Arduino's Serial object...

UARTs are asynchronous devices -- meaning not-synchronous, not timely. Characters may appear at any time, or not at all. All in a row, or spaced out in time, randomly or evenly. So read() by itself usually doesn't do what you want...

in pseudo-code, something like

Code: Select all | TOGGLE FULL SIZE
while true
    if uart.available() then
      data= uart.read(1);
      if (data == 144) then
           blah blah blah
      fi
    fi
end


You have to pop up a level of abstraction, and plan out your data-fetching. Then you can accommodate stuff like

* data never arrives
* other things happening (switch press, etc)
* receiving unexpected data

tomjennings
 
Posts: 78
Joined: Thu Aug 17, 2006 1:21 am

Please be positive and constructive with your questions and comments.