0

client = snap.client.Client()
client.connect('XXX.XXX.X.XXX', 0, 2)  #IP address, rack, slot

db = client.db_get(20)
print(db)

intDB = []

for i in range(1, 122):
    reading = client.db_read(20, i, 1)
    realReading = snap.util.get_real(reading, 0)
    array = [realReading]
    intDB.append(array)

print(intDB)

此代码应该以字节数组形式打印 DB,然后使用 PLC 输出的浮点值打印数组。但是,当我运行代码时,我收到以下错误消息:

Traceback (most recent call last):
  File "C:/Users/Asus/PycharmProjects/PLC-Connection/main.py", line 19, in <module>
    realReading = snap.util.get_real(reading, 0)
  File "C:\Users\Asus\PycharmProjects\PLC-Connection\venv\lib\site-packages\snap7\util.py", line 357, in get_real
    real = struct.unpack('>f', struct.pack('4B', *x))[0]
struct.error: pack expected 4 items for packing (got 1)
4

1 回答 1

0

我认为问题在于读取数据:

reading = client.db_read(20, i, 1)

20是数据块,i是索引,你只读1字节(第三个参数)。

要检索真实数据,您需要读出 4 个字节。

于 2022-01-14T15:00:29.160 回答