LoginSignup
2
1

More than 5 years have passed since last update.

Python 2 > C実装の標準出力をRS-232C出力する (pySerial 2.6使用)

Last updated at Posted at 2018-08-01
動作環境
Raspberry Pi 2 Model B (以下RPi)
Raspbian Jessie
Python 2.7.9
Python 3.4.2
gcc (Raspbian 4.9.2-10) 4.9.2
pySerial 2.6
USBシリアルケーブル (Prolific Technology社のドライバで動作)
  変換名人 USB-RS232 (コネクタ部分は青い透過型のハウジング)

関連

概要

  • C実装で1秒おきに標準出力される
  • その標準出力を受けて、PythonでRS-232C出力する

C > 1秒おきの標準出力

こちらの実装をそのまま使います。

接続環境

RPi -- USBシリアル -- RS-232Cケーブル(クロス) -- PCのRS-232Cポート

PC側ではTeraTermを起動し、9600bpsでの受信待ち状態にする。

PCの環境
Windows 10 Pro (64bit) バージョン 1803 (April 2018 Update)
Tera Term Version 4.9.0 (SVN# 6338)

Python 2 > 標準出力をRS-232C出力する

redirect_serial_180801.py
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import serial

# on pySerial 2.6
# on Python 2.7.9

CRLF = '\r\n'

with serial.Serial('/dev/ttyUSB0', 9600, timeout=10) as con1:
    while True:
        try:
            res = raw_input()
            con1.write(res)
            con1.write(CRLF)
        except EOFError:
            break

実行

RPi側
$ ./tickTime_180801 | python2 -u redirect_serial_180801.py 

PC側 (TeraTerm) : 9600 bps設定
qiita.png

用途

Cで実装したI2C通信(下記の発展版)の標準出力をPythonでRS-232Cに出力する。

関連

検索用キーワード

  • redirect
  • UART
2
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
2
1