I have an arduino gathering some data, and it also waits for very simple commands (a single character) on the serial port, and replies back on the serial port with some numbers sandwiched between the command character. As an example, I'd send "A" down the serial port, and it would reply with "A50.45,200.35A".
I've connected it up to the pi, was testing it manually using "screen /dev/ttyUSB0", then wrote a python script to communicate with it. All working fine.
However when I restart the pi (or rather it got restarted because my power cut out), the python script then doesn't work. It seems to send the command ok, but then is just hangs on the serial.read() command. If I quit out of that, run the above screen command all works well, then go back and run my python script and it works!
What is it that the "screen" command is doing, that somehow then enables my python code to work? This is my SendCommand function, it hangs at the first serPort.read(1) line.
Code: Select all
# com is a command
def sendCommand(serPort, com):
serPort.write(bytes([com]))
# wait until "A" is recieved
rec = 0
while rec != com:
bytesReceived = serPort.read(1)
if( len(bytesReceived) == 1 ):
rec = bytesReceived[0]
# now receive all data
rData = bytearray(0)
rec = 0
while rec != com:
bytesReceived = serPort.read(1)
if( len(bytesReceived) == 1 ):
rec = bytesReceived[0]
if( rec != com ):
rData.append(rec)
v = rData.decode()
return v