4.7 C99 Signals IO 接口
概述
C99 Signals 提供单点读、单点写、批量读、批量写和按时间间隔触发 IO 脉冲。端口号按控制器侧编号传入。
对应头文件:
include/c_arm_signals.h
接口签名
Arm_Signals_Read
c
int Arm_Signals_Read(ArmHandle* h, int signalType, int index, double* outValue);| 项 | 说明 |
|---|---|
| 描述 | 读取单路 IO 信号。 |
| 请求参数 | h : ArmHandle* ,C99 会话句柄,通常来自 Arm_Create() ,业务接口需要先连接成功signalType : int ,信号类型,取值见支持范围表index : int ,IO 端口编号outValue : double* ,输出信号值指针 |
| 返回值 | STATUS_CODE 整数值; 0 表示成功,其他值按状态码处理 |
Arm_Signals_WriteInt
c
int Arm_Signals_WriteInt(ArmHandle* h, int signalType, int index, int value);| 项 | 说明 |
|---|---|
| 描述 | 写入整型 IO 信号。 |
| 请求参数 | h : ArmHandle* ,C99 会话句柄,通常来自 Arm_Create() ,业务接口需要先连接成功signalType : int ,信号类型,仅支持 DO 、 RO 、 GO 、 TDO 对应取值index : int ,IO 端口编号value : int ,整型 IO 写入值 |
| 返回值 | STATUS_CODE 整数值; 0 表示成功,其他值按状态码处理 |
Arm_Signals_WriteFloat
c
int Arm_Signals_WriteFloat(ArmHandle* h, int signalType, int index, double value);| 项 | 说明 |
|---|---|
| 描述 | 写入浮点 IO 信号。 |
| 请求参数 | h : ArmHandle* ,C99 会话句柄,通常来自 Arm_Create() ,业务接口需要先连接成功signalType : int ,信号类型,仅支持 AO 对应取值index : int ,IO 端口编号value : double ,浮点 IO 写入值 |
| 返回值 | STATUS_CODE 整数值; 0 表示成功,其他值按状态码处理 |
Arm_Signals_MultiRead
c
int Arm_Signals_MultiRead(ArmHandle* h, int signalType, const int* portList, size_t portCount, int* outValues, size_t maxCount, size_t* outCount);| 项 | 说明 |
|---|---|
| 描述 | 批量读取 IO 信号。 |
| 请求参数 | h : ArmHandle* ,C99 会话句柄,通常来自 Arm_Create() ,业务接口需要先连接成功signalType : int ,信号类型,仅支持 DO 对应取值portList : const int* ,端口编号数组portCount : size_t ,端口编号数量outValues : int* ,批量输出数组,由调用方分配maxCount : size_t ,输出数组容量,表示调用方最多可接收多少个元素outCount : size_t* ,输出数量指针,成功时写入实际数量 |
| 返回值 | STATUS_CODE 整数值; 0 表示成功,其他值按状态码处理 |
Arm_Signals_MultiWrite
c
int Arm_Signals_MultiWrite(ArmHandle* h, int signalType, const int* ioList, size_t ioCount);| 项 | 说明 |
|---|---|
| 描述 | 批量写入 IO 信号。 |
| 请求参数 | h : ArmHandle* ,C99 会话句柄,通常来自 Arm_Create() ,业务接口需要先连接成功signalType : int ,信号类型,仅支持 DO 对应取值ioList : const int* ,扁平 IO 写入数组,按 [port, value, port, value] 组织ioCount : size_t ,扁平 IO 数组元素数量 |
| 返回值 | STATUS_CODE 整数值; 0 表示成功,其他值按状态码处理 |
Arm_Signals_TriggerIOWithIntervals
c
int Arm_Signals_TriggerIOWithIntervals(ArmHandle* h, int inPort, const int* intervals, size_t intervalCount, const int* outPorts, size_t outPortCount, int pulseDuration);| 项 | 说明 |
|---|---|
| 描述 | 按时间间隔触发 IO 输出脉冲。 |
| 请求参数 | h : ArmHandle* ,C99 会话句柄,通常来自 Arm_Create() ,业务接口需要先连接成功inPort : int ,输入端口编号intervals : const int* ,触发间隔数组intervalCount : size_t ,触发间隔数量outPorts : const int* ,输出端口编号数组outPortCount : size_t ,输出端口数量pulseDuration : int ,输出脉冲持续时间 |
| 返回值 | STATUS_CODE 整数值; 0 表示成功,其他值按状态码处理 |
参数与规则
支持范围
| 接口 | 支持范围 |
|---|---|
Arm_Signals_Read | DI 、 DO 、 UI 、 UO 、 RI 、 RO 、 GI 、 GO 、 TAI 、 TDI 、 TDO 、 AI 、 AO |
Arm_Signals_WriteInt | DO 、 RO 、 GO 、 TDO |
Arm_Signals_WriteFloat | AO |
Arm_Signals_MultiRead | DO |
Arm_Signals_MultiWrite | DO |
信号类型取值
| C 常量 | 值 | 说明 |
|---|---|---|
ARM_SIGNAL_DI | 1 | 数字输入 |
ARM_SIGNAL_DO | 2 | 数字输出 |
ARM_SIGNAL_UI | 3 | 专用输入 |
ARM_SIGNAL_UO | 4 | 专用输出 |
ARM_SIGNAL_RI | 5 | 远程输入 |
ARM_SIGNAL_RO | 6 | 远程输出 |
ARM_SIGNAL_GI | 7 | 组输入 |
ARM_SIGNAL_GO | 8 | 组输出 |
ARM_SIGNAL_TAI | 9 | 手腕模拟输入 |
ARM_SIGNAL_TDI | 10 | 手腕数字输入 |
ARM_SIGNAL_TDO | 11 | 手腕数字输出 |
ARM_SIGNAL_AI | 12 | 模拟输入 |
ARM_SIGNAL_AO | 13 | 模拟输出 |
| 项 | 规则 |
|---|---|
signalType | 使用信号类型取值,例如 ARM_SIGNAL_DI 、 ARM_SIGNAL_DO 、 ARM_SIGNAL_AI 、 ARM_SIGNAL_AO |
index | 控制器侧端口编号,按控制器实际编号传入 |
WriteInt | 用于整型输出;不支持的信号类型返回 UNSUPPORTED_SIGNAL_TYPE |
WriteFloat | 用于模拟输出;不支持的信号类型返回 UNSUPPORTED_SIGNAL_TYPE |
MultiRead | outValues + maxCount + outCount 返回批量值;当前仅支持 DO |
MultiWrite | ioList 是扁平数组,按 [port, value, port, value] 传入 |
TriggerIOWithIntervals | 根据输入端口和时间间隔触发输出端口脉冲, pulseDuration 单位为毫秒 |
失败口径:
| 场景 | 返回 |
|---|---|
| 信号类型不在支持范围内 | UNSUPPORTED_SIGNAL_TYPE |
portList 为空或 portCount 为 0 | INVALID_PARAMETER |
Arm_Signals_MultiRead() 输出容量不足 | BUFFER_TOO_SMALL |
ioList 为空、元素个数为奇数或 ioCount 为 0 | INVALID_PARAMETER |
intervals / outPorts 为空或数量为 0 | INVALID_PARAMETER |
Arm_Signals_TriggerIOWithIntervals() 会按 intervals 指定的时间间隔触发 outPorts 输出脉冲; pulseDuration 表示每次输出脉冲持续时间。
最小调用示例
c
#include <stdio.h> // 引入 printf,用于打印 DI 值
#include "c_arm_api.h" // 引入 C99 SDK 总头文件
int main(void) // 示例程序入口
{ // 进入示例主函数
ArmHandle* h = Arm_Create(); // 创建 C99 会话句柄
double value = 0.0; // 准备单点读取输出变量
if (h == NULL) { // 判断句柄是否创建失败
return 1; // 创建失败时退出
} // 结束句柄判断
if (Arm_Connect(h, "10.27.1.2", "10.27.1.102") != 0) { // 连接控制器
Arm_Destroy(h); // 连接失败时释放句柄
return 1; // 返回错误
} // 结束连接判断
int ret = Arm_Signals_Read(h, ARM_SIGNAL_DI, 0, &value); // 读取 DI0
printf("di0=%f\n", value); // 打印 DI0 值
Arm_Disconnect(h); // 断开连接
Arm_Destroy(h); // 销毁句柄
return ret == 0 ? 0 : 1; // 根据读取结果返回
} // 结束示例主函数场景化示例
单点与批量读取
c
double diValue = 0.0; // 准备单点 DI 输出变量
int ports[2] = {0, 1}; // 准备 DO 读取端口列表
int values[2] = {0}; // 准备批量读取输出数组
size_t outCount = 0U; // 准备接收实际输出数量
int readRet = Arm_Signals_Read(h, ARM_SIGNAL_DI, 0, &diValue); // 读取单点 DI
int multiRet = Arm_Signals_MultiRead(h, ARM_SIGNAL_DO, ports, 2, values, 2, &outCount); // 批量读取 DO
(void)readRet; // 示例中保留单点读取状态码
(void)multiRet; // 示例中保留批量读取状态码写入和触发
c
int ioList[4] = {1, 1, 2, 0}; // 准备批量写入扁平数组:DO1=1,DO2=0
int intervals[2] = {100, 200}; // 准备触发间隔,单位按控制器接口定义
int outPorts[2] = {1, 2}; // 准备输出端口列表
/* int writeIntRet = Arm_Signals_WriteInt(h, ARM_SIGNAL_DO, 1, 1); */ // 写 DO 会改变输出状态,确认后再执行
/* int writeFloatRet = Arm_Signals_WriteFloat(h, ARM_SIGNAL_AO, 1, 1.5); */ // 写 AO 会改变模拟输出,确认后再执行
/* int multiWriteRet = Arm_Signals_MultiWrite(h, ARM_SIGNAL_DO, ioList, 4); */ // 批量写 DO,确认后再执行
/* int triggerRet = Arm_Signals_TriggerIOWithIntervals(h, 0, intervals, 2, outPorts, 2, 50); */ // 触发 IO 脉冲,确认后再执行示例代码
cpp
#include <stdio.h>
extern "C" {
#include "c_arm_api.h"
}
int main(void)
{
// [ZH] 本示例直接在源码中写死连接地址,不解析命令行参数。
// [EN] This example hard-codes the connection addresses in the source code and does not parse command-line arguments.
// [ZH] 创建并连接 SDK 句柄。
// [EN] Create the SDK handle and connect to the robot.
ArmHandle* handle = Arm_Create();
if (handle == NULL) {
printf("[c99_signals] 创建句柄失败 / Failed to create the handle\n");
return 1;
}
int ret = Arm_Connect(handle, "10.27.1.2", "10.27.1.102");
if (ret != 0) {
printf("[c99_signals] 连接失败 / Connect failed, 状态码 / Status code: %d\n", ret);
Arm_Destroy(handle);
return 1;
}
printf("[c99_signals] 机器人连接成功 / Robot connected successfully\n");
// [ZH] 读取单路信号与批量信号。
// [EN] Read a single signal and a batch of signals.
double diValue = 0.0;
int ports[2] = {0, 1};
int values[2] = {0, 0};
size_t outCount = 0U;
ret = Arm_Signals_Read(handle, ARM_SIGNAL_DI, 0, &diValue);
printf("[c99_signals] Read 状态码 / Read status code: %d, DI[0]=%.6f\n", ret, diValue);
ret = Arm_Signals_MultiRead(handle, ARM_SIGNAL_DI, ports, 2U, values, 2U, &outCount);
printf("[c99_signals] MultiRead 状态码 / MultiRead status code: %d, 数量 / Count: %zu, 值 / Values: [%d, %d]\n",
ret,
outCount,
values[0],
values[1]);
// [ZH] 顺序执行全部写接口。
// [EN] Execute all write APIs in sequence.
ret = Arm_Signals_WriteInt(handle, ARM_SIGNAL_DO, 1, 1);
printf("[c99_signals] WriteInt 状态码 / WriteInt status code: %d\n", ret);
ret = Arm_Signals_WriteFloat(handle, ARM_SIGNAL_AO, 1, 1.5);
printf("[c99_signals] WriteFloat 状态码 / WriteFloat status code: %d\n", ret);
int ioList[4] = {4, 1, 6, 0};
ret = Arm_Signals_MultiWrite(handle, ARM_SIGNAL_DO, ioList, 4U);
printf("[c99_signals] MultiWrite 状态码 / MultiWrite status code: %d\n", ret);
int intervals[2] = {100, 200};
int outPorts[2] = {4, 5};
ret = Arm_Signals_TriggerIOWithIntervals(handle, 1, intervals, 2U, outPorts, 2U, 60);
printf("[c99_signals] TriggerIOWithIntervals 状态码 / TriggerIOWithIntervals status code: %d\n", ret);
// [ZH] 断开连接并销毁句柄。
// [EN] Disconnect and destroy the handle.
Arm_Disconnect(handle);
Arm_Destroy(handle);
printf("[c99_signals] 示例结束 / Example finished\n");
return 0;
}