Jul-10-2021, 08:55 PM
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).
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) # passThese 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