python - pySerial reading data from AT commands -
i'm having problem reading response rs232 obd2 interface via pyserial. code enters data, can see direct parallel terminal screen, fails read , print response, regardless of response.
right code not capable of printing response in neither versions of python. code looks :
from serial import * # tried using /from serial import serial import time ser = serial("/dev/rfcomm1", 38400, timeout=1) #print ('starting up, formatting responses') #ser.write("atz\r"), #ser.write("atsp0\r"), #ser.write("ats1\r"), #ser.write("atl1\r"), #ser.write("ath1\r"), #ser.write("atf1\r") #time.sleep(1) #print ('we have lift-off !') if ser.inwaiting() > 0: ser.flushinput() #ser.timeout = 1. time.sleep(1) #print (raw_data) ser.write("at rv\r") #the response should 13.5v, nil ser.timeout = 1. msg = ser.read(size=1024) print msg ser.close() i left @ rv command because while i'm working on sent text formatting commands ease job. right when send it gives me blank line (although terminal running on same machine displays desired output)
there no errors in code, , commands go through , responded interface, , can see in live term, nil appears when running python code. should ?
you should read after writing, not before.
# before writing anything, ensure there nil in buffer if ser.inwaiting() > 0: ser.flushinput() # set timeout reasonable, e.g., 1 s ser.timeout = 1. # send commands: ser.write("atz\r") # ... # read response, guess length more message msg = ser.read(1024) print msg # send more commands # read more responses # ... the point here there no way know when response has been received. code waits 1 sec after each command sent, unless more 1024 bytes arrive during time. there more clever algorithms, let's seek one, first.
if want more complicated serial line, have @ pexpect module.
some thoughts debugging python serial problems
serial communication problems bit sticky solve. pyserial reliable library, different platforms have different types of serial api, there lot of details. things have not become easier removal of physical serial ports, usb converters bring layer game. bluetooth converters worse.
the best way debug physical layer have monitor hardware 2 serial ports tapped serial lines. kind of sniffer helps isolate problem either end of connection. unfortunately, such sniffers @ hand when needed.
the next best thing short rd , td (rxd, txd) pins of serial line. way info echoed. if info received sent, physical connection good. 1 thing take care handshaking. if not know doing, disable flow command (xon/xoff, rts/cts, dtr/dsr. pyserial disables these if otherwise instructed.
in case of question above physical connection ok, piece of software demonstrates info sent , understood other device. (seeing sent not prove anything, info not go through physical layer, seeing produced device received proves physical connection ok.)
now know info comes operating system, pyserial not see it. or our code still somehow bad (no, shouldn't, but...)
let suspect own own code , seek else's code. can run command prompt:
python -m serial.tools.miniterm /dev/rfcomm1 38400 now have terminal can used manually send/receive info form other party. if behaviour can repeated (sends ok, info received system, not shown on terminal) this, problem not in our code.
the next step try:
sudo python -m serial.tools.miniterm /dev/rfcomm1 38400 in principle access right problems lead situations can receive not send. not harm test this, because odd rights cause odd problems.
pyserialhas handy function readline should read 1 line @ time serial line. wanted. however, in specific case lines seem end \r instead of \n. same may repeated elsewhere in code, special info special care needed. (the simple "read timeout" safe slow in sense.) discussed in: pyserial 2.6: specify end-of-line in readline()
the same issue plagues terminal programs. pyserial miniterm, see documentation (command-line alternative --cr).
if there timeouts, can , should made longer debugging purposes. one-second timeout may changed ten-second timeout create sure other device has ample time answer.
python serial-port raspberry-pi pyserial obd-ii
No comments:
Post a Comment