XScript 手册 · Chapter 20
DIO — 数字输入输出
以 IO["名称"] 访问。按 IO Editor 中注册的名称,以单个位为单位读写输入输出。
也可用赋值运算符(=)写出输出,在 bool 上下文中会自动返回当前值。
基本示例
xscript
// OUTPUT 控制
IO["O_Blow1"] = OFF;
IO["O_Blow2"] = ON;
IO["O_Blow2"].ToggleBit();
// INPUT 检查
if (IO["I_IFReadyPrev"] == false)
{
return false;
}
// 针对数字输出的条件判断
if (IO["O_DigitalOutput"])
{
// ON 的情况
}
else
{
// OFF 的情况
}实战示例 — 等待传送带料碗检测
用 WaitConti 等待传感器连续 ON 2 秒;若在限定时间内未检测到(false),则用 ShowError 弹出错误对话框后结束本步。WaitConti(waitOn, timeOut, contiTime) 会最多等待 timeOut,直到信号在目标状态连续保持 contiTime;若中途信号中断,持续时间重新计数(忽略瞬间抖动)。
xscript
// 等待最多 10 秒,直到传感器连续 ON 满 2 秒(2000ms)
if (IO[cnvSensorName].WaitConti(true, 10000, 2000) == false)
{
// 超时 — 未检测到料碗。弹出错误对话框后结束本步
ShowError(EB_Reset, 100, "conveyor bowl detect timeout");
return false;
}
Log($"{cnvSensorName} : bowl detected");若只想立即检查"当前是否已连续 ON 2 秒"而不等待,请用
CheckContiOn。ShowError(按钮集, 代码, 消息)显示错误对话框 — 参见 SYS。
事件函数关联
在 IO Editor 中指定,或在脚本初始化时通过 EventFunctionName 关联。
xscript
FUNCTION LinkCheckFunction()
{
IO["I_U1CnvBowlDetect"].EventFunctionName = "IOEvent_CnvBowlSens";
IO["I_U2CnvBowlDetect"].EventFunctionName = "IOEvent_CnvBowlSens";
}
FUNCTION IOEvent_CnvBowlSens(string name)
{
if (IO[name] == ON)
{
// 传感器 ON 时的处理
}
return true;
}方法
| 方法 | 说明 |
|---|---|
bool ReadBit(void) | 读取当前位值 |
bool WriteBit(bool SetOn) | 位输出 |
bool ToggleBit(void) | 反转当前值 |
bool Wait(bool waitOn, int TimeOut) | 等待至指定状态(ms) |
bool WaitConti(bool waitOn, int TimeOut, int ContiTime) | 等待至指定状态持续 ContiTime 以上 |
bool CheckContiOn(int timeMsec) | 立即检查过去 timeMsec 内是否一直为 ON(不等待) |
bool CheckContiOff(int timeMsec) | 立即检查过去 timeMsec 内是否一直为 OFF(不等待) |
bool Contains(string keyword) | 名称是否包含关键词 |
bool InitBoard(void) | 板卡初始化 |
bool SetDelayedOn(int delay) / bool SetDelayedOff(int delay) | ON/OFF 延迟设置(ms) |
void RunEventFuncion(bool onoff) | 手动执行事件函数 |
void ClearLinkModule(void) | 解除已关联的事件函数 |
属性
| 属性 | 类型 | 说明 |
|---|---|---|
Name · ID · Description | string | 标识 |
BoardType · Module · Index | string/int | 物理位置 |
GroupName | string | 分组 |
Inverse | bool | 是否信号反转 |
CheckFunctionName · EventFunctionName | string | 关联函数名 |
Message | string | UI 显示信息 |
SubModel | string | 型号区分 |
InitOk | bool | 初始化完成 |
Info | string | 调试信息 |
提示
- 避免直接的
ReadBit()轮询循环,改用Wait(true, timeout)→ 降低 CPU 占用。 - 要等待信号稳定用
WaitConti;要立即检查当前是否已稳定用CheckContiOn·CheckContiOff。 - 设置了
Inverse = true的信号,在脚本中已按反转后的值处理。 - 逻辑时序关键之处,用
SetDelayedOn/Off确保硬件去抖时间。