【问题标题】:Siemens LOGO! PLC data in the wrong order西门子 LOGO! PLC 数据顺序错误
【发布时间】:2021-08-21 11:14:55
【问题描述】:

所以我正在访问 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", 

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

非常感谢!

【问题讨论】:

  • 尝试通过support.industry.siemens.com/tf/ww/en/conf/65提问。还有关于LOGO和Python的文章。
  • 谢谢,我会这样做以供将来参考。这更像是一个编程错误。我已经为此挂了 2 周,不明白为什么它不起作用。但我现在解决了!

标签: python json plc siemens snap7


【解决方案1】:

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

dict 数据需要在 while 循环中定义,以便每次将 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/']
...

【讨论】:

    猜你喜欢
    • 2021-09-13
    • 2018-10-09
    • 2022-08-13
    • 1970-01-01
    • 2016-11-18
    • 2015-10-07
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    相关资源
    最近更新 更多