跳到主要内容

原场景脚本参考

以下十个脚本来自案例静态读取,按原文本展示,没有执行、重写或校验运行结果。连接地址仍由原适配器配置决定。脚本中的日志不能代替 PLC 侧点位读回。

启动外部PLC​

所属脚本组:initScripts。

// 初始化PLC连接和点位;后续由模型事件写PLC后直接扫描一次。
PLC初始化

PLC扫描​

所属脚本组:methodScripts。

def main() {
try {
// 每次只建立一个短连接:读取PLC并修改模型属性,不直接搬运物料。
if (!中鼎PLC.connect()) {
throw new RuntimeException("PLCAdapter连接失败")
}

// 模拟PLC暂不支持多点批量读,因此依次读取三个正转输出。
def value252 = 中鼎PLC.getTagValue("输送线252正转")
def value251 = 中鼎PLC.getTagValue("输送线251正转")
def value250 = 中鼎PLC.getTagValue("输送线250正转")
if (value252 == null || value251 == null || value250 == null) {
throw new RuntimeException("正转输出读取失败")
}

boolean run252 = value252 as boolean
boolean run251 = value251 as boolean
boolean run250 = value250 as boolean
def command = "${run252},${run251},${run250}"

// 仅在PLC命令变化时更新属性和打印日志,避免高频事件影响仿真推进。
if (!command.equals(PLC_LastCommand)) {
PLC_LastCommand = command
输送线252.Stopped = !(run252 || run251)
输送线251.Stopped = !(run251 || run250)
输送线250.Stopped = !run250
println("[PLC联调][读取] DB7000正转:252=" + run252 + ",251=" + run251 + ",250=" + run250)
}

if (PLC_Error) {
PLC_Error = false
println("[PLC联调][恢复] PLCAdapter通信已恢复")
}
} catch (Exception e) {
if (!PLC_Error) {
输送线252.Stopped = true
输送线251.Stopped = true
输送线250.Stopped = true
println("[PLC联调][异常] PLC扫描失败,输送线已停止:" + e.getMessage())
}
PLC_Error = true
PLC演示起点.Description = "PLC=" + 中鼎PLC.Server + ";TASK=" + PLC_TaskCount + ";ERROR=" + e.getMessage()
PLC_LastCommand = ""
} finally {
// 本次读取完成即断开;下一次模型事件写PLC后会直接调用本脚本一次。
中鼎PLC.disconnect()
}
}

PLC初始化​

所属脚本组:methodScripts。

def main() {
// 打开运行日志。println 已由引擎绑定到 Web 编辑器“运行日志”面板。
sim.setOpenLog(true)

PLC_TaskCount = 0
PLC_CompletedCount = 0
PLC_LastCommand = ""
PLC_Error = false
PLC演示起点.Description = "PLC=" + 中鼎PLC.Server + ";TASK=0"

// 仿真启动时三条输送线保持停止,后续只由 PLC扫描 修改 Stopped 属性。
输送线252.Stopped = true
输送线251.Stopped = true
输送线250.Stopped = true

// processTask() 自动任务真正会触发的三个回调。
堆垛机1.PickFinishCtrl = &PLC货叉收回
堆垛机1.PutFinishCtrl = &PLC堆垛机到位
堆垛机1.TaskFinishCtrl = &PLC任务完成

/*
* 点位名称 -> PLC 地址,只在这里集中维护。
*
* DB7000:输送线握手
* 首段启动请求 DBX0.0 = Number[0].In.insureBtn,Source 有物料时置位。
* 二段启动请求 DBX22.0 = Number[1].In.insureBtn,物料到达输送线252时置位。
* 三段启动请求 DBX44.0 = Number[2].In.insureBtn,物料到达输送线251时置位。
* 输送线252正转 DBX16.0 = Number[0].Q.Fwd,PLC命令,控制输送线252。
* 输送线251正转 DBX38.0 = Number[1].Q.Fwd,PLC命令,控制输送线251。
* 输送线250正转 DBX60.0 = Number[2].Q.Fwd,PLC命令,控制输送线250。
*
* DB7001:堆垛机动作反馈
* 堆垛机行走终点 DBX10.0 = Travel.FwdEnd。
* 堆垛机升降终点 DBX28.0 = Lift.UpEnd。
* 货叉1原位 DBX70.0 = Fork.Z1.Center_Stop。
* 货叉1有货 DBX71.1 = Fork.Z1.OnLoad。
*/
中鼎PLC.registerTags([
"首段启动请求": "DB7000.DBX0.0",
"二段启动请求": "DB7000.DBX22.0",
"三段启动请求": "DB7000.DBX44.0",
"输送线252正转": "DB7000.DBX16.0",
"输送线251正转": "DB7000.DBX38.0",
"输送线250正转": "DB7000.DBX60.0",
"堆垛机行走终点": "DB7001.DBX10.0",
"堆垛机升降终点": "DB7001.DBX28.0",
"货叉1原位": "DB7001.DBX70.0",
"货叉1有货": "DB7001.DBX71.1"
])

if (!中鼎PLC.connect()) {
PLC_Error = true
println("[PLC联调][异常] 无法连接PLCAdapter:" + 中鼎PLC.Server)
return
}

// 清除上一轮残留的请求和设备反馈。
中鼎PLC.setTagValue("首段启动请求", false)
中鼎PLC.setTagValue("二段启动请求", false)
中鼎PLC.setTagValue("三段启动请求", false)
中鼎PLC.setTagValue("堆垛机行走终点", false)
中鼎PLC.setTagValue("堆垛机升降终点", false)
中鼎PLC.setTagValue("货叉1原位", true)
中鼎PLC.setTagValue("货叉1有货", false)
中鼎PLC.disconnect()
println("[PLC联调][连接] PLCAdapter通信自检成功:" + 中鼎PLC.Server + ",协议=S7,rack=0,slot=1")
}

PLC请求首段​

所属脚本组:methodScripts。

def main() {
// Source产生物料时触发:向PLC请求启动第一段输送线252。
def itemName = "待发送物料"
if (@) {
itemName = @.Name
}
if (!中鼎PLC.connect()) {
println("[PLC联调][异常] 首段请求写入前,PLCAdapter连接失败")
return
}
中鼎PLC.setTagValue("首段启动请求", true)
中鼎PLC.disconnect()
// 模拟PLC会立即更新输出,直接读取一次即可;不要创建周期扫描任务。
PLC扫描.main()
println("[PLC联调][写入] " + itemName + " 已就绪:DB7000.DBX0.0=true")
}

PLC输送到位​

所属脚本组:methodScripts。

def main() {
// 输送线末端触发;物料移动仍由 Source/Conveyor 连线完成。
def itemName = "无物料"
if (@) {
itemName = @.Name
}

if (?.Name.equals("输送线252")) {
if (!中鼎PLC.connect()) {
println("[PLC联调][异常] 输送线252到位反馈写入前,PLCAdapter连接失败")
return
}
中鼎PLC.setTagValue("首段启动请求", false)
中鼎PLC.setTagValue("二段启动请求", true)
中鼎PLC.disconnect()
PLC扫描.main()
println("[PLC联调][写入] " + itemName + " 到达输送线252:DBX0.0=false,DBX22.0=true")
} else if (?.Name.equals("输送线251")) {
if (!中鼎PLC.connect()) {
println("[PLC联调][异常] 输送线251到位反馈写入前,PLCAdapter连接失败")
return
}
中鼎PLC.setTagValue("二段启动请求", false)
中鼎PLC.setTagValue("三段启动请求", true)
中鼎PLC.disconnect()
PLC扫描.main()
println("[PLC联调][写入] " + itemName + " 到达输送线251:DBX22.0=false,DBX44.0=true")
} else if (?.Name.equals("输送线250")) {
PLC_TaskCount = PLC_TaskCount + 1
PLC演示起点.Description = "PLC=" + 中鼎PLC.Server + ";TASK=" + PLC_TaskCount

def taskNo = "PLC_" + PLC_TaskCount
int targetColumn = PLC_TaskCount + 1
int targetLayer = PLC_TaskCount + 1
堆垛机1.TaskList.appendRow(taskNo, "输送线250", "货架_copy2", targetColumn, targetLayer, 1, 0)
println("[PLC联调][派单] " + taskNo + ":输送线250 -> 货架_copy2[列" + targetColumn + ",层" + targetLayer + "]")
堆垛机1.processTask()
}
}

PLC货叉收回​

所属脚本组:methodScripts。

def main() {
// PickFinishCtrl触发。@有物料表示从输送线250取货完成。
if (!中鼎PLC.connect()) {
println("[PLC联调][异常] 货叉反馈写入前,PLCAdapter连接失败")
return
}
boolean hasLoad = false
if (@) {
hasLoad = true
中鼎PLC.setTagValue("三段启动请求", false)
println("[PLC联调][取货] 输送线250取货完成:DB7000.DBX44.0=false")
}

中鼎PLC.setTagValue("货叉1原位", true)
中鼎PLC.setTagValue("货叉1有货", hasLoad)
中鼎PLC.disconnect()
if (hasLoad) {
PLC扫描.main()
}
println("[PLC联调][货叉] 收回:DB7001.DBX70.0=true,DBX71.1=" + hasLoad)
}

PLC堆垛机到位​

所属脚本组:methodScripts。

def main() {
// PutFinishCtrl触发:堆垛机已到达入库目标位置。
if (!中鼎PLC.connect()) {
println("[PLC联调][异常] 堆垛机到位反馈写入前,PLCAdapter连接失败")
return
}
中鼎PLC.setTagValue("堆垛机行走终点", true)
中鼎PLC.setTagValue("堆垛机升降终点", true)
中鼎PLC.disconnect()
println("[PLC联调][堆垛机] 到达目标:DB7001.DBX10.0=true,DBX28.0=true")
}

PLC任务完成​

所属脚本组:methodScripts。

def main() {
// TaskFinishCtrl触发:一次完整入库任务结束,复位堆垛机反馈。
if (!中鼎PLC.connect()) {
println("[PLC联调][异常] 任务完成反馈写入前,PLCAdapter连接失败")
return
}
中鼎PLC.setTagValue("堆垛机行走终点", false)
中鼎PLC.setTagValue("堆垛机升降终点", false)
中鼎PLC.setTagValue("货叉1原位", true)
中鼎PLC.setTagValue("货叉1有货", false)
中鼎PLC.disconnect()
PLC_CompletedCount = PLC_CompletedCount + 1
println("[PLC联调][完成] 第" + PLC_CompletedCount + "个入库任务完成")
}

PLC停机​

所属脚本组:methodScripts。

def main() {
sim.setOpenLog(true)
输送线252.Stopped = true
输送线251.Stopped = true
输送线250.Stopped = true

if (中鼎PLC.connect()) {
中鼎PLC.setTagValue("首段启动请求", false)
中鼎PLC.setTagValue("二段启动请求", false)
中鼎PLC.setTagValue("三段启动请求", false)
中鼎PLC.setTagValue("堆垛机行走终点", false)
中鼎PLC.setTagValue("堆垛机升降终点", false)
中鼎PLC.setTagValue("货叉1原位", true)
中鼎PLC.setTagValue("货叉1有货", false)
中鼎PLC.disconnect()
}
println("[PLC联调][停止] 模型已停止,PLC反馈已复位,PLCAdapter已断开")
}

停止外部PLC​

所属脚本组:endScripts。

// 场景结束时统一停止设备并清除反馈。
PLC停机