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
#4
Working more and more on this code, I was able to fix the looping issue (thanks Deanhystad), but still having an issue of receiving back from the serial port.

I'm was getting inconsistent returns from the serial port each time I run the program (see output).

I noticed that I do not get return information each time the program does a ser.read(ser.inWaiting(), only the first time even though I think I am doing repeated ser.write. Also ser.isOpen value changes between program runs. I don't understand this.

If the program receives RX b'\x01\x01\x01\xad' on the first read, then the program ends as expected, but, if the first read is something like RX b'\x1exxxx' then it won't end well.

Please help.

Here is my code:

#// 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,4,180])
state_req = bytearray([170,1,0,171])
state_ret = bytearray([0,0,0,0,0])

Zeros = bytearray([0,0,0])
Playing = True
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)
 
while Playing:
	print ("Loop 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)
		if (state_ret == Zeros):
			Playing = False 

print ("Done")
ser.close()
Output:
pi@raspberrypi:~ $ sudo python3 SerialTest2.py True Script Started bytearray(b'\xaa\x07\x02\x00\x04\xb7') Loop Query Requested ---> True ReadLine 4 ===> True RX b'\x01\x01\x01\xad' Loop Query Requested ---> True Loop Query Requested ---> True Loop Query Requested ---> True ReadLine 3 ===> True RX b'\x00\x00\x00' Done pi@raspberrypi:~ $ sudo python3 SerialTest2.py True Script Started bytearray(b'\xaa\x07\x02\x00\x04\xb7') Loop Query Requested ---> True ReadLine 5 ===> True RX b'\x1exxxx' Loop Query Requested ---> True Loop Query Requested ---> True Loop Query Requested ---> True Loop Query Requested ---> True ^CTraceback (most recent call last): File "SerialTest2.py", line 30, in <module> time.sleep(1) KeyboardInterrupt pi@raspberrypi:~ $
Reply


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

Possibly Related Threads…
Thread Author Replies Views Last Post
  Delete file with read-only permission, but write permission to parent folder cubei 6 22,371 Jun-01-2024, 07:22 AM
Last Post: Eleanorreo
  Extending list doesn't work as expected mmhmjanssen 2 350 May-09-2024, 05:39 PM
Last Post: Pedroski55
  UART write binary code trix 3 477 Apr-28-2024, 04:57 PM
Last Post: deanhystad
  Last record in file doesn't write to newline gonksoup 3 592 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  UART & I2C slow down a loop trix 4 771 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,815 Nov-09-2023, 10:56 AM
Last Post: mg24
Question Special Characters read-write Prisonfeed 1 721 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  pyserial/serial "has no attribute 'Serial' " gowb0w 9 5,704 Aug-24-2023, 07:56 AM
Last Post: gowb0w
  Why doesn't calling a parent constructor work with arbitrary keyword arguments? PurposefulCoder 4 1,078 Jun-24-2023, 02:14 PM
Last Post: deanhystad
  How do I read and write a binary file in Python? blackears 6 8,138 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

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