def MBPostRequest(self, Func, Addr, Qty, TID, UID, Message): # Check the parameters which represent the URL string. FunctionCode, Address, Quantity, TransID, UnitID = self._ValidateURLParams(Func, Addr, Qty, TID, UID) # Check if the values could be read without error. if ((FunctionCode == None) or (Address == None) or (Quantity == None) or (TransID == None) or (UnitID == None)): # Set some initial default values for these. TransID = -1 UnitID = -1 FunctionCode = 0 Address = 0 data = '0000' exceptioncode = 0 return TransID, UnitID, FunctionCode, Address, data, exceptioncode # Try to parse the XML message to get just the data portion of the message. XMLParser = RestXMLParser() if XMLParser.ParseRequest(Message): MsgData = XMLParser.GetMsgData() else: MsgData = '' # At this point, the parameters have all be checked to see if they can be used. # Now validate the data according to the requirements of each function. if (FunctionCode == 5): exceptioncode = 0 # Default exception code. # Check for address limits. if not self._AddrLimitsOK(Address, Quantity, FunctionCode): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 2 # Exception code. # Check if request data is valid. if MsgData not in ('0000', 'ff00', 'fF00', 'Ff00', 'FF00'): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. return TransID, UnitID, FunctionCode, Address, MsgData, exceptioncode elif (FunctionCode == 6): exceptioncode = 0 # Default exception code. # Check for address limits. if not self._AddrLimitsOK(Address, Quantity, FunctionCode): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 2 # Exception code. # Check if message length is OK. if (len(MsgData) != 4): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. else: # Try to convert to binary string. If no good, then # the string is not valid hexadecimal data. try: temp = ModbusDataStrLib.hex2bin(MsgData) except: FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. return TransID, UnitID, FunctionCode, Address, MsgData, exceptioncode elif (FunctionCode == 15): exceptioncode = 0 # Default exception code. # Check for address limits. if not self._AddrLimitsOK(Address, Quantity, FunctionCode): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 2 # Exception code. # Qty of outputs is out of range. elif ((Quantity < 1) or (Quantity > self._protocollimits[FunctionCode])): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Qty for coils exceeds the amount of data sent. elif (Quantity > len(MsgData)): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Qty for coils is less than the amount of data sent. elif (Quantity < (len(MsgData) - 8)): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Qty for coils exceeds amount of data sent. # Try to convert to binary string. If no good, then # the string is not valid hexadecimal data. # Length must also be a multiple of 8. else: try: temp = ModbusDataStrLib.bininversor(MsgData) except: FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. return TransID, UnitID, FunctionCode, Address, (Quantity, MsgData), exceptioncode elif (FunctionCode == 16): exceptioncode = 0 # Default exception code. # Check for address limits. if not self._AddrLimitsOK(Address, Quantity, FunctionCode): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 2 # Exception code. # Qty of registers is out of range. elif ((Quantity < 1) or (Quantity > self._protocollimits[FunctionCode])): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Qty does not match the amount of data sent. elif ((Quantity * 4) != len(MsgData)): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Check if message length is OK. Length must be a multiple of 4. elif ((len(MsgData) % 4) != 0): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. else: # Try to convert to binary string. If no good, then # the string is not valid hexadecimal data. try: temp = ModbusDataStrLib.hex2bin(MsgData) except: FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. return TransID, UnitID, FunctionCode, Address, (Quantity, MsgData), exceptioncode # Function code is not supported. else: # This error code needs special treatment in case of bad function # codes that are very large. FunctionCode = (FunctionCode & 0xff) | 0x80 # Create error code. exceptioncode = 1 # Exception code. return TransID, UnitID, FunctionCode, 0, '0000', exceptioncode
def MBPostRequest(self, Func, Addr, Qty, TID, UID, Message): # Check the parameters which represent the URL string. FunctionCode, Address, Quantity, TransID, UnitID = self._ValidateURLParams( Func, Addr, Qty, TID, UID) # Check if the values could be read without error. if ((FunctionCode == None) or (Address == None) or (Quantity == None) or (TransID == None) or (UnitID == None)): # Set some initial default values for these. TransID = -1 UnitID = -1 FunctionCode = 0 Address = 0 data = '0000' exceptioncode = 0 return TransID, UnitID, FunctionCode, Address, data, exceptioncode # Try to parse the XML message to get just the data portion of the message. XMLParser = RestXMLParser() if XMLParser.ParseRequest(Message): MsgData = XMLParser.GetMsgData() else: MsgData = '' # At this point, the parameters have all be checked to see if they can be used. # Now validate the data according to the requirements of each function. if (FunctionCode == 5): exceptioncode = 0 # Default exception code. # Check for address limits. if not self._AddrLimitsOK(Address, Quantity, FunctionCode): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 2 # Exception code. # Check if request data is valid. if MsgData not in ('0000', 'ff00', 'fF00', 'Ff00', 'FF00'): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. return TransID, UnitID, FunctionCode, Address, MsgData, exceptioncode elif (FunctionCode == 6): exceptioncode = 0 # Default exception code. # Check for address limits. if not self._AddrLimitsOK(Address, Quantity, FunctionCode): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 2 # Exception code. # Check if message length is OK. if (len(MsgData) != 4): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. else: # Try to convert to binary string. If no good, then # the string is not valid hexadecimal data. try: temp = ModbusDataStrLib.hex2bin(MsgData) except: FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. return TransID, UnitID, FunctionCode, Address, MsgData, exceptioncode elif (FunctionCode == 15): exceptioncode = 0 # Default exception code. # Check for address limits. if not self._AddrLimitsOK(Address, Quantity, FunctionCode): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 2 # Exception code. # Qty of outputs is out of range. elif ((Quantity < 1) or (Quantity > self._protocollimits[FunctionCode])): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Qty for coils exceeds the amount of data sent. elif (Quantity > len(MsgData)): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Qty for coils is less than the amount of data sent. elif (Quantity < (len(MsgData) - 8)): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Qty for coils exceeds amount of data sent. # Try to convert to binary string. If no good, then # the string is not valid hexadecimal data. # Length must also be a multiple of 8. else: try: temp = ModbusDataStrLib.bininversor(MsgData) except: FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. return TransID, UnitID, FunctionCode, Address, ( Quantity, MsgData), exceptioncode elif (FunctionCode == 16): exceptioncode = 0 # Default exception code. # Check for address limits. if not self._AddrLimitsOK(Address, Quantity, FunctionCode): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 2 # Exception code. # Qty of registers is out of range. elif ((Quantity < 1) or (Quantity > self._protocollimits[FunctionCode])): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Qty does not match the amount of data sent. elif ((Quantity * 4) != len(MsgData)): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. # Check if message length is OK. Length must be a multiple of 4. elif ((len(MsgData) % 4) != 0): FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. else: # Try to convert to binary string. If no good, then # the string is not valid hexadecimal data. try: temp = ModbusDataStrLib.hex2bin(MsgData) except: FunctionCode = FunctionCode + 128 # Create error code. exceptioncode = 3 # Exception code. return TransID, UnitID, FunctionCode, Address, ( Quantity, MsgData), exceptioncode # Function code is not supported. else: # This error code needs special treatment in case of bad function # codes that are very large. FunctionCode = (FunctionCode & 0xff) | 0x80 # Create error code. exceptioncode = 1 # Exception code. return TransID, UnitID, FunctionCode, 0, '0000', exceptioncode