PLC介绍
西门子系列PLC在国内的市场占比第一,1200系列中小型PLC,因其众多的产品序列、强大的通讯功能和丰富扩展模块,被使用在工业生产、自动化生产线、智能制造、机器人等各行各业。根据CPU的供电电源的型号和数字量输出的类型,CPU 1215 C又可以分为CPU 1215C AC/DC/RLY 、CPU 1215C DC/DC/RLY 、CPU 1215C DC/DC/DC三种型号,以CPU 1215C DC/DC/DC型号为例,第一个“DC”表示该款型号PLC的供电电源为直流24V,第二个“DC”表示PLC的数字量输入为晶体管型信号输入,且数字量输入只能是晶体管型,第三个“DC”表示PLC的数字量输出类型为晶体管型,而“RLY”表示数字量的出类型为继电器型。不同的数字量输出类型对应不同的负载和带载能力。晶体管型输出端可以做高速脉冲输出,但是流出的最大电流只有0.5A,带载能力弱;继电器型输出端子不能做高速脉冲输出,也就是继电器不能快速的开和关操作,但是继电器型输出端子流出的最大电流能到2.0A,带载能力很强。
紧凑型 CPU 1215C 带有:
3 种设备类型,带有不同的电源和控制电压。
- 集成电源,可作为宽范围交流或直流电源(85 ~ 264 V AC 或 24 V DC)
- 集成的 24 V 编码器/负载电流源:
- 用于直接连接传感器和编码器。带有 400 mA 输出电流,它也可用作负载电源。
- 14 点集成 24 V 直流数字量输入(漏电流/源电流(IEC 1 型漏电流))。
- 10 点集成数字量输出,24 V 直流或继电器。
- 2 点集成模拟量输入 0 ~ 10 V。
- 2 点集成模拟量输出 0 ~ 20 mA。
- 4 点脉冲输出 (PTO),频率最高 100 kHz。
- 脉冲宽度调制输出 (PWM),频率最高 100 kHz。
- 2 个集成以太网接口(TCP/IP native、ISO-on-TCP)。
- 6 个快速计数器(3 个最大频率为 100 kHz;3 个最大频率为 30 kHz),带有可参数化的使能和复位输入,可以同时用作带有 2 点单独输入的加减计数器,或用于连接增量型编码器
- 通过附加通信接口扩展,例如,RS485 或 RS232
- 通过信号板使用模拟或数字信号直接在 CPU 上扩展(保持 CPU 安装尺寸)
- 通过信号模块使用各种模拟量和数字量输入和输出信号扩展。
- 可选存储器扩展(SIMATIC 存储卡)。
- PID 控制器,具有自动调谐功能。
- 集成实时时钟。
PLC程序准备:
1、数据块1:modbus tcp通信配置,设置如下
2、数据块2:数据类型转换,设置如下:
3、梯形图程序,如下:
1)设置modbus tcp服务器端:
2) 数据转换:
从IW64获取第一路模拟量输入,将其转成工程量,放入保持寄存器中。
从IW66获取第二路模拟量输入,将其转成工程量,放入保持寄存器中。
从保持寄存器获取要设定的工程值,将其转成模拟值,写入第一路模拟输出地址QW64
从保持寄存器获取要设定的工程值,将其转成模拟值,写入第二路模拟输出地址QW66
EPICS程序准备
所需安装的EPICS 模板
由于EPICS模块之间的依赖关系,需要以下4个模块:
base, seq , ipac ,asyn, modbus
IOC建立过程
先创建一个项目目录,在其下用makeBaseApp工具创建一个IOC应用程序的框架,编辑configure/RELEASE文件,添加本IOC依赖的模块,在程序src下Makefile中添加所依赖的库文件和源文件; 在Db文件下添加模板文件:
configure/RELEASE:
...
SUPPORT=/usr/local/EPICS/synApps/support
# If using the sequencer, point SNCSEQ at its top directory:
#SNCSEQ = $(MODULES)/seq-ver
ASYN=$(SUPPORT)/asyn
MODBUS=$(SUPPORT)/modbus
AUTOSAVE=$(SUPPORT)/autosave
...
xxxApp/src:
s7-1215c_DBD += asyn.dbd
s7-1215c_DBD += drvAsynIPPort.dbd
s7-1215c_DBD += modbus.dbd
s7-1215c_DBD += asSupport.dbd
# Include dbd files from all support applications:
# #s7-1215c_DBD += xxx.dbd
#
# # Add all the support libraries needed by this IOC
#s7-1215c_LIBS += xxx
s7-1215c_LIBS += asyn
s7-1215c_LIBS += modbus
s7-1215c_LIBS += autosave
xxxApp/Db:
bi_bit.template:用于按位读取PLC的输入输出点
record(bi,"$(P)$(R)BI$(N)") {
field(DTYP,"asynUInt32Digital")
field(INP,"@asynMask($(PORT) $(OFFSET) 0x1)")
field(SCAN,"$(SCAN)")
field(ZNAM,"$(ZNAM)")
field(ONAM,"$(ONAM)")
field(ZSV,"$(ZSV)")
field(OSV,"$(OSV)")
}
bo_bit.template:用于按位写PLC的输出点。
record(bo,"$(P)$(R)BO$(N)") {
field(DTYP,"asynUInt32Digital")
field(OUT,"@asynMask($(PORT-QO) $(OFFSET) 0x1)")
field(ZNAM,"$(ZNAM)")
field(ONAM,"$(ONAM)")
}
record(bi,"$(P)$(R)BO$(N)_RBV") {
field(DTYP,"asynUInt32Digital")
field(INP,"@asynMask($(PORT-QI) $(OFFSET) 0x1)")
field(SCAN,"$(SCAN)")
field(ZNAM,"$(ZNAM)")
field(ONAM,"$(ONAM)")
}
float32_2words_in.template:用于读取读取模拟输入的电压值
record(ai, "$(P)$(R)VOLTAGE$(N)") {
field(DTYP,"asynFloat64")
field(INP,"@asyn($(PORT) $(OFFSET))FLOAT32_BE")
field(HOPR,"$(HOPR)")
field(LOPR,"$(LOPR)")
field(PREC,"$(PREC)")
field(SCAN,"$(SCAN)")
field(EGU, "V")
}
float32_2words_out.template:用于设置输出电流值
record(ao, "$(P)$(R)CURRENT$(N)") {
field(DTYP,"asynFloat64")
field(OUT,"@asyn($(PORT-CO) $(OFFSET))FLOAT32_BE")
field(HOPR,"$(HOPR)")
field(LOPR,"$(LOPR)")
field(PREC,"$(PREC)")
field(EGU, "mA")
}
record(ai, "$(P)$(R)CURRENT$(N)_RBV") {
field(DTYP,"asynFloat64")
field(INP,"@asyn($(PORT-CI) $(OFFSET))FLOAT32_BE")
field(HOPR,"$(HOPR)")
field(LOPR,"$(LOPR)")
field(PREC,"$(PREC)")
field(SCAN,"$(SCAN)")
field(EGU, "mA")
}
回到此IOC的顶层目录,编译以上代码,生产IOC程序。
5、编辑启动目录,添加模板替换文件,编辑启动脚本st.cmd:
1)添加的实例化文件如下:
1)qb.substitutions:实例化10个bo记录,用于写,分别写一个位到PLC的输出点Q0.0~Q0.7,Q1.0-Q1.1。
file "../../db/bo_bit.template" { pattern
{P, R, N, PORT-QO, ,PORT-QI OFFSET, ZNAM, ONAM, SCAN}
{S7-1215C:, BIT:, 00 QB-O, QB-I 0, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 01 QB-O, QB-I 1, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 02 QB-O, QB-I 2, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 03 QB-O, QB-I 3, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 04 QB-O, QB-I 4, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 05 QB-O, QB-I 5, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 06 QB-O, QB-I 6, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 07 QB-O, QB-I 7, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 10 QB-O, QB-I 8, Low, High, "I/O Intr"}
{S7-1215C:, BIT:, 11 QB-O, QB-I 9, Low, High, "I/O Intr"}
}
2) ib.substitutions:实例化14个bi记录,用于读取,分别读取一个位到PLC的输出点I0.0~I0.7,I1.0~I1.5。
file "../../db/bi_bit.template" { pattern
{P, R, N, PORT, OFFSET, ZNAM, ONAM, ZSV, OSV, SCAN}
{S7-1215C:, BIT:, 00 IB, 0, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 01 IB, 1, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 02 IB, 2, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 03 IB, 3, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 04 IB, 4, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 05 IB, 5, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 06 IB, 6, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 07 IB, 7, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 10 IB, 8, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 11 IB, 9, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 12 IB, 10, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 13 IB, 11, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 14 IB, 12, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
{S7-1215C:, BIT:, 15 IB, 13, Low, High, NO_ALARM, MAJOR, "I/O Intr"}
}
3)voltages.substitutions:实例化2个ai记录,这两个实例将分别从PLC的两路电压模拟量输入。
file "../../db/float32_2words_in.template" { pattern
{P, R, N PORT, OFFSET , HOPR, LOPR , PREC ,SCAN}
{S7-1215C:, AI: 0 VIN, 0 , 10, 0 ,3 , "I/O Intr"}
{S7-1215C:, AI: 1 VIN, 2 , 10, 0 ,3 , "I/O Intr"}
}
4)currents.substitutions:实例化两个ao记录,用于分别设置PLC的两路电流模拟输出。
file "../../db/float32_2words_out.template" { pattern
{P, R, , N PORT-CO, PORT-CI OFFSET, HOPR, LOPR, , PREC, SCAN}
{S7-1215C:, AO:, 0 IOUT IIN, 0 , 20, 0, 3 , "I/O Intr"}
{S7-1215C:, AO:, 1 IOUT IIN, 2 , 20, 0, 3 , "I/O Intr"}
}
编辑st.cmd文件:
#!../../bin/linux-aarch64/s7-1215c
#- You may have to change s7-1215c to something else
#- everywhere it appears in this file
< envPaths
cd "${TOP}"
## Register all support components
dbLoadDatabase "dbd/s7-1215c.dbd"
s7_1215c_registerRecordDeviceDriver pdbbase
drvAsynIPPortConfigure("S1215C","192.168.3.217:502",0,0,1)
modbusInterposeConfig("S1215C",0 ,2000,0)
# func code = 5 按位写线圈
drvModbusAsynConfigure("QB-O", "S1215C", 1, 5, 0, 10 , 0, 1000, "QB-O")
# func code = 1 按位读线圈
drvModbusAsynConfigure("QB-I", "S1215C", 1, 1, 0, 10 , 0, 500, "QB-I")
# func code = 2 按位读取离散输入
drvModbusAsynConfigure("IB", "S1215C", 1, 2, 0, 14 , 0, 500, "IB")
# func code = 3 按字读取保持寄存器
drvModbusAsynConfigure("VIN", "S1215C", 1 , 3, 0, 4 , 0, 500, "VIN")
# func code = 3 按字读取保持寄存器
drvModbusAsynConfigure("IIN", "S1215C", 1 , 3, 4, 4 , 0, 500, "IIN")
# func code = 6 按字写入保持寄存器
drvModbusAsynConfigure("IOUT", "S1215C", 1, 6, 4, 4 , 0, 1000, "IOUT")
## Load record instances
#dbLoadRecords("db/s7-1215c.db","user=orangepi")
cd "${TOP}/iocBoot/${IOC}"
dbLoadTemplate("qb.substitutions")
dbLoadTemplate("ib.substitutions")
dbLoadTemplate("voltages.substitutions")
dbLoadTemplate("currents.substitutions")
iocInit
IOC程序启动以及测试
在IOC启动目录下,程序启动命令:../../bin/linux-aarch64/s7-1215c st.cmd
dbl查看所有加载的记录。
S7-1215C:AI:VOLTAGE0
S7-1215C:AI:VOLTAGE1
S7-1215C:AO:CURRENT0_RBV
S7-1215C:AO:CURRENT1_RBV
S7-1215C:AO:CURRENT0
S7-1215C:AO:CURRENT1
S7-1215C:BIT:BO00_RBV
S7-1215C:BIT:BO01_RBV
S7-1215C:BIT:BO02_RBV
S7-1215C:BIT:BO03_RBV
S7-1215C:BIT:BO04_RBV
S7-1215C:BIT:BO05_RBV
S7-1215C:BIT:BO06_RBV
S7-1215C:BIT:BO07_RBV
S7-1215C:BIT:BO10_RBV
S7-1215C:BIT:BO11_RBV
S7-1215C:BIT:BI00
S7-1215C:BIT:BI01
S7-1215C:BIT:BI02
S7-1215C:BIT:BI03
S7-1215C:BIT:BI04
S7-1215C:BIT:BI05
S7-1215C:BIT:BI06
S7-1215C:BIT:BI07
S7-1215C:BIT:BI10
S7-1215C:BIT:BI11
S7-1215C:BIT:BI12
S7-1215C:BIT:BI13
S7-1215C:BIT:BI14
S7-1215C:BIT:BI15
S7-1215C:BIT:BO00
S7-1215C:BIT:BO01
S7-1215C:BIT:BO02
S7-1215C:BIT:BO03
S7-1215C:BIT:BO04
S7-1215C:BIT:BO05
S7-1215C:BIT:BO06
S7-1215C:BIT:BO07
S7-1215C:BIT:BO10
S7-1215C:BIT:BO11