Skip to content

Instantly share code, notes, and snippets.

@Drunkar
Created June 2, 2016 04:02
Show Gist options
  • Save Drunkar/b80eaefabb62c74d01804164e5234aca to your computer and use it in GitHub Desktop.
Save Drunkar/b80eaefabb62c74d01804164e5234aca to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import sys
from threading import Thread
import threading
import time
import serial
def main():
im920RW().start()
while True:
time.sleep(10)
print("--")
# ============================================== #
# IM920 送受信クラス
# ============================================== #
class im920RW(threading.Thread):
# ============================================== #
# コンストラクタ
# ============================================== #
def __init__(self):
threading.Thread.__init__(self)
# デーモン化(これ以外のデーモンで無いスレッドが終了すると終了する)
self.setDaemon(True)
# ============================================== #
# メイン処理
# ============================================== #
def run(self):
global com
try:
com = serial.Serial(
port = '/dev/ttyAMA0',
baudrate = 19200,
bytesize = serial.EIGHTBITS, # 8 ビット
parity = serial.PARITY_NONE, # パリティチェック 偶数 = serial.PARITY_EVEN
stopbits = serial.STOPBITS_ONE, # 1 ストップビット
timeout = None, # wait forever
xonxoff = False,
rtscts = False,
writeTimeout = None,
dsrdtr = False,
interCharTimeout = None)
com.flushInput()
com.flushOutput()
com.open()
print('Open Serial')
print(1, 0, 'Open Serial')
while True: # 送受信ループ
rData = ''
com.flushInput()
rData = com.readline().strip() # 1行受信
if rData == '': continue
print(rData)
rDatas = rData.split(':') # ヘッダとデータの区切り
if len(rDatas) > 1:
rData = rData.split(':')[1] # データ部分取得
com.write('TXDA ' + rData + '\r\n') # 送信
com.flushOutput()
com.readline() # IM920 からの 'OK'or'NG' 読み捨て
except Exception as e:
print('Serial Open Error:' + str(e))
print(1, 0, 'SerialOpen Error')
sys.exit()
if __name__=='__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment