|
23 | 23 | log = logging.getLogger() |
24 | 24 | log.setLevel(logging.INFO) |
25 | 25 |
|
| 26 | +ORDER_DICT = { |
| 27 | + "<": "LITTLE", |
| 28 | + ">": "BIG" |
| 29 | +} |
| 30 | + |
26 | 31 |
|
27 | 32 | def run_binary_payload_ex(): |
28 | 33 | # ----------------------------------------------------------------------- # |
@@ -71,97 +76,104 @@ def run_binary_payload_ex(): |
71 | 76 | # +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # |
72 | 77 |
|
73 | 78 | # ----------------------------------------------------------------------- # |
74 | | - builder = BinaryPayloadBuilder(byteorder=Endian.Big, |
75 | | - wordorder=Endian.Little) |
76 | | - builder.add_string('abcdefgh') |
77 | | - builder.add_bits([0, 1, 0, 1, 1, 0, 1, 0]) |
78 | | - builder.add_8bit_int(-0x12) |
79 | | - builder.add_8bit_uint(0x12) |
80 | | - builder.add_16bit_int(-0x5678) |
81 | | - builder.add_16bit_uint(0x1234) |
82 | | - builder.add_32bit_int(-0x1234) |
83 | | - builder.add_32bit_uint(0x12345678) |
84 | | - builder.add_16bit_float(12.34) |
85 | | - builder.add_16bit_float(-12.34) |
86 | | - builder.add_32bit_float(22.34) |
87 | | - builder.add_32bit_float(-22.34) |
88 | | - builder.add_64bit_int(-0xDEADBEEF) |
89 | | - builder.add_64bit_uint(0x12345678DEADBEEF) |
90 | | - builder.add_64bit_uint(0x12345678DEADBEEF) |
91 | | - builder.add_64bit_float(123.45) |
92 | | - builder.add_64bit_float(-123.45) |
93 | | - payload = builder.to_registers() |
94 | | - print("-" * 60) |
95 | | - print("Writing Registers") |
96 | | - print("-" * 60) |
97 | | - print(payload) |
98 | | - print("\n") |
99 | | - payload = builder.build() |
100 | | - address = 0 |
101 | | - # Can write registers |
102 | | - # registers = builder.to_registers() |
103 | | - # client.write_registers(address, registers, unit=1) |
104 | | - |
105 | | - # Or can write encoded binary string |
106 | | - client.write_registers(address, payload, skip_encode=True, unit=1) |
107 | | - # ----------------------------------------------------------------------- # |
108 | | - # If you need to decode a collection of registers in a weird layout, the |
109 | | - # payload decoder can help you as well. |
110 | | - # |
111 | | - # Here we demonstrate decoding a random register layout, unpacked it looks |
112 | | - # like the following: |
113 | | - # |
114 | | - # - a 8 byte string 'abcdefgh' |
115 | | - # - a 32 bit float 22.34 |
116 | | - # - a 16 bit unsigned int 0x1234 |
117 | | - # - another 16 bit unsigned int which we will ignore |
118 | | - # - an 8 bit int 0x12 |
119 | | - # - an 8 bit bitstring [0,1,0,1,1,0,1,0] |
120 | | - # ----------------------------------------------------------------------- # |
121 | | - address = 0x0 |
122 | | - count = len(payload) |
123 | | - result = client.read_holding_registers(address, count, unit=1) |
124 | | - print("-" * 60) |
125 | | - print("Registers") |
126 | | - print("-" * 60) |
127 | | - print(result.registers) |
128 | | - print("\n") |
129 | | - decoder = BinaryPayloadDecoder.fromRegisters(result.registers, |
130 | | - byteorder=Endian.Big, |
131 | | - wordorder=Endian.Little) |
132 | | - |
133 | | - assert decoder._byteorder == builder._byteorder, \ |
134 | | - "Make sure byteorder is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder" |
135 | | - |
136 | | - assert decoder._wordorder == builder._wordorder, \ |
137 | | - "Make sure wordorder is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder" |
138 | | - |
139 | | - |
140 | | - decoded = OrderedDict([ |
141 | | - ('string', decoder.decode_string(8)), |
142 | | - ('bits', decoder.decode_bits()), |
143 | | - ('8int', decoder.decode_8bit_int()), |
144 | | - ('8uint', decoder.decode_8bit_uint()), |
145 | | - ('16int', decoder.decode_16bit_int()), |
146 | | - ('16uint', decoder.decode_16bit_uint()), |
147 | | - ('32int', decoder.decode_32bit_int()), |
148 | | - ('32uint', decoder.decode_32bit_uint()), |
149 | | - ('16float', decoder.decode_16bit_float()), |
150 | | - ('16float2', decoder.decode_16bit_float()), |
151 | | - ('32float', decoder.decode_32bit_float()), |
152 | | - ('32float2', decoder.decode_32bit_float()), |
153 | | - ('64int', decoder.decode_64bit_int()), |
154 | | - ('64uint', decoder.decode_64bit_uint()), |
155 | | - ('ignore', decoder.skip_bytes(8)), |
156 | | - ('64float', decoder.decode_64bit_float()), |
157 | | - ('64float2', decoder.decode_64bit_float()), |
158 | | - ]) |
159 | | - |
160 | | - print("-" * 60) |
161 | | - print("Decoded Data") |
162 | | - print("-" * 60) |
163 | | - for name, value in iteritems(decoded): |
164 | | - print("%s\t" % name, hex(value) if isinstance(value, int) else value) |
| 79 | + combos = [(wo, bo) for wo in [Endian.Big, Endian.Little] for bo in [Endian.Big, Endian.Little]] |
| 80 | + for wo, bo in combos: |
| 81 | + print("-" * 60) |
| 82 | + print("Word Order: {}".format(ORDER_DICT[wo])) |
| 83 | + print("Byte Order: {}".format(ORDER_DICT[bo])) |
| 84 | + print() |
| 85 | + builder = BinaryPayloadBuilder(byteorder=bo, |
| 86 | + wordorder=wo) |
| 87 | + strng = "abcdefgh" |
| 88 | + builder.add_string(strng) |
| 89 | + builder.add_bits([0, 1, 0, 1, 1, 0, 1, 0]) |
| 90 | + builder.add_8bit_int(-0x12) |
| 91 | + builder.add_8bit_uint(0x12) |
| 92 | + builder.add_16bit_int(-0x5678) |
| 93 | + builder.add_16bit_uint(0x1234) |
| 94 | + builder.add_32bit_int(-0x1234) |
| 95 | + builder.add_32bit_uint(0x12345678) |
| 96 | + builder.add_16bit_float(12.34) |
| 97 | + builder.add_16bit_float(-12.34) |
| 98 | + builder.add_32bit_float(22.34) |
| 99 | + builder.add_32bit_float(-22.34) |
| 100 | + builder.add_64bit_int(-0xDEADBEEF) |
| 101 | + builder.add_64bit_uint(0x12345678DEADBEEF) |
| 102 | + builder.add_64bit_uint(0x12345678DEADBEEF) |
| 103 | + builder.add_64bit_float(123.45) |
| 104 | + builder.add_64bit_float(-123.45) |
| 105 | + payload = builder.to_registers() |
| 106 | + print("-" * 60) |
| 107 | + print("Writing Registers") |
| 108 | + print("-" * 60) |
| 109 | + print(payload) |
| 110 | + print("\n") |
| 111 | + payload = builder.build() |
| 112 | + address = 0 |
| 113 | + # Can write registers |
| 114 | + # registers = builder.to_registers() |
| 115 | + # client.write_registers(address, registers, unit=1) |
| 116 | + |
| 117 | + # Or can write encoded binary string |
| 118 | + client.write_registers(address, payload, skip_encode=True, unit=1) |
| 119 | + # ----------------------------------------------------------------------- # |
| 120 | + # If you need to decode a collection of registers in a weird layout, the |
| 121 | + # payload decoder can help you as well. |
| 122 | + # |
| 123 | + # Here we demonstrate decoding a random register layout, unpacked it looks |
| 124 | + # like the following: |
| 125 | + # |
| 126 | + # - a 8 byte string 'abcdefgh' |
| 127 | + # - a 32 bit float 22.34 |
| 128 | + # - a 16 bit unsigned int 0x1234 |
| 129 | + # - another 16 bit unsigned int which we will ignore |
| 130 | + # - an 8 bit int 0x12 |
| 131 | + # - an 8 bit bitstring [0,1,0,1,1,0,1,0] |
| 132 | + # ----------------------------------------------------------------------- # |
| 133 | + address = 0x0 |
| 134 | + count = len(payload) |
| 135 | + result = client.read_holding_registers(address, count, unit=1) |
| 136 | + print("-" * 60) |
| 137 | + print("Registers") |
| 138 | + print("-" * 60) |
| 139 | + print(result.registers) |
| 140 | + print("\n") |
| 141 | + decoder = BinaryPayloadDecoder.fromRegisters(result.registers, |
| 142 | + byteorder=bo, |
| 143 | + wordorder=wo) |
| 144 | + |
| 145 | + assert decoder._byteorder == builder._byteorder, \ |
| 146 | + "Make sure byteorder is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder" |
| 147 | + |
| 148 | + assert decoder._wordorder == builder._wordorder, \ |
| 149 | + "Make sure wordorder is consistent between BinaryPayloadBuilder and BinaryPayloadDecoder" |
| 150 | + |
| 151 | + |
| 152 | + decoded = OrderedDict([ |
| 153 | + ('string', decoder.decode_string(len(strng))), |
| 154 | + ('bits', decoder.decode_bits()), |
| 155 | + ('8int', decoder.decode_8bit_int()), |
| 156 | + ('8uint', decoder.decode_8bit_uint()), |
| 157 | + ('16int', decoder.decode_16bit_int()), |
| 158 | + ('16uint', decoder.decode_16bit_uint()), |
| 159 | + ('32int', decoder.decode_32bit_int()), |
| 160 | + ('32uint', decoder.decode_32bit_uint()), |
| 161 | + ('16float', decoder.decode_16bit_float()), |
| 162 | + ('16float2', decoder.decode_16bit_float()), |
| 163 | + ('32float', decoder.decode_32bit_float()), |
| 164 | + ('32float2', decoder.decode_32bit_float()), |
| 165 | + ('64int', decoder.decode_64bit_int()), |
| 166 | + ('64uint', decoder.decode_64bit_uint()), |
| 167 | + ('ignore', decoder.skip_bytes(8)), |
| 168 | + ('64float', decoder.decode_64bit_float()), |
| 169 | + ('64float2', decoder.decode_64bit_float()), |
| 170 | + ]) |
| 171 | + |
| 172 | + print("-" * 60) |
| 173 | + print("Decoded Data") |
| 174 | + print("-" * 60) |
| 175 | + for name, value in iteritems(decoded): |
| 176 | + print("%s\t" % name, hex(value) if isinstance(value, int) else value) |
165 | 177 |
|
166 | 178 | # ----------------------------------------------------------------------- # |
167 | 179 | # close the client |
|
0 commit comments