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

如何解决西门子标志! PLC 数据顺序错误

所以我正在访问 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,"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_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","13:41:59": {"input": "1001010000","13:42:00": {"input": "1001010000","13:42:01": {"input": "1001010000","output": "1001"}}{"13:41:57": {"input": "1001010000","output": "1001"},"13:42:02": {"input": "1010010000","output": "101"}}{"13:41:57": {"input": "1001010000","output": "101"},"13:42:03": {"input": "1010010000","13:42:04": {"input": "1001010000","13:42:05": {"input": "1001010001",

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

非常感谢!

解决方法

我发现了为什么它如此重复数据包。 因为 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/']
...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?