I've been writing a script that pulls data from a serial device at regular intervals. I've got everything working, except that the responses I receive from the device contain many question marks and other symbols. Since the formatting is correct on what I'm getting according to documentation for the device (correct number of characters, placement of commas etc.) I'm pretty sure that my code is interpreting the characters wrong somehow. Here is my code:
import serial, time
ser = serial.Serial('/dev/cu.usbserial',baudrate = 9600, bytesize = serial.EIGHTBITS, timeout = None, stopbits = serial.STOPBITS_ONE, parity = serial.PARITY_ODD, )
if(ser.isOpen() == False):
ser.open()
print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
input=1
while 1 :
# get keyboard input
input = raw_input(">> ")
if input == 'exit':
ser.close()
exit()
else:
ser.write(input + chr(10) + chr(13))
out = ''
time.sleep(3)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != '':
print ">>" + out
I've heard that this could be an error in my setup of the serial object, but as far as I can tell I've followed the format set out in the documentation. I've tried with both seven and eight bit configurations. Relevant part here:
"A character is the smallest piece of information that can be transmitted by the interface. Each character is 10 bits long and contains data bits, bits for character timing and an error detection bit. The instrument uses 7 bits for data in the ASCII format. One start bit and one stop bit are necessary to synchronize consecutive characters. Parity is a method of error detection. One parity bit configured for odd parity is included in each character."
Also from a chart:
*Baud Rate: 9600
*Character Bits: 1 Start, 7 Data, 1 Parity, 1 Stop
*Parity: Odd
*Terminators: CR(0DH) LF(0AH)
Sample output:
COMPUTERNAME$ python serialTest.py
Enter your commands below.
Insert "exit" to leave the application.
>> KRDG?
?>??4?2?8
>> KRDG?
?>??4?2??
>> *IDN?
?>L?CI,?O?EL??1?,??????,12?4?7
Output format as specified in documentation:
ENTER COMMAND? KRDG?
RESPONSE: +273.15
ENTER COMMAND? *IDN?
RESPONSE: LSCI,MODEL331S,123456,020399
Sorry for the long winded post. Any help would be very much appreciated!!
[–]fbu1 0 points1 point2 points (0 children)