Implementation example to process python> test <CR> <LF> <ACK> <NAK> test2 <CR> <LF>

Operating environment


Raspberry Pi2 + raspbian (python serial installed)
Windows 7 pro 32bit

Thing you want to do

Confirmation of python implementation that handles control characters used in RS-232C communication

Connection form

Uses two USB serial cables

--Win side> SRC06-USB (Buffalo) --Pi2 side> USB-RS232 (TFTEC JAPAN)

Communication speed is 9600 bps

soft

--Windows side: RS232C test tool --You can send <ACK> and strings together

code

The code on the Pi2 side tried to implement the following

github

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()

result

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

Implementation example to process python> test <CR> <LF> <ACK> <NAK> test2 <CR> <LF>
Offline real-time how to write Python implementation example of E14
Offline real-time how to write E11 ruby and python implementation example
Offline real-time how to write Python implementation example of E15 problem
PRML Chapter 6 Gaussian Process Regression Python Implementation
How to do portmanteau test with python