def NextRequest(self): """Increment the command list and execute the next command. This includes constructing the next message. """ # Transaction ID is incremented for each request to check message integrity. # It must not exceed the maximum for an 16 bit unsigned int though. self._TransID += 1 if (self._TransID > 65535): self._TransID = 1 # Get the parameters for this command. self._CmdName, self._FunctionCode, self._AddrRequested, self._QuantityRequested, \ self._MemAddr, self._UnitID, EndofList = self._ConnectionInfo.NextCommand() # Get message data appropriate for the function being executed. if self._FunctionCode in (1, 2, 3, 4): MsgData = '' # No message data is sent for a read. elif (self._FunctionCode == 5): CoilData = MBDataTable.MemMap.GetCoilsBool(self._MemAddr) if CoilData: MsgData = ModbusDataLib.coilvalue( 1) # Special format for function 5 else: MsgData = ModbusDataLib.coilvalue( 0) # Special format for function 5 elif self._FunctionCode in (6, 16): MsgData = MBDataTable.MemMap.GetHoldingRegisters( self._MemAddr, self._QuantityRequested) elif (self._FunctionCode == 15): MsgData = MBDataTable.MemMap.GetCoils(self._MemAddr, self._QuantityRequested) else: return '', True # Invalid function code. # Create and return message. try: Message = self._MBClientMsg.MBRequest(self._TransID, self._UnitID, self._FunctionCode, self._AddrRequested, self._QuantityRequested, MsgData) except: Message = '' return Message, EndofList
def NextRequest(self): """Increment the command list and execute the next command. This includes constructing the next message. """ # Transaction ID is incremented for each request to check message integrity. # It must not exceed the maximum for an 16 bit unsigned int though. self._TransID += 1 if (self._TransID > 65535): self._TransID = 1 # Get the parameters for this command. self._CmdName, self._FunctionCode, self._AddrRequested, self._QuantityRequested, \ self._MemAddr, self._UnitID, EndofList = self._ConnectionInfo.NextCommand() # Get message data appropriate for the function being executed. if self._FunctionCode in (1, 2, 3, 4): MsgData = '' # No message data is sent for a read. elif (self._FunctionCode == 5): CoilData = MBDataTable.MemMap.GetCoilsBool(self._MemAddr) if CoilData: MsgData = ModbusDataLib.coilvalue(1) # Special format for function 5 else: MsgData = ModbusDataLib.coilvalue(0) # Special format for function 5 elif self._FunctionCode in (6, 16): MsgData = MBDataTable.MemMap.GetHoldingRegisters(self._MemAddr, self._QuantityRequested) elif (self._FunctionCode == 15): MsgData = MBDataTable.MemMap.GetCoils(self._MemAddr, self._QuantityRequested) else: return '', True # Invalid function code. # Create and return message. try: Message = self._MBClientMsg.MBRequest(self._TransID, self._UnitID, self._FunctionCode, self._AddrRequested, self._QuantityRequested, MsgData) except: Message = '' return Message, EndofList
def SetCoilsBool(self, addr, data): """Write coils to the host (function 15). #######Changed to 5 addr (integer) = Modbus discrete inputs address. data (string) = Packed binary string with the data to write. """ ## print "data", data # bindata = ModbusDataLib.boollist2bin([data]) bindata = ModbusDataLib.coilvalue(data) ## print bindata # self._ModbusRequest(15, addr, 1, bindata) ## print "Size", len(bindata) self._ModbusRequest(5, addr, 1, bindata)
def SetCoilsBool(self, addr, data): """Write coils to the host (function 15). #######Changed to 5 addr (integer) = Modbus discrete inputs address. data (string) = Packed binary string with the data to write. """ ## print "data", data # bindata = ModbusDataLib.boollist2bin([data]) bindata = ModbusDataLib.coilvalue(data) ## print bindata # self._ModbusRequest(15, addr, 1, bindata) ## print "Size", len(bindata) self._ModbusRequest(5, addr, 1, bindata)