Skip to content

Instantly share code, notes, and snippets.

@MitI-7
Created December 23, 2015 05:07
Show Gist options
  • Save MitI-7/01a63ff79718dd466965 to your computer and use it in GitHub Desktop.
Save MitI-7/01a63ff79718dd466965 to your computer and use it in GitHub Desktop.
PC-OP-RS1をpythonから操作する
import serial
from struct import *
from binascii import *
class PC_OP_RS1:
def __init__(self, port):
self._ser = serial.Serial(port, 115200, timeout=1)
self._LED = pack('B', 0x69) # LED点灯
self._RECEIVE = pack('B', 0x72) # 送信
self._TRANSMIT = pack('B', 0x74) # 受信
self.ch1 = pack('B', 0x31) # Aの黄
self.ch2 = pack('B', 0x32) # Aの黒
self.ch3 = pack('B', 0x33) # Bの黄
self.ch4 = pack('B', 0x34) # Bの黒
def led(self):
"""LEDを点灯させる"""
self._ser.write(self._LED)
self._ser.read() # 0x4f
def receive(self) -> bytes:
"""赤外線を受信する"""
self._ser.write(self._RECEIVE)
self._ser.read(1) # 0x59
self._ser.read(1) # 0x53
data = hexlify(self._ser.read(240))
self._ser.read(1) # 0x45
return data
def transmit(self, hex_data: bytes, channel: bytes):
""" 赤外線を送信する"""
bin_data = a2b_hex(hex_data)
self._ser.write(self._TRANSMIT)
self._ser.read(1) # 0x59
self._ser.write(channel) # チャンネルの指定
self._ser.read(1) # 0x59
self._ser.write(bin_data) # データの送信
self._ser.read(1) # 0x45
def __enter__(self):
return self
def __exit__(self, *args):
self._ser.close()
def main():
# 受信したデータをそのまま送信する
with PC_OP_RS1(2) as por:
print("LEDの点灯")
por.led()
print("受信開始")
data = por.receive()
print(data)
print("送信開始")
por.transmit(data, por.ch1)
print("終了")
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment