Jul-12-2021, 02:59 PM
(This post was last modified: Jul-12-2021, 02:59 PM by deanhystad.)
I could not follow the link, so this advise of the uninformed guessing type. I think 170 (0xAA) is a start of packet marker and that the packet ends with some sort of checksum. I am accepting that the play track command is 7 and the read status command is 1. I also accept that the read status command returns 3 bytes.
Instead of looping and peeking at the read buffer I would specify a read timeout when opening the serial port.
Instead of looping and peeking at the read buffer I would specify a read timeout when opening the serial port.
"""Sends a message via serial & receives back information via serial.""" import serial import time PLAYING_DONE = bytearray([0, 0, 0]) def write_command(port, command): """Write command to serial port""" command = [170].extend(command) # Command packet starts with 0xAA command.append(sum(command) % 256) # Append checksum port.write(bytearray(command)) def read_status(port): """Read status? Returns 3L bytearray""" write_command(port, [1, 0]) return port.read(3) def play_track(port, track_info): """Start playing track. Track is??""" write_command(port, [7].extend(track_info)) ser = serial.Serial( '/dev/ttyS0', baudrate=9600, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, timeout=1) # 1 second timeout for read play_track(ser, [2, 0, 4]) # Track info? playing = None while playing != PLAYING_DONE: time.sleep(1) playing = read_status(ser) print(f'Status = f{playing}') ser.close()