此处使用的IOC中含有3个温度值,2个二进制输出,2个二进制输入,如下所示:
三个温度值的过程变量名:
- THREETS:T1:Temperarure
- THREETS:T2:Temperarure
- THREETS:T3:Temperarure
两个二进制输出:
- TEST:Bo1
- TEST:Bo2
两个二进制输入:
- TEST:Bi1
- TEST:Bi2
在mysql数据库中建立一个名为temperature的数据库,并且在此数据库下建立一个名为temperature的数据表:
mysql> DESC temperature;
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| temp1 | float | YES | | NULL | |
| temp2 | float | YES | | NULL | |
| temp3 | float | YES | | NULL | |
| light1 | tinyint | YES | | NULL | |
| light2 | tinyint | YES | | NULL | |
| status1 | tinyint | YES | | NULL | |
| status2 | tinyint | YES | | NULL | |
| timepoint | datetime | YES | | NULL | |
+-----------+----------+------+-----+---------+----------------+
9 rows in set (0.00 sec)
以下python代码实现一秒钟读取一次以上所有EPICS过程变量,并且把它们存储到数据库对应表中,使用pymsql模块操作数据库,使用epics模块通过通道访问读取过程变量:文章来源:https://www.toymoban.com/news/detail-546908.html
import pymysql
import epics
import cryptography
import time
# 连接数据库
db=pymysql.connect(host="192.168.50.181",user="blctrl",
password="@AAaa0012",
port=3306,
database="temperature")
cursor = db.cursor()
# 构造插入插入字段
tempname="temp1,temp2,temp3"
boname="light1,light2"
biname="status1,status2"
s="%s,%s,%s,%s" % (tempname, boname, biname,"timepoint")
print(s)
# 从通道访问获取值,并且构造进行数据插入的sql语句
def create_insert_sql(s):
temps=[]
for i in range(3):
temper_ch="THREETS:T%d:Temperature" % (i+1)
temper=epics.caget(temper_ch)
temps.append(temper)
bos=[]
for i in range(2):
bo_ch="TEST:Bo%d" % (i+1)
bo=epics.caget(bo_ch)
bos.append(bo)
bis=[]
for i in range(2):
bi_ch="TEST:Bi%d" % (i+1)
bi=epics.caget(bi_ch)
bis.append(bi)
# 构造插入值
v1="%f,%f,%f " % tuple(temps)
v2="%d,%d " % tuple(bos)
v3="%d,%d" % tuple(bis)
v="%s,%s,%s,%s" % (v1,v2,v3, "now()")
# print(s,v)
insert_sql="""INSERT INTO {0} ({1}) VALUES ({2})""".format("temperature",s,v)
return insert_sql
# 执行sql语句
def insert(cursor, db, insert_sql):
cursor.execute(insert_sql)
db.commit()
# 每秒读取一次过程变量,进行数据库插入,进行指定的次数
def insert_per_second(cursor, db, s ,times=10):
while True:
insert_sql=create_insert_sql(s)
insert(cursor, db, insert_sql)
if times == 0:
break;
times =times-1
time.sleep(1)
# 数据库插入5次
insert_per_second(cursor, db, s,times=5)
# 断开数据库连接
db.close()
执行insert_per_second(cursor, db, s,times=5)函数前后数据表temperaure中数据对比:文章来源地址https://www.toymoban.com/news/detail-546908.html
mysql> select * from temperature;
Empty set (0.00 sec)
mysql> select * from temperature;
+----+-------+-------+-------+--------+--------+---------+---------+---------------------+
| id | temp1 | temp2 | temp3 | light1 | light2 | status1 | status2 | timepoint |
+----+-------+-------+-------+--------+--------+---------+---------+---------------------+
| 11 | 28 | 28.3 | 27.8 | 0 | 1 | 1 | 0 | 2023-07-07 08:54:28 |
| 12 | 28 | 28.3 | 27.7 | 0 | 1 | 1 | 0 | 2023-07-07 08:54:29 |
| 13 | 28 | 28.3 | 27.7 | 0 | 1 | 1 | 0 | 2023-07-07 08:54:31 |
| 14 | 28 | 28.3 | 27.8 | 0 | 1 | 1 | 0 | 2023-07-07 08:54:33 |
| 15 | 28 | 28.3 | 27.8 | 0 | 1 | 1 | 0 | 2023-07-07 08:54:34 |
| 16 | 28 | 28.3 | 27.8 | 0 | 1 | 1 | 0 | 2023-07-07 08:54:36 |
+----+-------+-------+-------+--------+--------+---------+---------+---------------------+
6 rows in set (0.00 sec)
到了这里,关于mysql在EPICS IOC过程变量数据存储中的应用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!