Python Forum
UART Serial Read & Write to MP3 Player Doesn't Work
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
UART Serial Read & Write to MP3 Player Doesn't Work
#13
The read_status command has to read 5 bytes to get 3 status bytes. In the code below I added a check for the start of message character (often referred to as a sentinel) and a checsum check.
"""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)
    port.write(bytearray(command))

def read_status(port):
    """Read status?  Returns 3L bytearray"""
    write_command(port, [1, 0])
    bytes = port.read(5)
    print('Reply ', bytes)
    if bytes is None:
        raise ValueError('Read timeout')
    if bytes[0] != 0xAA:
        raise ValueError('Serial sentinel error')
    if bytes[4] != (sum(bytes[:-1]) % 256):
        raise ValueError('Serial read checksum error')
    return bytes[1:-1]

def play_track(port, track_info):
    """Start playing track.  Track is??"""
    write_command(port, [7] + track_info)

with serial.Serial(
        'COM3:',
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=1) as ser:
    # Start of serial context
    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}')
    # end of serial context
print("I am done")
To prevent leaving the serial port open the code uses a context manager. "with serial.Serial(...)" creates a context for the serial port. When the program exits this context (the indented instructions below the "with") the serial port is automatically closed. It doesn't matter if the context exits because the program is done using the serial port or if there is an exception.
Reply


Messages In This Thread
RE: UART Serial Read & Write to MP3 Player Doesn't Work - by deanhystad - Jul-13-2021, 02:35 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extending list doesn't work as expected mmhmjanssen 2 253 May-09-2024, 05:39 PM
Last Post: Pedroski55
  UART write binary code trix 3 395 Apr-28-2024, 04:57 PM
Last Post: deanhystad
  Last record in file doesn't write to newline gonksoup 3 525 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 708 Dec-28-2023, 05:14 PM
Last Post: trix
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,674 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 683 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 5,115 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,021 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  How do I read and write a binary file in Python? blackears 6 7,541 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 873 Mar-21-2023, 10:06 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020