저번 시간에 exe(실행파일)로 버튼을 누르면 PLC OUT 부분 led가 켜지는 것까지 알아봤습니다.
이번 시간에는 IN부분을 까지 만들어서 PLC IN, OUT 둘 다 exe 파일로 만들어 보겠습니다.
테스트용으로 만든 프로그램이라 실제 사용하려면 코드를 손보거나 추가해야하는 부분이 많습니다!!
참고용으로만 봐주시면 감사하겠습니다.
최종 결과물

구상하기
1. 저번 시간에 만든 코드에 IN 부분을 추가해야 합니다.
2. IN 부분의 작동원리
접속이라는 버튼을 누르면 PLC의 IN 부분의 LED가 켜져 있는 번호 그대로 화면에 나타내 주게 만들겠습니다.
한마디로 접속 버튼을 누르면 PLC의 상태를 미러링 하는 프로그램입니다.
3. IN 부분의 읽어오기 프로토콜을 알아야 합니다
IN 부분 READ 프로토콜

저번시간에 사용한 쓰기와 읽기는 큰 차는 없습니다.
WSS를 RSS로 바꿔주기만 하면 됩니다.
import serial
import time
from tkinter import *
ser = serial.Serial('COM3', 9600, timeout=1)
root = Tk()
root.title("시리얼 통신")
root.geometry("480x480+300+100") # 가로 세로 + x좌표 + y좌표
def btncmd_contect():
ser.write(b'\x0501RSS0106%PW000\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
a = readOut[10]
b = readOut[11]
c = readOut[12]
d = readOut[13]
접속버튼을 누르면 IN부분을 읽어오게 만들었습니다.
다음으로 IN부분의 버튼 제작입니다.
코드가 길어서 일부만 올렸습니다.
ABCD 영역으로 나누었고 plc 부분을 4개씩 끊어서 16진수로 0~F로 나누어 짰습니다
PLC에는 거꾸로 들어가기 때문에 DCBA입니다
D부분
# d
if d == "0":
btn_00 = Button(root, text="00", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", width=5, height=2)
btn_03 = Button(root, text="03", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "1":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", width=5, height=2)
btn_03 = Button(root, text="03", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "2":
btn_00 = Button(root, text="00", width=5, height=2)
btn_01 = Button(root, text="01", bg="red", width=5, height=2)
btn_02 = Button(root, text="02", width=5, height=2)
btn_03 = Button(root, text="03", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "3":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", bg="red", width=5, height=2)
btn_02 = Button(root, text="02", width=5, height=2)
btn_03 = Button(root, text="03", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "4":
btn_00 = Button(root, text="00", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", bg="red", width=5, height=2)
btn_03 = Button(root, text="03", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "5":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", bg="red", width=5, height=2)
btn_03 = Button(root, text="03", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "6":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", bg="red", width=5, height=2)
btn_02 = Button(root, text="02", bg="red", width=5, height=2)
btn_03 = Button(root, text="03", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "7":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", bg="red", width=5, height=2)
btn_02 = Button(root, text="02", bg="red", width=5, height=2)
btn_03 = Button(root, text="03", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "8":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", width=5, height=2)
btn_03 = Button(root, text="03", bg="red", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "9":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", width=5, height=2)
btn_03 = Button(root, text="03", bg="red", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "A":
btn_00 = Button(root, text="00", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", width=5, height=2)
btn_03 = Button(root, text="03", bg="red", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "B":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", bg="red", width=5, height=2)
btn_02 = Button(root, text="02", width=5, height=2)
btn_03 = Button(root, text="03", bg="red", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "C":
btn_00 = Button(root, text="00", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", bg="red", width=5, height=2)
btn_03 = Button(root, text="03", bg="red", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "D":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", width=5, height=2)
btn_02 = Button(root, text="02", bg="red", width=5, height=2)
btn_03 = Button(root, text="03", bg="red", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "E":
btn_00 = Button(root, text="00", width=5, height=2)
btn_01 = Button(root, text="01", bg="red", width=5, height=2)
btn_02 = Button(root, text="02", bg="red", width=5, height=2)
btn_03 = Button(root, text="03", bg="red", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
elif d == "F":
btn_00 = Button(root, text="00", bg="red", width=5, height=2)
btn_01 = Button(root, text="01", bg="red", width=5, height=2)
btn_02 = Button(root, text="02", bg="red", width=5, height=2)
btn_03 = Button(root, text="03", bg="red", width=5, height=2)
btn_00.grid(row=1, column=1, padx=5, pady=5)
btn_01.grid(row=1, column=2, padx=5, pady=5)
btn_02.grid(row=1, column=3, padx=5, pady=5)
btn_03.grid(row=1, column=4, padx=5, pady=5)
이런 식으로 CBA 부분도 만들었습니다.
접속 부분
btn_contect = Button(root, fg="blue", text="접속",command=btncmd_contect, width=5, height=2)
btn_contect.grid(row=3, column=1, padx=5, pady=10)
출력 부분
def btncmd_z():
ser.write(b'\x0501WSS0106%PW0020000\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_0():
ser.write(b'\x0501WSS0106%PW0020001\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_1():
ser.write(b'\x0501WSS0106%PW0020002\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_2():
ser.write(b'\x0501WSS0106%PW0020004\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_3():
ser.write(b'\x0501WSS0106%PW0020008\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_4():
ser.write(b'\x0501WSS0106%PW0020010\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_5():
ser.write(b'\x0501WSS0106%PW0020020\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_6():
ser.write(b'\x0501WSS0106%PW0020040\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_7():
ser.write(b'\x0501WSS0106%PW0020080\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_8():
ser.write(b'\x0501WSS0106%PW0020100\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_9():
ser.write(b'\x0501WSS0106%PW0020200\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_A():
ser.write(b'\x0501WSS0106%PW0020400\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_B():
ser.write(b'\x0501WSS0106%PW0020800\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_C():
ser.write(b'\x0501WSS0106%PW0021000\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_D():
ser.write(b'\x0501WSS0106%PW0022000\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_E():
ser.write(b'\x0501WSS0106%PW0024000\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_F():
ser.write(b'\x0501WSS0106%PW0028000\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
def btncmd_all():
ser.write(b'\x0501WSS0106%PW002FFFF\x04')
readOut = ser.readline().decode('ascii')
print(readOut)
버튼 별로 누르면 쓰기 영역에 미리 써놓은 write가 보내집니다.
출력 버튼
btn_0 = Button(root, text="20", command=btncmd_0, width=5, height=2)
btn_1 = Button(root, text="21", command=btncmd_1, width=5, height=2)
btn_2 = Button(root, text="22", command=btncmd_2, width=5, height=2)
btn_3 = Button(root, text="23", command=btncmd_3, width=5, height=2)
btn_0.grid(row=5, column=1, padx=5, pady=5)
btn_1.grid(row=5, column=2, padx=5, pady=5)
btn_2.grid(row=5, column=3, padx=5, pady=5)
btn_3.grid(row=5, column=4, padx=5, pady=5)
btn_4 = Button(root, text="24", command=btncmd_4, width=5, height=2)
btn_5 = Button(root, text="25", command=btncmd_5, width=5, height=2)
btn_6 = Button(root, text="26", command=btncmd_6, width=5, height=2)
btn_7 = Button(root, text="27", command=btncmd_7, width=5, height=2)
btn_4.grid(row=5, column=5, padx=5, pady=5)
btn_5.grid(row=5, column=6, padx=5, pady=5)
btn_6.grid(row=5, column=7, padx=5, pady=5)
btn_7.grid(row=5, column=8, padx=5, pady=5)
btn_8 = Button(root, text="28", command=btncmd_8, width=5, height=2)
btn_9 = Button(root, text="29", command=btncmd_9, width=5, height=2)
btn_A = Button(root, text="2A", command=btncmd_A, width=5, height=2)
btn_B = Button(root, text="2B", command=btncmd_B, width=5, height=2)
btn_8.grid(row=6, column=1, padx=5, pady=5)
btn_9.grid(row=6, column=2, padx=5, pady=5)
btn_A.grid(row=6, column=3, padx=5, pady=5)
btn_B.grid(row=6, column=4, padx=5, pady=5)
btn_C = Button(root, text="2C", command=btncmd_C, width=5, height=2)
btn_D = Button(root, text="2D", command=btncmd_D, width=5, height=2)
btn_E = Button(root, text="2E", command=btncmd_E, width=5, height=2)
btn_F = Button(root, text="2F", command=btncmd_F, width=5, height=2)
btn_C.grid(row=6, column=5, padx=5, pady=5)
btn_D.grid(row=6, column=6, padx=5, pady=5)
btn_E.grid(row=6, column=7, padx=5, pady=5)
btn_F.grid(row=6, column=8, padx=5, pady=5)
초기화, ALL 버튼
btn_k = Button(root, fg="red", text="초기화", command=btncmd_z, width=5, height=2)
btn_k.grid(row=7, column=1, padx=5, pady=10)
btn_all = Button(root, fg="blue", text="ALL",
command=btncmd_all, width=5, height=2)
btn_all.grid(row=7, column=2, padx=5, pady=10)
root.mainloop()
최종 결과

접속을 누르게 되면 IN부분에 해당하는 읽기 전용 UI가 나옵니다.
여기서 접속을 누르면??

00~0F
빨간색이 PLC IN 부분 그대로 비춰줍니다. (PLC 미러링)
여기서 접속을 누르게 되면 다시 PLC를 미러링 해옵니다.
언제든지 접속 버튼만 누르면 PLC IN부분을 미러링이 가능합니다.
20~2F
기존의 out 부분과 같습니다.
버튼을 누르면 PLC out 부분에 해당번호에 똑같이 LED 가 켜집니다.
초기화 ALL 버튼도 마찬가지입니다.
여기까지 PLC를 Python-exe에서 모니터링, 제어를 해보았습니다.
다음 시간에는 PLC로부터 받은 데이터를 엑셀에 저장하고 데이터를 분석하여 그래프로 까지 출력해 보는 프로그램을 만들어 보겠습니다.
감사합니다.