def __init__(self, updatecallback, address = 0x9d, name = "/dev/serial0", rate = 9600, Parity = None, OnePointFiveStopBits = None, config = None, host = None, port = None, modbustcp = False): # True of Modbus TCP, else if TCP then assume serial over TCP (Modbus RTU over serial) super(ModbusProtocol, self).__init__(updatecallback = updatecallback, address = address, name = name, rate = rate, config = config) try: if config == None: self.ModbusTCP = modbustcp self.Host = host self.Port = port self.TransactionID = 0 self.AlternateFileProtocol = False if host != None and port != None and self.config == None: # in this instance we do not use a config file, but config comes from command line self.UseTCP = True # ~3000 for 9600 bit time * 10 bits * 10 char * 2 packets + wait time(3000) (convert to ms * 1000) self.ModBusPacketTimoutMS = (((((1/float(self.Rate)) * 10.0) * 10.0 * 2.0) *1000.0) + 3000.0) # .00208 self.ModBusPacketTimoutMS += self.AdditionalModbusTimeout * 1000.0 if self.ModbusTCP: self.MIN_PACKET_RESPONSE_LENGTH -= 2 self.MBUS_RES_PAYLOAD_SIZE_MINUS_LENGTH -= 2 self.MBUS_FILE_READ_PAYLOAD_SIZE_MINUS_LENGTH -= 2 self.MBUS_CRC_SIZE = 0 self.MIN_PACKET_ERR_LENGTH -= 2 if self.UseTCP: self.ModBusPacketTimoutMS = self.ModBusPacketTimoutMS + 2000 #Starting serial connection if self.UseTCP: self.Slave = SerialTCPDevice(config = self.config, host = host, port = port) else: self.Slave = SerialDevice(name = name, rate = rate, Parity = Parity, OnePointFiveStopBits = OnePointFiveStopBits, config = self.config) self.Threads = self.MergeDicts(self.Threads, self.Slave.Threads) except Exception as e1: self.LogErrorLine("Error opening modbus device: " + str(e1)) self.FatalError("Error opening modbus device.") try: # CRCMOD library, used for CRC calculations self.ModbusCrc = crcmod.predefined.mkCrcFun('modbus') self.InitComplete = True except Exception as e1: self.FatalError("Unable to find crcmod package: " + str(e1))
def __init__(self, updatecallback, address=0x9d, name="/dev/serial0", rate=9600, Parity=None, OnePointFiveStopBits=None, config=None, host=None, port=None): super(ModbusProtocol, self).__init__(updatecallback=updatecallback, address=address, name=name, rate=rate, config=config) try: if host != None and port != None and self.config == None: # in this instance we do not use a config file, but config comes from command line self.UseTCP = True # ~3000 for 9600 bit time * 10 bits * 10 char * 2 packets + wait time(3000) (convert to ms * 1000) self.ModBusPacketTimoutMS = ( ((((1 / float(rate)) * 10.0) * 10.0 * 2.0) * 1000.0) + 3000.0 ) # .00208 self.ModBusPacketTimoutMS += self.AdditionalModbusTimeout * 1000.0 if self.UseTCP: self.ModBusPacketTimoutMS = self.ModBusPacketTimoutMS + 2000 #Starting serial connection if self.UseTCP: self.Slave = SerialTCPDevice(config=self.config, host=host, port=port) else: self.Slave = SerialDevice( name=name, rate=rate, Parity=Parity, OnePointFiveStopBits=OnePointFiveStopBits, config=self.config) self.Threads = self.MergeDicts(self.Threads, self.Slave.Threads) except Exception as e1: self.LogErrorLine("Error opening serial device: " + str(e1)) self.FatalError("Error opening serial device.") try: # CRCMOD library, used for CRC calculations self.ModbusCrc = crcmod.predefined.mkCrcFun('modbus') self.InitComplete = True except Exception as e1: self.FatalError("Unable to find crcmod package: " + str(e1))