Reputation: 13
I am trying to write commands and read responses to/from a serial port (RS232) on a laboratory pump. I have the pump connected to my PC via a USB-to-serial adaptor, and have been able to send commands fine but I'm struggling to read all lines of the pump's response.
I'm sending the command
ser.write(b'ver\r\n')
to which the pump should respond with its firmware version number, followed by a character representing its status (stopped (:), pumping (>), stalled(*)); I'm reading the response in PySerial with the following code:
time.sleep(1)
while True:
line = ser.readline()
print(line)
I'm monitoring the TX pin on the pump's port using an oscilloscope and using its automatic decoding feature; the scope shows this as the response:
CR LF SP SP 1 1 . 0 6 2 CR LF CR LF :
whereas my python script reads the response as
b'\r\n'
b' 11.062\r\n'
b'\r\n'
Clearly the colon at the end is not being read as it comes after the CR LF characters, and I believe that the pump is sending the EoL characters at the beginning of the line - i.e., what I should be reading is
b'\r\n 11.062'
b'\r\n'
b'\r\n:'
Is there a way to tell PySerial to look for EoL characters at the beginning lines, or to get it to continue reading the port even when no EoL characters are present?
I'm not particularly familiar with serial communication, so please forgive any naïvety.
See below for my full script:
import time
import serial
import traceback
print('Libraries imported')
# configure the serial connections
ser = serial.Serial(
port='COM12',
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.EIGHTBITS
)
ser.isOpen()
try:
print('Querying...')
#\r is the CR (carriage return) character
#\n is the LF (line feed) character
#'b' makes it a bytearray (required type of serial.Serial.write() argument)
ser.write(b'ver\r\n')
time.sleep(1)
while True:
line = ser.readline()
print(line)
except Exception:
traceback.print_exc()
while True:
entry = input("Press x to close terminal: ")
if entry == 'x':
break
else:
continue
Upvotes: 0
Views: 39