Exemple #1
0
    def GetInputRegistersIntList(self, addr, qty):
        """Read input registers from the host (function 4).
		addr (integer) = Modbus discrete inputs address.
		qty (integer) = Number of registers.
		"""
        return ModbusDataLib.signedbin2intlist(
            self._ModbusRequest(4, addr, qty, None))
def GetServerData(func, adr, qty):
	try:
		# Send the request.
		client.SendRequest(1, 1, func, adr, qty)
		# Get the reply.
		TransID, rfunct, Address, Qty, MsgData, Excode = client.ReceiveResponse()
	except:
		print('Demosim - lost contact with server - exiting')
		sys.exit()

	# Decode the data.
	if (rfunct in [1, 2]):
		values = ModbusDataLib.bin2boollist(MsgData)
	elif (rfunct in [3, 4]):
		values = ModbusDataLib.signedbin2intlist(MsgData)
	else:
		print('Demosim - Bad function code %s when reading data.' % rfunct)
		# Provide a default based on the sending function.
		if (func in [1, 2]):
			values = [False] * 8
		elif (funct in [3, 4]):
			values = [0] * 8
	return values
	def GetInputRegistersIntList(self, addr, qty):
		"""Read input registers from the host (function 4).
		addr (integer) = Modbus discrete inputs address.
		qty (integer) = Number of registers.
		"""
		return ModbusDataLib.signedbin2intlist(self._ModbusRequest(4, addr, qty, None))
	def GetHoldingRegistersInt(self, addr):
		"""Read holding registers from the host (function 3).
		addr (integer) = Modbus discrete inputs address.
		"""
		return ModbusDataLib.signedbin2intlist(self._ModbusRequest(3, addr, 1, None))[0]
Exemple #5
0
	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
Exemple #6
0
    def GetHoldingRegistersInt(self, addr):
        """Read holding registers from the host (function 3).
		addr (integer) = Modbus discrete inputs address.
		"""
        return ModbusDataLib.signedbin2intlist(
            self._ModbusRequest(3, addr, 1, None))[0]