So I'm trying to communicate with with a zigbee module connected to a serial port by using AT (sometimes refered as Hayes) commands.
My problem is I can't figure out how to correctly read its answers with the pyserial module of Python (I'm using Python 2.7 on an embedded device).
Sometime the script manage to perfectly read the module's responses and sometimes it just returns a suite of 'A' characters.
Here is a sample output :
Opening serial /dev/ttyS2 ... /dev/ttyS2 is open...
Enter command or 'exit' : AT
------------- Response -------------
AT OK
Enter command or 'exit' : ATI
------------- Response ------------- aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaAaAaaaAaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Enter command or 'exit' :
I really don't understand what's happening here but I think it's probably because I use the pyserial module incorrectly. I've already tried to use the inWaiting() command to read only the bytes contained in the input buffer but this function always returns zero and the readline() command don't seems to work either so I just read packets of 1024 bytes.
Here's the code I've written so far :
import serial, time, os
def sendAtCommand(command):
# responseArray = []
# i = 0
try:
# print('Number of bytes waiting in input buffer before write : '+str(ser.inWaiting())) -> always zero...
if ser.inWaiting():
# flush input buffer, discarding all its contents
print('flushing input buffer ...')
ser.flushInput()
# flush output buffer, aborting current output
# ser.flushOutput()
try:
ser.write(command.encode('ascii')+'\r')
#ser.write(command+'\r')
#time.sleep(5)
except SerialTimeoutException as e:
print(repr(e))
response = ser.read(size=1024)
# print('Number of bytes waiting in input buffer after write : '+str(ser.inWaiting())) -> always zero
# while ser.inWaiting():
# print('Reading response ... '+str(i))
# responseArray.append(ser.readline(eol='\r\n'))
# i += 1
# time.sleep(0.2)
print('------------------------------------')
print('------------- Response -------------')
print('------------------------------------')
print(response)
# for line in responseArray:
# print(line)
print('------------------------------------')
time.sleep(1)
except KeyboardInterrupt as e:
print('Closing serial '+port+' before interrupting ...')
ser.close()
exit()
VERSION = '0.02'
firstStart = True
port = '/dev/ttyS2'
baudrate = 19200
bytesize = 8
parity = 'N'
stopbits = 1
timeout = 1
xonxoff = False
rtscts = False
dsrdtr = False
write_timeout = None
inter_byte_timeout = None
try:
os.system('cls' if os.name == 'nt' else 'clear')
print('')
print('PySerial version : '+serial.VERSION)
firstStart = False
print('')
print('Opening serial '+port+' ...')
ser = serial.Serial(port, baudrate, bytesize, parity, stopbits, timeout,
xonxoff, rtscts, write_timeout, dsrdtr, inter_byte_timeout)
except ValueError as e:
print(repr(e))
except SerialException as e:
print(repr(e))
if ser.isOpen():
print(ser.name + ' is open...')
print('')
while True:
try:
cmd = raw_input('Enter command or \'exit\' : ')
if cmd == 'exit':
ser.close()
exit()
else:
sendAtCommand(cmd)
except KeyboardInterrupt as e:
print('Closing serial '+port+' before interrupting ...')
ser.close()
exit()