1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python > test<CR><LF><ACK><NAK>test2<CR><LF>を処理する実装例

Last updated at Posted at 2016-02-16
動作環境
Raspberry Pi2 + raspbian (python serialインストール済み)
Windows 7 pro 32bit

やりたいこと

RS-232C通信で使われるcontrol characterを扱うpython実装の確認

接続形態

![](http://yuml.me/diagram/class/[Win7]RS232C-->[クロス], [クロス]RS232C-->[Pi2])

USBシリアルケーブルを2本使用

  • Win側 > SRC06-USB (バッファロー)
  • Pi2側 > USB-RS232 (TFTEC JAPAN)

通信速度は 9600 bps

ソフト

code

Pi2側のコードは以下を実装してみた

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

結果

Win7側のRS232Cテストツールから以下の文字列を送信してみた。

test<CR><LF><ACK><NAK>test2<CR><LF>

Pi2側(上記コードを実行)では以下となった

test
control char:6
control char:21
test2
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?