Using a UART to recieve MIDI

CircuitPython on hardware including Adafruit's boards, and CircuitPython libraries using Blinka on host computers.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
Grumpy_Mike
 
Posts: 31
Joined: Thu Mar 22, 2018 2:46 am

Using a UART to recieve MIDI

Post by Grumpy_Mike »

I have serial MIDI coming over a 5 pin cable into an RP2040 from an other device. I have the optically isolated interface circuitry and I want to see the incoming messages all of which will be restricted to note on and note off 3 byte MIDI messages.

However it seems like I am not picking up all the bytes I am expecting to.
I have defined the UART on the RX1 input of the RP2040 by:-

Code: Select all

uart = busio.UART(None, board.GP5, bits=8, parity=None, stop=1, timeout=0.03, baudrate=31250) # MIDI Baud rate
Then I am using this code to check what is coming in.

Code: Select all

def updateMIDIin():
    data = uart.read(3)  # read up to 32 bytes
    if data is not None:
        print("got data ")
        print(data)
I am sending 0x90, 0x34, 0x60 - Note on
followed by 0x80, 0x34, 0x60 - Note off

But what I am receiving is:-
got data
b'\x904`'
got data
b'\x804`'

I have played about with the time out but still not getting all the bytes I expect. Reading six rather than three bytes just repeats the same sort of thing only on the same line.

I did try the code:-

Code: Select all

        data_string = ''.join([chr(0) for b in data])
        print(data_string, end="")
But that just prints blank lines.

Has anyone any idea where I am going wrong?
Thanks

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: Using a UART to recieve MIDI

Post by danhalbert »

You are getting three bytes. When Python prints a bytestring, it prints the regular printing characters as themselves, and prints the rest with \xnn escapes. For example, it is printing:

Code: Select all

b'\x904`'
This is three bytes: \x90, 4, and ` (backtick). \x90 has no printed representation so it prints as a hex escape, but the other bytes are regular printing characters.
To see all the bytes as hex, do something like:

Code: Select all

[hex(b) for b in data]
which will produce

Code: Select all

['0x90', '0x34', '0x60']
Or, if you want a single string, something like:

Code: Select all

''.join(f"{hex(b)} " for b in data)
producing

Code: Select all

'0x90 0x34 0x60 '

User avatar
danhalbert
 
Posts: 4654
Joined: Tue Aug 08, 2017 12:37 pm

Re: Using a UART to recieve MIDI

Post by danhalbert »

Another way that is available on all but the smallest CircuitPython builds is binascii.hexlify, e.g.:

Code: Select all

>>> import binascii
>>> data = b'\x904`'
>>> binascii.hexlify(data)
b'903460'

User avatar
Grumpy_Mike
 
Posts: 31
Joined: Thu Mar 22, 2018 2:46 am

Re: Using a UART to recieve MIDI

Post by Grumpy_Mike »

Thanks a lot. Yes the

Code: Select all

[hex(b) for b in data]
Worked and solved my problem.

Cheers

Locked
Please be positive and constructive with your questions and comments.

Return to “Adafruit CircuitPython”