def SetOutputsBool(self, addr, data): """Write outputs to the host (command 13). addr (integer) = SBus outputs address. data (string) = Packed binary string with the data to write. """ bindata = ModbusDataLib.boollist2bin([data]) self._SBusRequest(13, addr, 1, bindata)
def SetOutputsBool(self, addr, data): """Write outputs to the host (command 13). addr (integer) = SBus outputs address. data (string) = Packed binary string with the data to write. """ bindata = ModbusDataLib.boollist2bin([data]) self._SBusRequest(13, addr, 1, bindata)
def WriteServerData(cmd, adr, qty, data): if (cmd == 11): msgdata = ModbusDataLib.boollist2bin(data) elif (cmd == 14): msgdata = SBusMsg.signedint32list2bin(data) else: print('Bad command code %s when writing data.' % cmd) return MsgSeqCounter.incseq() # Send the request. client.SendRequest(MsgSeqCounter.getseq(), 1, cmd, qty, adr, msgdata) # Get the reply. TeleAttr, MsgSeq, MsgData = client.ReceiveResponse()
def WriteServerData(func, adr, qty, data): if (func == 15): msgdata = ModbusDataLib.boollist2bin(data) elif (func == 16): msgdata = ModbusDataLib.signedintlist2bin(data) else: print('Demosim - Bad function code %s when writing data.' % func) return try: # Send the request. client.SendRequest(1, 1, func, adr, qty, msgdata) # Get the reply. TransID, rfunct, Address, Qty, MsgData, Excode = client.ReceiveResponse() except: print('Demosim - lost contact with server - exiting') sys.exit()
def NextCommand(self, readtable, servercmd): """This should execute the next command, whatever it is. This is called regularly by GenClient. Parameters: readtable (dict) = A dictionary containing a mirror of the requested portion of the server data table. E.g. {'coil' : [True, False, True, True], 'inp' : [True, False, True, True], 'inpreg' : [], 'holdingreg' : [1234, 5678]} servercmd (string) = A command from the server. This will consist of one of the following: 'run' = Run normally. 'pause' = Pause operation. 'restart' = A restart is requested. The restart request will remain in effect until new parameters are loaded. Returns: dict, float = - A dictionary in the same format as the "readtable" input parameter. This will contain the data to be written to the server. - The time in seconds to wait before running this function again. Data table items in excess of those configured for transfer will be ignored. """ # First check if we've got a good parameter set. if not self._ConfigOK: # Set the connection status. self._ConnectStatus = 'stopped' nextpoll = 1.0 return self._WriteTable, nextpoll # Get the next command. try: cmdname, cmdvalue = self._CommandIter.next() nextpoll = self._CommandTime except StopIteration: self._CommandIter = iter(self._CommandList) cmdname, cmdvalue = self._CommandIter.next() nextpoll = self._RepeatTime # See what the command says we should do. A typical command looks like this: # [('action', 'poll'), ('stn', 1), ('cmd', 3), ('remoteaddr', 0), # ('qty', 10), ('datatype', 'coil'), ('dataoffset', 0)] cmdexp = dict(cmdvalue) cmd = cmdexp['cmd'] datatype = cmdexp['datatype'] dataoffset = cmdexp['dataoffset'] qty = cmdexp['qty'] # This is a write command, so we need data. # Write flags or outputs. if cmd in (11, 13): msgdata = ModbusDataLib.boollist2bin(readtable[datatype][dataoffset:dataoffset + qty]) # Write registers. SBus registers are 32 bit, so we need 2 Modbus # registers for each SBus register. elif cmd == 14: msgdata = ModbusDataLib.signedintlist2bin(readtable[datatype][dataoffset:dataoffset + (qty * 2)]) else: msgdata = '' # ##################################### # If we have an error, then bail out, close the socket and set an error # code. Set the client to None so we will re-initialise on the next round. # Create the client if necessary. if self._SBusClient == None: self._SBusClient = SBusSimpleClient.SBusSimpleClient(self._Host, self._Port, self._TimeOut) self._MsgSeq += 1 try: # Send the request. self._SBusClient.SendRequest(self._MsgSeq, cmdexp['stn'], cmd, qty, cmdexp['remoteaddr'], msgdata) # Get the response. telegramattr, recvmsgseq, recvdata = self._SBusClient.ReceiveResponse() # If we have an error, bail out here. except: self._SBusClient = None self._ConnectStatus = 'faulted' self._AddCmdStatus(cmdname, 'connection') # Exit here if error. return {}, 0.5 # ##################################### # This is a read command, so we need to save the data. # Read flags, inputs, or outputs. if (cmd in (2, 3, 5)) and (telegramattr == 1): booldata = ModbusDataLib.bin2boollist(recvdata) self._WriteTable[datatype][dataoffset:dataoffset + qty] = booldata[:qty] # Set the connection status. self._ConnectStatus = 'running' self._AddCmdStatus(cmdname, 'ok') # Read registers. elif (cmd == 6) and (telegramattr == 1): regdata = ModbusDataLib.signedbin2intlist(recvdata) # SBus registers are twice the size of Modbus registers. self._WriteTable[datatype][dataoffset:dataoffset + (qty * 2)] = regdata[:qty * 2] # Set the connection status. self._ConnectStatus = 'running' self._AddCmdStatus(cmdname, 'ok') # This was a write and the response was an ack or nak. elif (cmd in (11, 13, 14)) and (telegramattr == 2): # Decode the ack/nak acknak = ModbusDataLib.BinStr2SignedInt(recvdata) # The Ack was OK. if acknak != 0: self._ConnectStatus = 'faulted' self._AddCmdStatus(cmdname, 'deviceerr') else: # Set the connection status. self._ConnectStatus = 'running' self._AddCmdStatus(cmdname, 'ok') else: self._ConnectStatus = 'faulted' self._AddCmdStatus(cmdname, 'deviceerr') return self._WriteTable, nextpoll