# blog:https://blog.goo.ne.jp/soratobu_macaronipenguin 
# FaceBook:https://www.facebook.com/akihiro.sugawara1
import serial
import time
send_data = [None] * 2 
send_data[0] = "01 05 00 01 01 00 9d 9a" #Relay No.1 ON 
send_data[1] = "01 05 00 01 00 00 9c 0a" #Relay No.1 OFF 
relay_status = True 
#c_port = 'COM3' # Windows 
#c_port = '/dev/ttyCH341USB0' 
c_port = '/dev/ttyUSB0' #Debian 
def plus(i): 
    return(int(i,16)) 
def send_process(i): 
    #送信データを作成 
    l_d1 = list(map(plus,send_data[i].split())) 
    send_binary = bytes(l_d1) 
    # pyserialで送信 
    with serial.Serial(port=c_port,baudrate=9600,bytesize=8,parity='N',stopbits=1,timeout=0.2) as ser: 
        res = ser.write(send_binary) 
        b_reply = ser.read(8) 
        # print(b_reply) 
#メイン 
while True: 
    send_process(relay_status) 
    # print(relay_status) 
    i = "0" 
    while i != "1": 
        i = input("1を入力してください") 
    relay_status = not(relay_status) 
