from pycomm3 import LogixDriver, CommError
import minimalmodbus as mbs

class EthernetIP:
    def __init__(self, ip="192.168.1.12",type="Micro800" ):
        self.driver = LogixDriver(ip)
    
    def read_tag(self, tag):
        try:
            with self.driver:
                resp = self.driver.read(tag)
            return resp
        except Exception as e:
            print("Error in read_tag")
            print(e)

    def read_tags(self, tags):
        try:
            with self.driver:
                resp = self.driver.read(*tags)
            return resp
        
        except CommError as ce:
            print(ce)
        except Exception as e:
            print("Error in read_tags")
            print(e)

    def write_tag(self, tag, value):
        try:
            with self.driver:
                resp = self.driver.write(tag, value)
            return resp
        except Exception as e:
            print("Error in write_tag")
            print(e)
    
    def write_tags(self, tags, values):
        try:
            a = zip(tags, values)
            with self.driver:
                resp = self.driver.write(*a)
            return resp
        except Exception as e:
            print("Error in write_tag")
            print(e)

class Modbus:
    def __init__(self, baud=19200, stopBits=1, station=247, device="tty/S0"):
        self.driver = mbs.Instrument(device)