所以我正在访问一个 Siemens LOGO!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",
这是脚本的一次连续执行。正如您所看到的,时间戳并不是真正按顺序排列的。谁能帮我们解决这个问题。也许你们能看到我看不到的东西。
非常感谢!