import modbus_tk.modbus_tcp as modbus_tcp
import time

timeout = 10
class ZLAN6042:
    def __init__(self):
        self.s = modbus_tcp.TcpMaster("169.254.89.200", 502)  # Open socket for PCS
        self.s.set_timeout(timeout)

    def AI(self,num):
        if num==1:
            data=self.s.execute(0x01, 0x04,0,1)
            return data
        else:
            data = self.s.execute(0x01, 0x04, 1, 1)
            return data

    def DI(self,num):
        if num==1:
            data = self.s.execute(0x01, 0x01, 0, 1)
            return data
        elif num==2:
            data = self.s.execute(0x01, 0x01, 1, 1)
            return data
        elif num==3:
            data = self.s.execute(0x01, 0x01, 2, 1)
            return data
        else:
            data = self.s.execute(0x01, 0x01, 3, 1)
            return data

    def RL(self,num,state):
        if num==1:
            if state=='ON':
                data = self.s.execute(0x01, 0x05, 0x0010,output_value=0xff00)
                return data
            else:
                data = self.s.execute(0x01, 0x05, 0x0010, output_value=0x0000)
                return data
        elif num==2:
            if state=='ON':
                data = self.s.execute(0x01, 0x05, 0x0011,output_value=0xff00)
                return data
            else:
                data = self.s.execute(0x01, 0x05, 0x0011, output_value=0x0000)
                return data
        elif num==3:
            if state=='ON':
                data = self.s.execute(0x01, 0x05, 0x0012,output_value=0xff00)
                return data
            else:
                data = self.s.execute(0x01, 0x05, 0x0012, output_value=0x0000)
                return data
        else:
            if state=='ON':
                data = self.s.execute(0x01, 0x05, 0x0013,output_value=0xff00)
                return data
            else:
                data = self.s.execute(0x01, 0x05, 0x0013, output_value=0x0000)
                return data

if __name__ == "__main__":
    control=ZLAN6042()
    # print(control.AI(2))
    # print(control.DI(1))
    # print(control.RL(1,'ON'))
    # print(control.RL(2,'ON'))
    # print(control.RL(3,'ON'))
    # print(control.RL(4,'ON'))

    print(control.RL(1, 'OFF'))
    print(control.RL(2, 'OFF'))
    print(control.RL(3, 'OFF'))
    print(control.RL(4, 'OFF'))