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

RP2040 based microcontroller boards running MicroPython.
Target audience: MicroPython users with an RP2040 boards.
This does not include conventional Linux-based Raspberry Pi boards.
Post Reply
User avatar
Grumpy_Mike
Posts: 8
Joined: Sun Feb 14, 2021 8:04 pm

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

Post by Grumpy_Mike » Sat Mar 06, 2021 7:21 am

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.
Last edited by Grumpy_Mike on Sat Mar 06, 2021 10:11 am, edited 3 times in total.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART won't send all MIDI control messages

Post by Roberthh » Sat Mar 06, 2021 7:24 am

why not using ser.write(b'\xC0'))? That avoids problems with encoding.

User avatar
Grumpy_Mike
Posts: 8
Joined: Sun Feb 14, 2021 8:04 pm

Re: UART won't send all MIDI control messages

Post by Grumpy_Mike » Sat Mar 06, 2021 7:35 am

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.

User avatar
Roberthh
Posts: 3668
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: UART won't send all MIDI control messages

Post by Roberthh » Sat Mar 06, 2021 9:03 am

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)

User avatar
Grumpy_Mike
Posts: 8
Joined: Sun Feb 14, 2021 8:04 pm

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

Post by Grumpy_Mike » Sat Mar 06, 2021 10:12 am

Excellent, works like a charm.
Many thanks. :)

Post Reply