西门子标志! PLC 数据顺序错误

Siemens LOGO! PLC data in the wrong order

所以我正在访问西门子徽标! PLC 从中提取一些数据。我设法与我的工作伙伴一起做到了这一点,但我们对如何保存数据感到困惑。数据在字典中加上时间戳,其中包含来自 PLC 的输出和输入字节。但是数据是按一定顺序出现的,并且在提取数据的那一刻就被加上了时间戳。

现在的问题是时间戳和数据的保存顺序与数据进来的顺序不一样。在这个过程中的某个地方出错了(我认为),但我们似乎找不到它.

这是我们使用的 Python 代码:

# initial testing to connect to a PLC
#connects to PLC save I/O into json format with timestamp
import snap7
import time
import ctypes
import math
import json

PLCip = "x.x.x.x" #input  ip
Port = 000 # input port
Rack = 0
Slot = 1
size_to_read = 100
datadb = (ctypes.c_uint8 * size_to_read)()

#creates client and connect to plc, returns true if connected
Client = snap7.client.Client()
Client.connect(PLCip, Rack, Slot)
print("Connected:", Client.get_connected())

#converts sum INT to bits
def access_bit(data, num):
    base = int(num // 8)
    shift = int(num % 8)
    return (data[base] & (1<<shift)) >> shift

#empty dict for storing I/O data
mydata = {}

#save data dict to json format
def save_to_file(stamp, input, output, file):
    mydata[stamp] = {'input': input, 'output': output}
    with open(file,'a') as f:
        json.dump(mydata, f)

while True:
    # ts = time.time()    #gets current time 
    _now = time.time()
    ts = time.localtime(_now)
    # timestamp = (time.strftime('%H:%M:%S',ts),str('%.3f'%_now).split('.')[1])
    timestamp = time.strftime('%X',ts)
    datestamp = time.strftime('%d-%m-%Y',ts)
    log_folder='logs/'
    color=['black/','orange/','metal/']

    #json_file = str(log_folder)+str(datestamp)+'-'+str(timestamp)+'.json'
    json_file = str(log_folder)+str(color[2])+str(datestamp)+'.json'

    data = Client.eb_read(0,2)  # reads input data from 2 bytes
    data_input = format(int.from_bytes(data, "little"), "b")    #converts from int to bits

    outputdata = Client.ab_read(0,2)    #reads output data from 2 bytes
    data_output = format(int.from_bytes(outputdata, "little"), "b")    #converts from int to bits

    #save it to json file with parameters
    save_to_file(timestamp,data_input,data_output, json_file)
    #print to cli for debugging
    print_text= "Time: {}\nInput: {}\nOutput: {}\n".format(timestamp,data_input,data_output)
    print(print_text)
    #wait 1 second and restart
    time.sleep(1)

而我们得到的数据:

{"13:41:57": {"input": "1001010000", "output": "0"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}, "13:41:59": {"input": "1001010000", "output": "0"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}, "13:41:59": {"input": "1001010000", "output": "0"}, "13:42:00": {"input": "1001010000", "output": "0"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}, "13:41:59": {"input": "1001010000", "output": "0"}, "13:42:00": {"input": "1001010000", "output": "0"}, "13:42:01": {"input": "1001010000", "output": "1001"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}, "13:41:59": {"input": "1001010000", "output": "0"}, "13:42:00": {"input": "1001010000", "output": "0"}, "13:42:01": {"input": "1001010000", "output": "1001"}, "13:42:02": {"input": "1010010000", "output": "101"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}, "13:41:59": {"input": "1001010000", "output": "0"}, "13:42:00": {"input": "1001010000", "output": "0"}, "13:42:01": {"input": "1001010000", "output": "1001"}, "13:42:02": {"input": "1010010000", "output": "101"}, "13:42:03": {"input": "1010010000", "output": "101"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}, "13:41:59": {"input": "1001010000", "output": "0"}, "13:42:00": {"input": "1001010000", "output": "0"}, "13:42:01": {"input": "1001010000", "output": "1001"}, "13:42:02": {"input": "1010010000", "output": "101"}, "13:42:03": {"input": "1010010000", "output": "101"}, "13:42:04": {"input": "1001010000", "output": "0"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}, "13:41:59": {"input": "1001010000", "output": "0"}, "13:42:00": {"input": "1001010000", "output": "0"}, "13:42:01": {"input": "1001010000", "output": "1001"}, "13:42:02": {"input": "1010010000", "output": "101"}, "13:42:03": {"input": "1010010000", "output": "101"}, "13:42:04": {"input": "1001010000", "output": "0"}, "13:42:05": {"input": "1001010001", "output": "1001"}}{"13:41:57": {"input": "1001010000", "output": "0"}, "13:41:58": {"input": "1001010000", "output": "0"}, "13:41:59": {"input": "1001010000", 

这是一个连续执行的脚本。如您所见,时间戳并不是真正有序的。 谁能帮我们解决这个问题。也许你们能看到我看不到的东西。

非常感谢!

我发现为什么它如此重复数据包。 因为 dict 数据是在 while 循环之外定义的,所以它不断向 dict 添加新的键、值。 dict 被包装到 json 文件中,并不断呈指数增长。

需要在 while 循环中定义 dict 数据,以便每次将 dict 转储到 json 时都可以重复使用。

...
#save data dict to json format
def save_to_file(stamp, input, output, file):
    mydata[stamp] = {'input': input, 'output': output}
    with open(file,'a') as f:
        json.dump(mydata, f)

while True:
    mydata={}
    # ts = time.time()    #gets current time 
    _now = time.time()
    ts = time.localtime(_now)
    # timestamp = (time.strftime('%H:%M:%S',ts),str('%.3f'%_now).split('.')[1])
    timestamp = time.strftime('%X',ts)
    datestamp = time.strftime('%d-%m-%Y',ts)
    log_folder='logs/'
    color=['black/','orange/','metal/']
...