本文整理汇总了Python中pymodbus.payload.BinaryPayloadBuilder.add_string方法的典型用法代码示例。如果您正苦于以下问题:Python BinaryPayloadBuilder.add_string方法的具体用法?Python BinaryPayloadBuilder.add_string怎么用?Python BinaryPayloadBuilder.add_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymodbus.payload.BinaryPayloadBuilder
的用法示例。
在下文中一共展示了BinaryPayloadBuilder.add_string方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_payload_server
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_string [as 别名]
def run_payload_server():
# ----------------------------------------------------------------------- #
# build your payload
# ----------------------------------------------------------------------- #
builder = BinaryPayloadBuilder(byteorder=Endian.Little,
wordorder=Endian.Little)
builder.add_string('abcdefgh')
builder.add_bits([0, 1, 0, 1, 1, 0, 1, 0])
builder.add_8bit_int(-0x12)
builder.add_8bit_uint(0x12)
builder.add_16bit_int(-0x5678)
builder.add_16bit_uint(0x1234)
builder.add_32bit_int(-0x1234)
builder.add_32bit_uint(0x12345678)
builder.add_32bit_float(22.34)
builder.add_32bit_float(-22.34)
builder.add_64bit_int(-0xDEADBEEF)
builder.add_64bit_uint(0x12345678DEADBEEF)
builder.add_64bit_uint(0xDEADBEEFDEADBEED)
builder.add_64bit_float(123.45)
builder.add_64bit_float(-123.45)
# ----------------------------------------------------------------------- #
# use that payload in the data store
# ----------------------------------------------------------------------- #
# Here we use the same reference block for each underlying store.
# ----------------------------------------------------------------------- #
block = ModbusSequentialDataBlock(1, builder.to_registers())
store = ModbusSlaveContext(di=block, co=block, hr=block, ir=block)
context = ModbusServerContext(slaves=store, single=True)
# ----------------------------------------------------------------------- #
# initialize the server information
# ----------------------------------------------------------------------- #
# If you don't set this or any fields, they are defaulted to empty strings.
# ----------------------------------------------------------------------- #
identity = ModbusDeviceIdentification()
identity.VendorName = 'Pymodbus'
identity.ProductCode = 'PM'
identity.VendorUrl = 'http://github.com/bashwork/pymodbus/'
identity.ProductName = 'Pymodbus Server'
identity.ModelName = 'Pymodbus Server'
identity.MajorMinorRevision = '1.5'
# ----------------------------------------------------------------------- #
# run the server you want
# ----------------------------------------------------------------------- #
StartTcpServer(context, identity=identity, address=("localhost", 5020))
示例2: testBigEndianPayloadBuilder
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_string [as 别名]
def testBigEndianPayloadBuilder(self):
''' Test basic bit message encoding/decoding '''
builder = BinaryPayloadBuilder(endian=Endian.Big)
builder.add_8bit_uint(1)
builder.add_16bit_uint(2)
builder.add_32bit_uint(3)
builder.add_64bit_uint(4)
builder.add_8bit_int(-1)
builder.add_16bit_int(-2)
builder.add_32bit_int(-3)
builder.add_64bit_int(-4)
builder.add_32bit_float(1.25)
builder.add_64bit_float(6.25)
builder.add_string(b'test')
builder.add_bits(self.bitstring)
self.assertEqual(self.big_endian_payload, builder.to_string())
示例3: testLittleEndianPayloadBuilder
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_string [as 别名]
def testLittleEndianPayloadBuilder(self):
''' Test basic bit message encoding/decoding '''
builder = BinaryPayloadBuilder(endian=Endian.Little)
builder.add_8bit_uint(1)
builder.add_16bit_uint(2)
builder.add_32bit_uint(3)
builder.add_64bit_uint(4)
builder.add_8bit_int(-1)
builder.add_16bit_int(-2)
builder.add_32bit_int(-3)
builder.add_64bit_int(-4)
builder.add_32bit_float(1.25)
builder.add_64bit_float(6.25)
builder.add_string('test')
builder.add_bits(self.bitstring)
self.assertEqual(self.little_endian_payload, str(builder))
示例4: testLittleEndianPayloadBuilder
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_string [as 别名]
def testLittleEndianPayloadBuilder(self):
""" Test basic bit message encoding/decoding """
builder = BinaryPayloadBuilder(byteorder=Endian.Little,
wordorder=Endian.Little)
builder.add_8bit_uint(1)
builder.add_16bit_uint(2)
builder.add_32bit_uint(3)
builder.add_64bit_uint(4)
builder.add_8bit_int(-1)
builder.add_16bit_int(-2)
builder.add_32bit_int(-3)
builder.add_64bit_int(-4)
builder.add_32bit_float(1.25)
builder.add_64bit_float(6.25)
builder.add_16bit_uint(1) # placeholder
builder.add_string(b'test')
builder.add_bits(self.bitstring)
self.assertEqual(self.little_endian_payload, builder.to_string())
示例5: BinaryPayloadBuilder
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_string [as 别名]
# ---------------------------------------------------------------------------#
# If you need to build a complex message to send, you can use the payload
# builder to simplify the packing logic.
#
# Here we demonstrate packing a random payload layout, unpacked it looks
# like the following:
#
# - a 8 byte string 'abcdefgh'
# - a 32 bit float 22.34
# - a 16 bit unsigned int 0x1234
# - an 8 bit int 0x12
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
# ---------------------------------------------------------------------------#
builder = BinaryPayloadBuilder(endian=Endian.Little)
builder.add_string("abcdefgh")
builder.add_32bit_float(22.34)
builder.add_16bit_uint(0x1234)
builder.add_8bit_int(0x12)
builder.add_bits([0, 1, 0, 1, 1, 0, 1, 0])
payload = builder.build()
address = 0x01
result = client.write_registers(address, payload, skip_encode=True)
# ---------------------------------------------------------------------------#
# If you need to decode a collection of registers in a weird layout, the
# payload decoder can help you as well.
#
# Here we demonstrate decoding a random register layout, unpacked it looks
# like the following:
#
示例6: run_binary_payload_ex
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_string [as 别名]
def run_binary_payload_ex():
# ----------------------------------------------------------------------- #
# We are going to use a simple client to send our requests
# ----------------------------------------------------------------------- #
client = ModbusClient('127.0.0.1', port=5440)
client.connect()
# ----------------------------------------------------------------------- #
# If you need to build a complex message to send, you can use the payload
# builder to simplify the packing logic.
#
# Here we demonstrate packing a random payload layout, unpacked it looks
# like the following:
#
# - a 8 byte string 'abcdefgh'
# - a 32 bit float 22.34
# - a 16 bit unsigned int 0x1234
# - another 16 bit unsigned int 0x5678
# - an 8 bit int 0x12
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
# - an 32 bit uint 0x12345678
# - an 32 bit signed int -0x1234
# - an 64 bit signed int 0x12345678
# The packing can also be applied to the word (wordorder) and bytes in each
# word (byteorder)
# The wordorder is applicable only for 32 and 64 bit values
# Lets say we need to write a value 0x12345678 to a 32 bit register
# The following combinations could be used to write the register
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
# Word Order - Big Byte Order - Big
# word1 =0x1234 word2 = 0x5678
# Word Order - Big Byte Order - Little
# word1 =0x3412 word2 = 0x7856
# Word Order - Little Byte Order - Big
# word1 = 0x5678 word2 = 0x1234
# Word Order - Little Byte Order - Little
# word1 =0x7856 word2 = 0x3412
# +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #
# ----------------------------------------------------------------------- #
builder = BinaryPayloadBuilder(byteorder=Endian.Little,
wordorder=Endian.Big)
builder.add_string('abcdefgh')
builder.add_32bit_float(22.34)
builder.add_16bit_uint(0x1234)
builder.add_16bit_uint(0x5678)
builder.add_8bit_int(0x12)
builder.add_bits([0, 1, 0, 1, 1, 0, 1, 0])
builder.add_32bit_uint(0x12345678)
builder.add_32bit_int(-0x1234)
builder.add_64bit_int(0x1234567890ABCDEF)
payload = builder.build()
address = 0
client.write_registers(address, payload, skip_encode=True, unit=1)
# ----------------------------------------------------------------------- #
# If you need to decode a collection of registers in a weird layout, the
# payload decoder can help you as well.
#
# Here we demonstrate decoding a random register layout, unpacked it looks
# like the following:
#
# - a 8 byte string 'abcdefgh'
# - a 32 bit float 22.34
# - a 16 bit unsigned int 0x1234
# - another 16 bit unsigned int which we will ignore
# - an 8 bit int 0x12
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
# ----------------------------------------------------------------------- #
address = 0x00
count = len(payload)
result = client.read_holding_registers(address, count, unit=1)
print("-" * 60)
print("Registers")
print("-" * 60)
print(result.registers)
print("\n")
decoder = BinaryPayloadDecoder.fromRegisters(result.registers,
byteorder=Endian.Little,
wordorder=Endian.Big)
decoded = {
'string': decoder.decode_string(8),
'float': decoder.decode_32bit_float(),
'16uint': decoder.decode_16bit_uint(),
'ignored': decoder.skip_bytes(2),
'8int': decoder.decode_8bit_int(),
'bits': decoder.decode_bits(),
"32uints": decoder.decode_32bit_uint(),
"32ints": decoder.decode_32bit_int(),
"64ints": decoder.decode_64bit_int(),
}
print("-" * 60)
print("Decoded Data")
#.........这里部分代码省略.........
示例7: BinaryPayloadBuilder
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_string [as 别名]
#---------------------------------------------------------------------------#
# If you need to build a complex message to send, you can use the payload
# builder to simplify the packing logic.
#
# Here we demonstrate packing a random payload layout, unpacked it looks
# like the following:
#
# - a 8 byte string 'abcdefgh'
# - a 32 bit float 22.34
# - a 16 bit unsigned int 0x1234
# - an 8 bit int 0x12
# - an 8 bit bitstring [0,1,0,1,1,0,1,0]
#---------------------------------------------------------------------------#
builder = BinaryPayloadBuilder(endian=Endian.Little)
builder.add_string('abcdefgh')
builder.add_32bit_float(22.34)
builder.add_16bit_uint(0x1234)
builder.add_8bit_int(0x12)
builder.add_bits([0,1,0,1,1,0,1,0])
payload = builder.build()
address = 0x01
result = client.write_registers(address, payload, skip_encode=True)
#---------------------------------------------------------------------------#
# If you need to decode a collection of registers in a weird layout, the
# payload decoder can help you as well.
#
# Here we demonstrate decoding a random register layout, unpacked it looks
# like the following:
#