import modbus_tk.modbus_tcp as mt
import  modbus_tk.defines as md

plc = mt.TcpMaster('192.168.1.15')

def set_06(addr,y):
    plc.execute(slave=1, function_code=6, starting_address=addr, output_value=y)


def get_03(adr,num):
    db=plc.execute(slave=1,function_code=3, starting_address=adr,quantity_of_x=num)
    return db





from PyQt5.Qt import *



class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("的学习")
        self.resize(1000,500)
        self.labels = []

        self.setup_ui()
        self.a=0
        self.b =0

        self.timer = QTimer(self)
        self.timer.timeout.connect(self.fun)
        self.timer.start(200)



    def setup_ui(self):
        for i in range(16):
            btn=QPushButton(f'btn-{i}',self)
            btn.setFixedSize(50,30)
            btn.move(20+50*i,50)
            btn.setProperty('id',i)
            btn.clicked[bool].connect(self.btn_click)
            btn.setCheckable(True)

        for i in range(16,32):
            btn = QPushButton(f'btn-{i}', self)
            btn.setFixedSize(50, 30)
            btn.move(20 + 50 * (i-16),100)
            btn.setProperty('id', i)
            btn.clicked[bool].connect(self.btn_click)
            btn.setCheckable(True)

        for i in range(32):
            label=QLabel(self)
            label.setFixedSize(40,40)
            label.setStyleSheet('background-color:red;border-radius:20px')
            label.setProperty('id',i)
            self.labels.append(label)

            if i<16:
                label.move(20+50*i,250)
            elif i<32:
                label.move(20+50*(i-16),350)




    def btn_click(self,state):
        sender = self.sender()

        print(sender.property('id'))

        if sender.property('id')<16:
            key = 1 << sender.property('id')
            if state:
                self.a = self.a | key
            else:
                self.a = self.a & ~key

        elif sender.property('id')<32:
            key = 1<<sender.property('id')-16
            if state:
                self.b= self.b | key
            else:
                self.b = self.b & ~key

        set_06(0xa7a8,self.a)
        set_06(0xa7a9,self.b)






    def fun(self):
        db = get_03(0xa7b8, 1)
        db2 = get_03(1064, 1)

        # bin_db = bin(db[0])[2:].zfill(16)
        # print(bin_db)
        # bin_da2 = bin(db2[0])[2:].zfill(16)


        for i in range(16):
            if db[0] & (1 << i) > 0:
                self.labels[i].setStyleSheet('background-color:green;border-radius:20px')
            else:
                self.labels[i].setStyleSheet('background-color:red;border-radius:20px')

        for i in range(16):
            if db2[0] & (1 << i) > 0:
                self.labels[i+16].setStyleSheet('background-color:green;border-radius:20px')
            else:
                self.labels[i+16].setStyleSheet('background-color:red;border-radius:20px')













    def closeEvent(self, a0) -> None:
        set_06(0xa7a8, 0)
        set_06(0xa7a9, 0)


if __name__ == '__main__':
    import sys
    app=QApplication(sys.argv)

    win=Window()
    win.show()





    sys.exit(app.exec_())






06-15 20:07