def HandleGETRequest(self, Func, Addr, Qty, TID, UID): # Decode message. 'RequestData' means either number of coils or registers, depending # upon the function code being received. TransID, UnitID, FunctionCode, Address, RequestData, \ ExceptionCode = ModbusMsg.MBGetRequest(Func, Addr, Qty, TID, UID) if FunctionCode == 1: # Read coils. bindata = MBDataTable.MemMap.GetCoils( Address, RequestData) # RequestData = quantity. RequestData = ModbusRestLib.inversorbin(bindata) ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 2: # Read discrete inputs. bindata = MBDataTable.MemMap.GetDiscreteInputs( Address, RequestData) # RequestData = quantity. RequestData = ModbusRestLib.inversorbin(bindata) ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 3: # Read holding registers. bindata = MBDataTable.MemMap.GetHoldingRegisters( Address, RequestData) # RequestData = quantity. RequestData = ModbusRestLib.bin2hex(bindata) ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 4: # Read input registers. bindata = MBDataTable.MemMap.GetInputRegisters( Address, RequestData) # RequestData = quantity. RequestData = ModbusRestLib.bin2hex(bindata) ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode > 127: # Modbus exception. if (ExceptionCode == 1): ReplyData = _ErrorHtml % ('The Modbus function requested is not supported using HTTP Get', \ Func, Addr, Qty, TID, UID) return ReplyData, False elif (ExceptionCode == 2): ReplyData = _ErrorHtml % ('The address or address plus quantity is out of range.', \ Func, Addr, Qty, TID, UID) return ReplyData, False else: ReplyData = ModbusMsg.MBErrorResponse(TransID, UnitID, FunctionCode, ExceptionCode) return ReplyData, True else: # We shouldn't get here unless we can't understand the request at all. ReplyData = _ErrorHtml % ( """The Modbus function is not supported using HTTP GET or else one or more URL parameters could not be understood.""", Func, Addr, Qty, TID, UID) return ReplyData, False
def HandlePOSTRequest(self, Func, Addr, Qty, TID, UID, XMLData): # Decode message. 'RequestData' means either number of coils or registers, depending # upon the function code being received. TransID, UnitID, FunctionCode, Address, RequestData, \ ExceptionCode = ModbusMsg.MBPostRequest(Func, Addr, Qty, TID, UID, XMLData) if FunctionCode == 5: # Write single coil. if (RequestData == '0000'): bindata = '\x00\x00' else: bindata = '\xFF\x00' MBDataTable.MemMap.SetCoils(Address, 1, bindata) # RequestData contains data. ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 6: # Write single holding register. bindata = ModbusRestLib.hex2bin(RequestData) MBDataTable.MemMap.SetHoldingRegisters(Address, 1, bindata) # RequestData contains data. ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 15: # Write multiple coils. bindata = ModbusRestLib.bininversor(RequestData[1]) MBDataTable.MemMap.SetCoils(Address, RequestData[0], bindata) # RequestData is a tuple. ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData[0]) return ReplyData, True elif FunctionCode == 16: # Write multiple holding registers. bindata = ModbusRestLib.hex2bin(RequestData[1]) MBDataTable.MemMap.SetHoldingRegisters(Address, RequestData[0], bindata) # RequestData is a tuple. ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData[0]) return ReplyData, True elif FunctionCode > 127: # Modbus exception. if (ExceptionCode == 1): ReplyData = _ErrorHtml % ('The Modbus function requested is not supported using HTTP POST', \ Func, Addr, Qty, TID, UID) return ReplyData, False elif (ExceptionCode == 2): ReplyData = _ErrorHtml % ('The address or address plus quantity is out of range.', \ Func, Addr, Qty, TID, UID) return ReplyData, False else: ReplyData = ModbusMsg.MBErrorResponse(TransID, UnitID, FunctionCode, ExceptionCode) return ReplyData, True else: # We shouldn't get here unless we can't understand the request at all. ReplyData = _ErrorHtml % ("""The Modbus function is not supported using HTTP POST or else one or more URL parameters could not be understood.""", Func, Addr, Qty, TID, UID) return ReplyData, False
def HandleGETRequest(self, Func, Addr, Qty, TID, UID): # Decode message. 'RequestData' means either number of coils or registers, depending # upon the function code being received. TransID, UnitID, FunctionCode, Address, RequestData, \ ExceptionCode = ModbusMsg.MBGetRequest(Func, Addr, Qty, TID, UID) if FunctionCode == 1: # Read coils. bindata = MBDataTable.MemMap.GetCoils(Address, RequestData) # RequestData = quantity. RequestData = ModbusRestLib.inversorbin(bindata) ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 2: # Read discrete inputs. bindata = MBDataTable.MemMap.GetDiscreteInputs(Address, RequestData) # RequestData = quantity. RequestData = ModbusRestLib.inversorbin(bindata) ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 3: # Read holding registers. bindata = MBDataTable.MemMap.GetHoldingRegisters(Address, RequestData) # RequestData = quantity. RequestData = ModbusRestLib.bin2hex(bindata) ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 4: # Read input registers. bindata = MBDataTable.MemMap.GetInputRegisters(Address, RequestData) # RequestData = quantity. RequestData = ModbusRestLib.bin2hex(bindata) ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode > 127: # Modbus exception. if (ExceptionCode == 1): ReplyData = _ErrorHtml % ('The Modbus function requested is not supported using HTTP Get', \ Func, Addr, Qty, TID, UID) return ReplyData, False elif (ExceptionCode == 2): ReplyData = _ErrorHtml % ('The address or address plus quantity is out of range.', \ Func, Addr, Qty, TID, UID) return ReplyData, False else: ReplyData = ModbusMsg.MBErrorResponse(TransID, UnitID, FunctionCode, ExceptionCode) return ReplyData, True else: # We shouldn't get here unless we can't understand the request at all. ReplyData = _ErrorHtml % ("""The Modbus function is not supported using HTTP GET or else one or more URL parameters could not be understood.""", Func, Addr, Qty, TID, UID) return ReplyData, False
def HandlePOSTRequest(self, Func, Addr, Qty, TID, UID, XMLData): # Decode message. 'RequestData' means either number of coils or registers, depending # upon the function code being received. TransID, UnitID, FunctionCode, Address, RequestData, \ ExceptionCode = ModbusMsg.MBPostRequest(Func, Addr, Qty, TID, UID, XMLData) if FunctionCode == 5: # Write single coil. if (RequestData == '0000'): bindata = '\x00\x00' else: bindata = '\xFF\x00' MBDataTable.MemMap.SetCoils(Address, 1, bindata) # RequestData contains data. ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 6: # Write single holding register. bindata = ModbusRestLib.hex2bin(RequestData) MBDataTable.MemMap.SetHoldingRegisters( Address, 1, bindata) # RequestData contains data. ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData) return ReplyData, True elif FunctionCode == 15: # Write multiple coils. bindata = ModbusRestLib.bininversor(RequestData[1]) MBDataTable.MemMap.SetCoils(Address, RequestData[0], bindata) # RequestData is a tuple. ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData[0]) return ReplyData, True elif FunctionCode == 16: # Write multiple holding registers. bindata = ModbusRestLib.hex2bin(RequestData[1]) MBDataTable.MemMap.SetHoldingRegisters( Address, RequestData[0], bindata) # RequestData is a tuple. ReplyData = ModbusMsg.MBResponse(TransID, UnitID, FunctionCode, RequestData[0]) return ReplyData, True elif FunctionCode > 127: # Modbus exception. if (ExceptionCode == 1): ReplyData = _ErrorHtml % ('The Modbus function requested is not supported using HTTP POST', \ Func, Addr, Qty, TID, UID) return ReplyData, False elif (ExceptionCode == 2): ReplyData = _ErrorHtml % ('The address or address plus quantity is out of range.', \ Func, Addr, Qty, TID, UID) return ReplyData, False else: ReplyData = ModbusMsg.MBErrorResponse(TransID, UnitID, FunctionCode, ExceptionCode) return ReplyData, True else: # We shouldn't get here unless we can't understand the request at all. ReplyData = _ErrorHtml % ( """The Modbus function is not supported using HTTP POST or else one or more URL parameters could not be understood.""", Func, Addr, Qty, TID, UID) return ReplyData, False