本文整理汇总了Python中pymodbus.client.sync.ModbusTcpClient.execute方法的典型用法代码示例。如果您正苦于以下问题:Python ModbusTcpClient.execute方法的具体用法?Python ModbusTcpClient.execute怎么用?Python ModbusTcpClient.execute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymodbus.client.sync.ModbusTcpClient
的用法示例。
在下文中一共展示了ModbusTcpClient.execute方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ControlLink
# 需要导入模块: from pymodbus.client.sync import ModbusTcpClient [as 别名]
# 或者: from pymodbus.client.sync.ModbusTcpClient import execute [as 别名]
class ControlLink(object):
def __init__(self, host, port=Defaults.Port):
self.conn = ModbusTcpClient(host, port)
if not self.conn.connect():
raise RuntimeError('Could not connect to host %s port %d' % (host, port))
def read(self, offset):
# Adjust offset due to weirdness.
offset -= 40000
# Create request.
assert offset is not None
assert offset >= 0
req = ReadInputRegistersRequest(offset, 1)
assert req is not None
# Execute and return response.
res = self.conn.execute(req)
assert res is not None
# Extract single value from response.
values = res.registers
assert values is not None
assert len(values) == 1
return long(values[0])
def write(self, offset, value):
# Adjust offset due to weirdness.
offset -= 40000
# Create request.
assert offset is not None
assert offset >= 0
assert value is not None
req = WriteSingleRegisterRequest(offset, value)
# Execute and return response.
res = self.conn.execute(req)
assert res is not None
assert res.value is not None
nvalue = res.value
if nvalue != value:
report('address %d, wrote %d, returned %d'
% (offset, value, nvalue))
return nvalue
示例2: run_sync_client
# 需要导入模块: from pymodbus.client.sync import ModbusTcpClient [as 别名]
# 或者: from pymodbus.client.sync.ModbusTcpClient import execute [as 别名]
def run_sync_client():
# ------------------------------------------------------------------------#
# choose the client you want
# ------------------------------------------------------------------------#
# make sure to start an implementation to hit against. For this
# you can use an existing device, the reference implementation in the tools
# directory, or start a pymodbus server.
#
# If you use the UDP or TCP clients, you can override the framer being used
# to use a custom implementation (say RTU over TCP). By default they use
# the socket framer::
#
# client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
#
# It should be noted that you can supply an ipv4 or an ipv6 host address
# for both the UDP and TCP clients.
#
# There are also other options that can be set on the client that controls
# how transactions are performed. The current ones are:
#
# * retries - Specify how many retries to allow per transaction (default=3)
# * retry_on_empty - Is an empty response a retry (default = False)
# * source_address - Specifies the TCP source address to bind to
#
# Here is an example of using these options::
#
# client = ModbusClient('localhost', retries=3, retry_on_empty=True)
# ------------------------------------------------------------------------#
client = ModbusClient('localhost', port=5020)
# from pymodbus.transaction import ModbusRtuFramer
# client = ModbusClient('localhost', port=5020, framer=ModbusRtuFramer)
# client = ModbusClient(method='binary', port='/dev/ptyp0', timeout=1)
# client = ModbusClient(method='ascii', port='/dev/ptyp0', timeout=1)
# client = ModbusClient(method='rtu', port='/dev/ptyp0', timeout=1,
# baudrate=9600)
client.connect()
# ------------------------------------------------------------------------#
# specify slave to query
# ------------------------------------------------------------------------#
# The slave to query is specified in an optional parameter for each
# individual request. This can be done by specifying the `unit` parameter
# which defaults to `0x00`
# ----------------------------------------------------------------------- #
log.debug("Reading Device Information")
information = {}
rr = None
while not rr or rr.more_follows:
next_object_id = rr.next_object_id if rr else 0
rq = ReadDeviceInformationRequest(read_code=0x03, unit=UNIT,
object_id=next_object_id)
rr = client.execute(rq)
information.update(rr.information)
log.debug(rr)
print("Device Information : ")
for key in information.keys():
print(key, information[key])
# ----------------------------------------------------------------------- #
# You can also have the information parsed through the
# ModbusDeviceIdentificiation class, which gets you a more usable way
# to access the Basic and Regular device information objects which are
# specifically listed in the Modbus specification
# ----------------------------------------------------------------------- #
di = ModbusDeviceIdentification(info=information)
print('Product Name : ', di.ProductName)
# ----------------------------------------------------------------------- #
# close the client
# ----------------------------------------------------------------------- #
client.close()
示例3: ClearCountersRequest
# 需要导入模块: from pymodbus.client.sync import ModbusTcpClient [as 别名]
# 或者: from pymodbus.client.sync.ModbusTcpClient import execute [as 别名]
#
# request = ClearCountersRequest()
# response = client.execute(request)
# if isinstance(response, ClearCountersResponse):
# ... do something with the response
#
#
# What follows is a listing of all the supported methods. Feel free to
# comment, uncomment, or modify each result set to match with your reference.
#---------------------------------------------------------------------------#
#---------------------------------------------------------------------------#
# information requests
#---------------------------------------------------------------------------#
rq = ReadDeviceInformationRequest()
rr = client.execute(rq)
#assert(rr == None) # not supported by reference
assert(rr.function_code < 0x80) # test that we are not an error
assert(rr.information[0] == 'proconX Pty Ltd') # test the vendor name
assert(rr.information[1] == 'FT-MBSV') # test the product code
assert(rr.information[2] == 'EXPERIMENTAL') # test the code revision
rq = ReportSlaveIdRequest()
rr = client.execute(rq)
assert(rr == None) # not supported by reference
#assert(rr.function_code < 0x80) # test that we are not an error
#assert(rr.identifier == 0x00) # test the slave identifier
#assert(rr.status == 0x00) # test that the status is ok
rq = ReadExceptionStatusRequest()
rr = client.execute(rq)