Jul-12-2021, 08:14 PM
(This post was last modified: Jul-12-2021, 08:14 PM by deanhystad.)
Why do you think my code throws the error? Do you know what a "TypeError" is? Why is this error raised when "command" is a NoneType? How do you think a NoneType got passed to "write_command()"?
All the above questions are things a new Python programmer should be able to answer with a little bit of digging.
A few hints. The error has nothing to do with the while loop. Look at what the list.extend() command takes as an argument and what it returns.
Corrected code is below. There was also an error in the print command.
All the above questions are things a new Python programmer should be able to answer with a little bit of digging.
A few hints. The error has nothing to do with the while loop. Look at what the list.extend() command takes as an argument and what it returns.
Corrected code is below. There was also an error in the print command.
"""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] + command # Command packet starts with 0xAA command.append(sum(command) % 256) # Append checksum print('Command = ', command) # For debugging purposes 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] + track_info) ser = serial.Serial( 'COM3:', # Serial port on my computer 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 = {playing}') ser.close()