本文整理汇总了Python中pymodbus.payload.BinaryPayloadBuilder.build方法的典型用法代码示例。如果您正苦于以下问题:Python BinaryPayloadBuilder.build方法的具体用法?Python BinaryPayloadBuilder.build怎么用?Python BinaryPayloadBuilder.build使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymodbus.payload.BinaryPayloadBuilder
的用法示例。
在下文中一共展示了BinaryPayloadBuilder.build方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testPayloadBuilderReset
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
def testPayloadBuilderReset(self):
""" Test basic bit message encoding/decoding """
builder = BinaryPayloadBuilder()
builder.add_8bit_uint(0x12)
builder.add_8bit_uint(0x34)
builder.add_8bit_uint(0x56)
builder.add_8bit_uint(0x78)
self.assertEqual(b'\x12\x34\x56\x78', builder.to_string())
self.assertEqual([b'\x12\x34', b'\x56\x78'], builder.build())
builder.reset()
self.assertEqual(b'', builder.to_string())
self.assertEqual([], builder.build())
示例2: testPayloadBuilderReset
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
def testPayloadBuilderReset(self):
''' Test basic bit message encoding/decoding '''
builder = BinaryPayloadBuilder()
builder.add_8bit_uint(0x12)
builder.add_8bit_uint(0x34)
builder.add_8bit_uint(0x56)
builder.add_8bit_uint(0x78)
self.assertEqual('\x12\x34\x56\x78', str(builder))
self.assertEqual(['\x12\x34', '\x56\x78'], builder.build())
builder.reset()
self.assertEqual('', str(builder))
self.assertEqual([], builder.build())
示例3: testPayloadBuilderReset
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
def testPayloadBuilderReset(self):
""" Test basic bit message encoding/decoding """
builder = BinaryPayloadBuilder()
builder.add_8bit_uint(0x12)
builder.add_8bit_uint(0x34)
builder.add_8bit_uint(0x56)
builder.add_8bit_uint(0x78)
self.assertEqual("\x12\x34\x56\x78", str(builder))
self.assertEqual(["\x12\x34", "\x56\x78"], builder.build())
builder.reset()
self.assertEqual("", str(builder))
self.assertEqual([], builder.build())
示例4: write_registers
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
def write_registers(self, key):
point = self.point_dict.get(key[2:])
if point:
value = conpot_core.get_databus().get_value(key)
if not point.encoding == 'none':
endian = Endian.Auto
if point.endian == 'Little':
endian = Endian.Little
elif point.endian == 'Big':
endian = Endian.Big
builder = BinaryPayloadBuilder(endian=endian)
builder_map = {'bits': builder.add_bits,
'8unit': builder.add_8bit_uint,
'16unit': builder.add_16bit_uint,
'32unit': builder.add_32bit_uint,
'64unit': builder.add_64bit_uint,
'8int': builder.add_8bit_int,
'16int': builder.add_16bit_int,
'32int': builder.add_32bit_int,
'64int': builder.add_64bit_int,
'32float': builder.add_32bit_float,
'64float': builder.add_64bit_float,
'string': builder.add_string}
builder_map[point.encoding](value)
payload = [unpack(endian + 'H', x)[0] for x in builder.build()]
with lock:
return self.modbus_client.write_registers(point.address,
payload,
unit=point.slave_id)
else:
with lock:
return self.modbus_client.write_registers(point.address, [value], unit=point.slave_id)
示例5: write_modbus
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
def write_modbus(solve_coo):
if solve_coo is None:
return False
ra = solve_coo.ra.deg
if ra > 180:
ra -= 360
ra = ra * np.pi/180.
dec = solve_coo.dec.deg * np.pi/180.
val_dict = {
24592: ra,
24590: dec,
24594: time.time() - 1544000000.
}
for address, value in val_dict.items():
builder = BinaryPayloadBuilder(byteorder=Endian.Big,
wordorder=Endian.Big)
print(address, value)
builder.add_32bit_float(value)
payload = builder.build()
registers = builder.to_registers()
rr = modbus_client.write_registers(address, registers, unit=modbus_UNIT)
time.sleep(0.1)
if rr.isError():
return False
return True
示例6: setUp
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
def setUp(self):
'''
Initializes the test environment and builds request/result
encoding pairs
'''
self.value = 0xabcd
self.values = [0xa, 0xb, 0xc]
builder = BinaryPayloadBuilder(endian=Endian.Big)
builder.add_16bit_uint(0x1234)
self.payload = builder.build()
self.write = {
WriteSingleRegisterRequest(1, self.value) : b'\x00\x01\xab\xcd',
WriteSingleRegisterResponse(1, self.value) : b'\x00\x01\xab\xcd',
WriteMultipleRegistersRequest(1, self.values) : b'\x00\x01\x00\x03\x06\x00\n\x00\x0b\x00\x0c',
WriteMultipleRegistersResponse(1, 5) : b'\x00\x01\x00\x05',
WriteSingleRegisterRequest(1, self.payload[0], skip_encode=True): b'\x00\x01\x12\x34',
WriteMultipleRegistersRequest(1, self.payload, skip_encode=True): b'\x00\x01\x00\x01\x02\x12\x34',
}
示例7: BinaryPayloadBuilder
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
# 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:
#
# - 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]
示例8: run_binary_payload_ex
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [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")
#.........这里部分代码省略.........
示例9: write
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
def write(self):
try:
lb = 00000000
hb = 00000000
#### byte besteht immer aus 16 bits
for byte in self._db['out']:
for bit in sorted(self._db['out'][byte]):
if bit in self._db['out'][byte]:
bitpos = bit[0] #startbit/bitposition des binärwertes
type = bit[1]
value = bit[2]
name = bit[3]
bit[2] = bit[3]() ##aktueller wert des items abrufen und value updaten!
builder = BinaryPayloadBuilder(endian=Endian.Little)
##unterscheidung dateityp
if type == '5' or type == '5.001' or type == '6' : ##8bit uint / int
length = 8
if bitpos < 8: #lb
lb = value
else: #hb
hb = value
if type == '5':
builder.add_8bit_uint(lb)
builder.add_8bit_uint(hb)
#logger.debug('MODBUS: 8bit uint {0} ; {1}'.format(lb,hb))
elif type == '5.001': ##0-100 in 0-255 umwandeln!
#print(dpts.en5001(lb))
#print(dpts.en5001(hb))
lb = self.de5001(lb)
hb = self.de5001(hb)
#print("lb geschrieben", lb )
#print("hb geschrieben", hb )
builder.add_8bit_uint(lb)
builder.add_8bit_uint(hb)
#logger.debug('MODBUS: 8bit uint {0} ; {1}'.format(lb,hb))
elif type == '6':
if lb > 127:
lb = 127
elif lb < -128:
lb = -128
if hb > 127:
hb = 127
elif hb < -128:
hb = -128
builder.add_8bit_int(lb)
builder.add_8bit_int(hb)
#logger.debug('MODBUS: 8bit int {0} ; {1}'.format(lb.hb))
elif type == '7' or type == '8': #16bit uint / int
length = 16
if type == '7': #0...65535
builder.add_16bit_uint(value)
#logger.debug('MODBUS: 16bit uint {0} '.format(value))
else: #-32768...32767
builder.add_16bit_int(value)
#logger.debug('MODBUS: 16bit int {0}'.format(value))
elif type == '1':
length = 1
#nur pro byte einmal die bits wandeln
if bitpos < 8: #lb
lb = lb | int(value) << bitpos
#logger.debug('MODBUS: 8bit int{0}'.format(lb))
else: #hb
hb = hb | int(value) << bitpos
#logger.debug('MODBUS: 8bit int{0}'.format(hb))
builder.add_8bit_uint(lb)
builder.add_8bit_uint(hb)
payload = builder.build()
logger.debug('MODBUS: write to PLC: WORD {0} set to {1} '.format(byte,payload))
self._modbuspy.write_registers(byte, payload, skip_encode=True)
builder.reset()
except Exception as e:
logger.error('MODBUS: Could not write an OutWord, because {}'.format(e))
self._lock.release()
return None
示例10: byteswap
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import build [as 别名]
endian=client.read_holding_registers(49991,4,unit=1)
#endian.registers[0] = byteswap(endian.registers[0])
#endian.registers[1] = byteswap(endian.registers[1])
#endian.registers[2] = byteswap(endian.registers[2])
#endian.registers[3] = byteswap(endian.registers[3])
decoder=BinaryPayloadDecoder.fromRegisters(endian.registers,endian=Endian.Big)
decoded={
'val':hex(decoder.decode_32bit_uint()),
'v100':decoder.decode_32bit_float()
}
print decoded
encoder = BinaryPayloadBuilder(endian=Endian.Big)
encoder.add_16bit_uint(0x1234)
buf = encoder.build()
#buf[0] = charswap(buf[0])
client.write_registers(51234, buf, unit=1, skip_encode=True)
encoder = BinaryPayloadBuilder(endian=Endian.Big)
encoder.add_32bit_float(1.0114)
encoder.add_32bit_float(-6)
buf = encoder.build()
#buf[0] = charswap(buf[0])
#buf[1] = charswap(buf[1])
#buf[2] = charswap(buf[2])
#buf[3] = charswap(buf[3])
client.write_registers(50000 + 8 * 5, buf, unit=1, skip_encode=True)
while True:
result = client.read_input_registers(100, 6, unit=1)