本文整理汇总了Python中pymodbus.payload.BinaryPayloadBuilder.add_16bit_int方法的典型用法代码示例。如果您正苦于以下问题:Python BinaryPayloadBuilder.add_16bit_int方法的具体用法?Python BinaryPayloadBuilder.add_16bit_int怎么用?Python BinaryPayloadBuilder.add_16bit_int使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pymodbus.payload.BinaryPayloadBuilder
的用法示例。
在下文中一共展示了BinaryPayloadBuilder.add_16bit_int方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_payload_server
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_int [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_int [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_int [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_int [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: run_binary_payload_ex
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_int [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=5020)
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.Big,
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(0x12345678DEADBEEF)
builder.add_64bit_float(123.45)
builder.add_64bit_float(-123.45)
payload = builder.to_registers()
print("-" * 60)
print("Writing Registers")
print("-" * 60)
print(payload)
print("\n")
payload = builder.build()
address = 0
# Can write registers
# registers = builder.to_registers()
# client.write_registers(address, registers, unit=1)
# Or can write encoded binary string
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 = 0x0
count = len(payload)
result = client.read_holding_registers(address, count, unit=1)
print("-" * 60)
print("Registers")
print("-" * 60)
print(result.registers)
print("\n")
#.........这里部分代码省略.........
示例6: write
# 需要导入模块: from pymodbus.payload import BinaryPayloadBuilder [as 别名]
# 或者: from pymodbus.payload.BinaryPayloadBuilder import add_16bit_int [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