Operating environment
Raspberry Pi2 + raspbian (python serial installed)
Windows 7 pro 32bit
Confirmation of python implementation that handles control characters used in RS-232C communication
Uses two USB serial cables
--Win side> SRC06-USB (Buffalo) --Pi2 side> USB-RS232 (TFTEC JAPAN)
Communication speed is 9600 bps
--Windows side: RS232C test tool
--You can send <ACK>
and strings together
code
The code on the Pi2 side tried to implement the following
160216commPrintable.py
#!/usr/bin/env python
import serial
import time
def isControlCharOtherThanCRLF(code):
if ord(code) == 13 or ord(code) == 10:
return False
return ord(code) < 32
def main():
con=serial.Serial('/dev/ttyUSB0', 9600, timeout=10)
rcvd=''
while 1:
code = con.read()
if len(code) > 0:
if isControlCharOtherThanCRLF(code):
print "control char:" + str(ord(code))
else:
rcvd = rcvd + code
if "\n" in rcvd or "\r" in rcvd:
print rcvd,
rcvd = ''
main()
I sent the following character string from the RS232C test tool on the Win7 side.
test<CR><LF><ACK><NAK>test2<CR><LF>
On the Pi2 side (execute the above code) it is as follows
test
control char:6
control char:21
test2
Recommended Posts