1.python连接到本地的mysql数据
host:连接的本地的数据库的名称
user:用户名
password:密码
port:端口号,默认为3306
db:数据库名称
charset:字符编码
2.连接plc读取数据
# 创建通讯客户端实例
plcObj = snap7.client.Client()
# 连接至PLC
plcObj.connect('192.168.0.35', 0, 1)
确定数据在PLC的位置
因为要循环读取所以使用while True
3.将数据写入到数据库中
sql = "insert into *** values('%s', '%s', '%s')" % (****, *****, ******)
需要提交sql语句:con.commit()
时间根据自己的要求设定
time.sleep(*)
需要用的库有pymysql,time,snap7