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
#1
Not only am I a beginner to Python3 but I’m a beginner to reading and writing via serial port on a Raspberry PI (dev/ttyS0). I’m trying to communicate to a MP3 player board that is suppose to use UART Communication Command to play particular MP3s and to return status of the MP3, like if it is playing or finished playing.

The MP3 requires a unique set of values to include a summation byte for it to recognize what to do.

With much trial, error & many Internet searches, I was able to come up with some code that almost works.

It will play the requested MP3 and write the request for status some of the time. When it does return the status, the program doesn’t work like I thought it would when using the try: statement. I wanted it to keep on looping and I would figure out a way to break out when the status was zero. But it doesn’t loop.

Also, after the program ends by it’s self, the next time I try the program, the expected read doesn’t work. But, if I stop the program with a ^C, the next time I run the program, I get expected results in state_ret. I want it to loop till I get a state_ret equal to zero. I think I need to do some sort of reset at the beginning of the program to be sure .

I hope I’m clear about the above and the problems I’m having.

I can use suggestions and pointers on how reading & writing to serial really works, so I can be somewhat proficient and what to change in my program so it will work.

Here is my program (I use NANO as my editor).

#// Sends a  message via serial & receives back information via serial.

import time
import serial

ser = serial.Serial('/dev/ttyS0', baudrate=9600, parity=serial.PARITY_NONE,
	stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)

print (ser.isOpen())

track_req = bytearray([170,7,2,0,20,180])
state_req = bytearray([170,1,0,171])
state_ret = bytearray([0,0,0,0,0])
expected0 = bytearray([170,1,1,0,172])
expected1 = bytearray([170,1,1,1,173])
expected2 = bytearray([170,1,1,2,174])

track_req[5] = track_req[0]+ track_req[1]+ track_req[2]+ track_req[3]+ track_req[4] 

print ("Script Started", track_req)
ser.write(track_req)
time.sleep(2)

try:
	print ("Query Requested")
	print ("--->  ", ser.isOpen())
	ser.write(state_req)
	time.sleep(1)
	if (ser.inWaiting()>0): 
		print ("ReadLine", ser.inWaiting())
		print ("===>  ", ser.isOpen())
		state_ret = ser.read(ser.inWaiting())
		print("RX ", state_ret)

except OSError:
# ignore or log...  Let the loop retry.
	print ("SOError ", OSError)
#	pass
These are the 2 different outputs when I run without a ^c then after a ^c:

Output:
pi@raspberrypi:~ $ sudo python3 SerialTest.py True Script Started bytearray(b'\xaa\x07\x02\x00\x14\xc7') Query Requested ---> True ReadLine 5 ===> True RX b'xxxxx' pi@raspberrypi:~ $ ^C pi@raspberrypi:~ $ sudo python3 SerialTest.py True Script Started bytearray(b'\xaa\x07\x02\x00\x14\xc7') Query Requested ---> True ReadLine 4 ===> True RX b'\x01\x01\x01\xad' pi@raspberrypi:~ $ ^C
Reply


Messages In This Thread
UART Serial Read & Write to MP3 Player Doesn't Work - by bill_z - Jul-10-2021, 08:55 PM

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