Page 1 of 1

UART won't send all MIDI control messages [solved]

Posted: Sat Mar 06, 2021 7:21 am
by Grumpy_Mike
I am trying to send some MIDI messages through the UART. I am finding that every message above 0xB0 produces nothing on the output as seen by the MIDI monitor app on a Mac.

This is the code I am using:-

Code: Select all

"""
MicroPython code to write from UART1 on the Pico
sends some MIDI messages
"""
from machine import UART
from machine import Pin

ser = UART(1, 31250, tx=Pin(8), rx=None) # no recieve

# send control message - works OK
ser.write(chr(0xB0))
ser.write(chr(0x00)) # write bank select msb
ser.write(chr(0x00)) # set to a zero

# send a program change message - produces nothing
ser.write(chr(0xC0))
ser.write(chr(27))

# ask for retune - produces nothing
ser.write(chr(0xF8))
Is there something wrong with me using chr(), if so how do you send defined bytes?
Other messages like note on and note off work this way.

Re: UART won't send all MIDI control messages

Posted: Sat Mar 06, 2021 7:24 am
by Roberthh
why not using ser.write(b'\xC0'))? That avoids problems with encoding.

Re: UART won't send all MIDI control messages

Posted: Sat Mar 06, 2021 7:35 am
by Grumpy_Mike
Roberthh wrote:
Sat Mar 06, 2021 7:24 am
why not using ser.write(b'\xC0'))? That avoids problems with encoding.
So it does thanks. I am not too familiar with python sending bytes that has always worked on "grown up" python.

How would you cope with oring a variable into that?
like

Code: Select all

ser.write(chr(0xC0 | channel ))
Thanks again.

Re: UART won't send all MIDI control messages

Posted: Sat Mar 06, 2021 9:03 am
by Roberthh
In that case it's better to define a bytearray for the command, in which you can assign the values as integers

cmd = bytearray(cmdsize)
cmd[0] = 0xd0 | channel # set the first byte
# set more bytes as required
uart.write(cmd)

Re: UART won't send all MIDI control messages [solved]

Posted: Sat Mar 06, 2021 10:12 am
by Grumpy_Mike
Excellent, works like a charm.
Many thanks. :)