Reading Serial Data Using a Raspberry Pi Pico

678 Views Asked by At

I have a Spektrum Radio transmitter, along with its receiver. What I am currently trying to do is by using microPython and a Raspberry Pi Pico, I want to read the data coming into the receiver, and convert that into servo commands. All I know is that the protocol used by the transmitter/receiver is DSMX. How can I go about doing this? I only need to receive, I don't need to transfer any data back from the Raspberry Pi Pico.

I'm using Thonny, and all I've done is try to use the UART module and ustruct module and create a variable using that

uart = UART(1, baudrate = 115200)
data = uart.read()
header,id,data,error_checking,trailer = ustruct.unpack('>BBHHB',data)

When trying to run this, I get thrown the error

TypeError: object with buffer protocol required

I didn't expect anything, as I don't really know what I'm doing. Any help would be really appreciated.

1

There are 1 best solutions below

0
larsks On

You're getting that TypeError exception because your call to uart.read() is returning None (meaning that there was no data available on the serial port). I don't know anything about the DSMX protocol, but to avoid that error in your code you probably want something like:

format = 'BBHHB'
required_size = ustruct.calcsize(format)
if uart.any() >= required_size:
    data = uart.read(required_size)
    header,id,data,error_checking,trailer = ustruct.unpack(f'>{format}',data)

...and the above probably needs to live in some sort of loop.