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
#16
Deanhystad,

With your help and others, I was able to come up with something that works consistently well.

Thanks to all!

Seems there were several issues. One was timing. The timeout parameter in the open statement needed to be long enough for the MP3 board to compile the answer to the query. Another issue was timing between the request and the response. 3rd was to not ask for the read till the inWaiting was equal to the length of the response.

Also, I had trouble when I kept the serial port open through the whole program. Opening and closing probably causes a bunch of overhead but worked best for me. I tried only opening once in the program after I was successful opening and closing many times and it failed with time out issues.

Please consider this issue closed.

Here is the resultant code:

"""Sends a  message via serial & receives back information via serial."""
 
import serial
import time
to = 30    #<-- timeout value 
PLAYING_DONE = bytearray([170, 1, 1, 0, 172])

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  bytearray"""
    write_command(port, [1, 0])
    time.sleep(0.5)
    if port.inWaiting() > 4:
         return port.read(5)

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

ser = serial.Serial(
    '/dev/ttyS0',   # Serial port on my computer
    baudrate=9600,
    parity=serial.PARITY_NONE,
    stopbits=serial.STOPBITS_ONE,
    bytesize=serial.EIGHTBITS,
    timeout=to)     #  timeout for read
 
play_track(ser, [2, 0, 9])  # Track info?
ser.close()

playing = None

while playing != PLAYING_DONE:
    ser = serial.Serial(
        '/dev/ttyS0',   # Serial port on my computer
        baudrate=9600,
        parity=serial.PARITY_NONE,
        stopbits=serial.STOPBITS_ONE,
        bytesize=serial.EIGHTBITS,
        timeout=to)  # timeout for read

    playing = read_status(ser)
#    print(f'Return = {playing}')
    ser.close()


ser.close()
print("Done and closed")
Reply


Messages In This Thread
RE: UART Serial Read & Write to MP3 Player Doesn't Work - by bill_z - Jul-17-2021, 04:19 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  UART write binary code trix 3 267 Apr-28-2024, 04:57 PM
Last Post: deanhystad
  Last record in file doesn't write to newline gonksoup 3 473 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 655 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,530 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 650 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 4,425 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 978 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  How do I read and write a binary file in Python? blackears 6 6,902 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Python Serial: How to read the complete line to insert to MySQL? sylar 1 852 Mar-21-2023, 10:06 PM
Last Post: deanhystad
  Read text file, modify it then write back Pavel_47 5 1,672 Feb-18-2023, 02:49 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