基于EPICS modbus模块的一通道电压监测项目

2023-12-31 18:43:37

先介绍在本项目中使用到的硬件:

1)开发板:为香橙派Zero2,安装系统如下:

Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:        22.04
Codename:       jammy

2) USB转485模块:微雪工业级USB转RS485串口转换器

?3)485一通道电压测量模块:此模块默认通信参数

  • 波特率:9600
  • 数据位:8
  • 停止为:1
  • 校验:无

此模块的modbus rtu通信寄存器如下:

?以下是IOC软件部分:

以下是IOC控制程序的开发过程:

这个IOC程序需要以下模块支持:base, asyn和modbus。

1)用makeBaseApp构造IOC程序框架。

2)在configure/RELEASE下添加所需模块的路径:

SUPPORT=/usr/local/EPICS/synApps/support

ASYN=$(SUPPORT)/asyn
MODBUS=$(SUPPORT)/modbus

# EPICS_BASE should appear last so earlier modules can override stuff:
EPICS_BASE = /usr/local/EPICS/base

?3)在onevoltageApp/src目录下Makefile文件中添加,编译所需的模块以及库文件:

....
onevoltage_DBD += asyn.dbd
onevoltage_DBD += drvAsynSerialPort.dbd
onevoltage_DBD += modbusSupport.dbd


# Add all the support libraries needed by this IOC
onevoltage_LIBS += asyn
onevoltage_LIBS += modbus

...

4) 在onevoltageApp/Db文件中添加以下两个文件:

a)?longinInt32.template用于从硬件读取原始电压表示的数值:

record(longin,"$(P)$(R)") {
    field(DTYP,"asynInt32")
    field(INP,"@asyn($(PORT) $(OFFSET))$(DATA_TYPE)")
    field(SCAN, "$(SCAN)")
    field(FLNK, "$(P)Digitals")
    field(EGU, "V")
}

b)?calc_voltage.db:用于转换硬件原始电压表示格式为正常表示的电压格式:

record(calc, "$(P)Digitals") {
  field(SCAN, "Passive")
  field(FLNK, "$(P)Divsion")
  field(CALC, "FLOOR(A/B)")
  field(INPA, "$(P)RawV1")
  field(INPB, "10000")
}


record(calc, "$(P)Divsion") {
  field(SCAN, "Passive")
  field(FLNK, "$(P)Voltage")
  field(CALC, "10^A")
  field(INPA, "$(P)Digitals")
}

record(calc, "$(P)Voltage") {
  field(SCAN, "Passive")
  field(EGU, "V")
  field(CALC, "(A-C*10000)/B")
  field(INPA, "$(P)RawV1")
  field(INPB, "$(P)Divsion")
  field(INPC, "$(P)Digitals")
}

将以下两个文件添加到Makefile中:

...
DB += longinInt32.template
DB += calc_voltage.db
...

5) 返回这个IOC的顶层目录并且执行Make,编译这个项目。

6)进入启动目录,创建一个模板实例化文件,然后编辑st.cmd文件:

a) onevoltage.substitutions

file "../../db/longinInt32.template" { pattern
{P,           R,                PORT,     OFFSET,   DATA_TYPE,      SCAN}
{ONEVOLTAGE:,    RawV1,        A0_In_Word,     0,     UINT16,       "I/O Intr"}
}

b) st.cmd?

#!../../bin/linux-aarch64/onevoltage

#- You may have to change onevoltage to something else
#- everywhere it appears in this file

< envPaths

cd "${TOP}"

## Register all support components
dbLoadDatabase "dbd/onevoltage.dbd"
onevoltage_registerRecordDeviceDriver pdbbase

drvAsynSerialPortConfigure("onevoltage", "/dev/ttyUSB0", 0, 0, 0)
asynSetOption("onevoltage",0,"baud","9600")
asynSetOption("onevoltage",0,"parity","none")
asynSetOption("onevoltage",0,"bits","8")
asynSetOption("onevoltage",0,"stop","1")

modbusInterposeConfig("onevoltage",1 ,2000,0)

drvModbusAsynConfigure("A0_In_Word", "onevoltage", 1, 4,  0, 1, 0,  200, "onevoltage")

## Load record instances
dbLoadRecords("db/calc_voltage.db","P=ONEVOLTAGE:")

cd "${TOP}/iocBoot/${IOC}"
dbLoadTemplate("onevoltage.substitutions")

iocInit

7) 在IOC启动目录中,执行以下命令../../bin/linux-aarch64/onevoltage st.cmd,并且用dbl查看加载的记录:

epics> dbl
ONEVOLTAGE:Digitals
ONEVOLTAGE:Divsion
ONEVOLTAGE:Voltage
ONEVOLTAGE:RawV1

8) 先用命令行测试:

root@orangepizero2:~#  caget ONEVOLTAGE:RawV1 # 测量电压的原始表示
ONEVOLTAGE:RawV1               32002
root@orangepizero2:~#  caget ONEVOLTAGE:Voltage # 转换后的电压值
ONEVOLTAGE:Voltage             2.005

9) 用CSS制作图形界面:

写在2023年12月31日,祝大家新年快乐,有工作的,工作顺利,想换工作的,来年都能找一个好工作。?

文章来源:https://blog.csdn.net/yuyuyuliang00/article/details/135317620
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。