本文整理汇总了Python中pymodbus.payload.BinaryPayloadBuilder.add_16bit_uint方法的典型用法代码示例。如果您正苦于以下问题:Python BinaryPayloadBuilder.add_16bit_uint方法的具体用法?Python BinaryPayloadBuilder.add_16bit_uint怎么用?Python BinaryPayloadBuilder.add_16bit_uint使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymodbus.payload.BinaryPayloadBuilder
的用法示例。
在下文中一共展示了BinaryPayloadBuilder.add_16bit_uint方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_payload_server
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_uint [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_16bit_uint [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_16bit_uint [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_16bit_uint [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: setUp
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_uint [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',
}
示例6: BinaryPayloadBuilder
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_uint [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:
#
# - a 8 byte string 'abcdefgh'
# - a 32 bit float 22.34
示例7: run_binary_payload_ex
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_uint [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")
#.........这里部分代码省略.........
示例8: write
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_uint [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
示例9: byteswap
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_uint [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: