该示例演示了如何通过 API 接口对各种类型的参数执行读写操作。
如何在Visual Studio、QT、CLion中使用示例工程请参考示例工程使用说明。
// std
#include <iostream>
// alson
#include <alson/classic_client.h>
// 客户端事件监听器
class SimpleClientEventListener : public ALSON::BaseClientEventListener {
public:
void onDisconnectedByException() override {
// 当通信异常断开时,该函数会被调用。这里打印一条消息作为演示。
// 在实际的工程应用中应该及时向用户反馈,待异常解决后执行重连逻辑。
std::cout << "********************* client exception *******************" << std::endl;
}
};
// 设备事件监听器
class SimpleDeviceEventListener : public ALSON::ClassicDeviceEventListener {
public:
void onDeviceException(uint64_t code, const std::string& message) override {
// 当硬件出现异常时,该函数会被调用。这里打印一条消息作为演示。
// 硬件异常包括相机断开连接、串口断开连接等。
// 在实际的工程应用中应该及时向用户反馈,待异常解决后执行重连逻辑。
std::cout << "********************* device exception *******************" << std::endl;
}
};
int main(int argc, char** argv) {
try {
// 开启客户端日志,需要指定日志配置文件。
ALSON::Client::initLog("../../../LogConfig-Client.yaml");
// 检测所有服务端(即可用的设备),并获取其中第一个服务端的信息
std::vector<ALSON::ServerInfo> serverInfoList = ALSON::Client::discovery();
if (serverInfoList.empty()) {
std::cerr << "can not find any server or device" << std::endl;
return -1;
}
ALSON::ServerInfo serverInfo = serverInfoList.front();
// 构造客户端并建立通信连接
ALSON::Client client;
client.connect(serverInfo.getServerNetworkCardInfo().getIp(), serverInfo.getServerNetworkCardInfo().getBindPort());
std::cout << "is connected: " << client.isConnected() << std::endl;
// 心跳时间通常设置为 2000ms 至 3000ms。在开发调试过程中,可适当设置大些。
// 如果心跳时间很大,那么当通信意外断开时,客户端和服务端都需要经历很长的时间才能感知到。
// 在此期间,客户端将阻塞您的进程,服务端不能被新的客户端连接。
client.setHeartbeatTimeout(3000);
// 注册客户端事件监听器
ALSON::BaseClientEventListenerPtr clientEventListener = std::make_shared<SimpleClientEventListener>();
client.setClientEventListener(clientEventListener);
// 创建设备控制器对象
ALSON::ClassicDeviceController deviceController = client.createDeviceController<ALSON::ClassicDeviceController>();
// 注册设备事件监听器
ALSON::ClassicDeviceEventListenerPtr classicDeviceEventListener = std::make_shared<SimpleDeviceEventListener>();
deviceController.setDeviceEventListener(classicDeviceEventListener);
// 打开设备。注意无论设备是否关闭,在调用 DeviceController 中其他接口之前都必须调用 open 接口。
deviceController.open();
// 获取参数组管理器
ALSON::DeviceParameterManager parameterManager = client.createDeviceParameterManager();
// 重置参数组,所有参数会恢复成默认值
parameterManager.resetCurrentValue();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 枚举类型参数的主要操作方法,以曝光模式为例进行演示。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "Enumeration Parameter Exposure Mode" << std::endl;
ALSON::EnumerationParameterNodePtr exposureMode = parameterManager.getEnumerationParameterNode("2dParameters.exposureMode");
std::cout << "ExposureMode.key: " << exposureMode->getKey() << std::endl;
std::cout << "ExposureMode.name: " << exposureMode->getName() << std::endl;
std::cout << "ExposureMode.defaultValue: " << exposureMode->getDefaultValue() << std::endl;
std::cout << "ExposureMode.value: " << exposureMode->getValue() << std::endl;
// A 系列的曝光模式可以设置为 AUTO
std::cout << "Set ExposureMode to AUTO" << std::endl;
exposureMode->setValue("AUTO");
// // C 序列的曝光模式可以设置为 IN_SCAN
// std::cout << "Set ExposureMode as IN_SCAN" << std::endl;
// exposureMode->setValue("IN_SCAN");
std::cout << "ExposureMode.value: " << exposureMode->getValue() << std::endl;
// 将曝光模式重置为默认值
std::cout << "Reset ExposureMode" << std::endl;
exposureMode->resetValue();
std::cout << "ExposureMode.value: " << exposureMode->getValue() << std::endl;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 布尔类型参数的主要操作方法,以打光模式为例进行演示。
/// 注意:只有 A 系列部分型号拥有打光模式参数。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "Boolean Parameter Flash Mode" << std::endl;
ALSON::BooleanParameterNodePtr flashMode = parameterManager.getBooleanParameterNode("2dParameters.flashMode");
std::cout << "FlashMode.key: " << flashMode->getKey() << std::endl;
std::cout << "FlashMode.name: " << flashMode->getName() << std::endl;
std::cout << "FlashMode.defaultValue: " << flashMode->getDefaultValue() << std::endl;
std::cout << "FlashMode.value: " << flashMode->getValue() << std::endl;
// 开启打光模式
std::cout << "Set FlashMode to true" << std::endl;
flashMode->setValue(true);
std::cout << "FlashMode.value: " << flashMode->getValue() << std::endl;
// 将打光模式重置为默认值
std::cout << "Reset FlashMode" << std::endl;
flashMode.reset();
std::cout << "FlashMode.value: " << flashMode->getValue() << std::endl;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 整数类型参数的主要操作方法,以 2d 增益为例进行演示。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "Integer Parameter Gain" << std::endl;
ALSON::IntegerParameterNodePtr gain = parameterManager.getIntegerParameterNode("2dParameters.gain");
std::cout << "Gain.key: " << gain->getKey() << std::endl;
std::cout << "Gain.name: " << gain->getName() << std::endl;
std::cout << "Gain.defaultValue: " << gain->getDefaultValue() << std::endl;
std::cout << "Gain.maxValue: " << gain->getMaxValue() << std::endl;
std::cout << "Gain.minValue: " << gain->getMinValue() << std::endl;
std::cout << "Gain.value: " << gain->getValue() << std::endl;
// 将增益设置为 10
std::cout << "Set Gain to 10" << std::endl;
gain->setValue(10);
std::cout << "Gain.value: " << gain->getValue() << std::endl;
// 将增益重置为默认值
std::cout << "Reset Gain" << std::endl;
gain.reset();
std::cout << "Gain.value: " << gain->getValue() << std::endl;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 浮点数类型参数的主要操作方法,以 gamma 为例进行演示。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "Float Parameter Gamma" << std::endl;
ALSON::FloatParameterNodePtr gamma = parameterManager.getFloatParameterNode("2dParameters.gamma");
std::cout << "Gamma.key: " << gamma->getKey() << std::endl;
std::cout << "Gamma.name: " << gamma->getName() << std::endl;
std::cout << "Gamma.defaultValue: " << gamma->getDefaultValue() << std::endl;
std::cout << "Gamma.maxValue: " << gamma->getMaxValue() << std::endl;
std::cout << "Gamma.minValue: " << gamma->getMinValue() << std::endl;
std::cout << "Gamma.value: " << gamma->getValue() << std::endl;
// 将 gamma 设置为 10
std::cout << "Set Gamma to 0.8" << std::endl;
gamma->setValue(0.8f);
std::cout << "Gamma.value: " << gamma->getValue() << std::endl;
// 将 gamma 重置为默认值
std::cout << "Reset Gamma" << std::endl;
gamma.reset();
std::cout << "Gamma.value: " << gamma->getValue() << std::endl;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// 数组类型参数的主要操作方法,以 3d 曝光时间数组为例进行演示。
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "Array Parameter Exposure Time Array" << std::endl;
ALSON::ArrayParameterNodePtr exposureTimeArray = parameterManager.getArrayParameterNode("3dParameters.exposureTimeArray");
std::cout << "ExposureTimeArray.key: " << exposureTimeArray->getKey() << std::endl;
std::cout << "ExposureTimeArray.name: " << exposureTimeArray->getName() << std::endl;
// 数组容量,即当前数组中最多可以添加几个曝光时间。
std::cout << "ExposureTimeArray.capacity: " << exposureTimeArray->getCapacity() << std::endl;
// 数组大小,即当前数组中已经设置了几个曝光时间。
std::cout << "ExposureTimeArray.size: " << exposureTimeArray->getSize() << std::endl;
// 操作第一个曝光时间
std::cout << "First Exposure Time" << std::endl;
ALSON::IntegerParameterNodePtr exposureTimeArray0 = parameterManager.getIntegerParameterNode("3dParameters.exposureTimeArray[0]");
std::cout << "ExposureTimeArray[0].key: " << exposureTimeArray0->getKey() << std::endl;
std::cout << "ExposureTimeArray[0].name: " << exposureTimeArray0->getName() << std::endl;
std::cout << "ExposureTimeArray[0].defaultValue: " << exposureTimeArray0->getDefaultValue() << std::endl;
std::cout << "ExposureTimeArray[0].maxValue: " << exposureTimeArray0->getMaxValue() << std::endl;
std::cout << "ExposureTimeArray[0].minValue: " << exposureTimeArray0->getMinValue() << std::endl;
std::cout << "ExposureTimeArray[0].value: " << exposureTimeArray0->getValue() << std::endl;
// 将第一个曝光时间设置为 5000
std::cout << "Set ExposureTimeArray[0] to 5000" << std::endl;
exposureTimeArray0->setValue(5000);
std::cout << "ExposureTimeArray[0].value: " << exposureTimeArray0->getValue() << std::endl;
// 将第一个曝光时间重置为默认值
std::cout << "Reset ExposureTimeArray[0]" << std::endl;
exposureTimeArray0.reset();
std::cout << "ExposureTimeArray[0].value: " << exposureTimeArray0->getValue() << std::endl;
// 添加第二个曝光时间并操作
std::cout << "Second Exposure Time" << std::endl;
std::cout << "Add element" << std::endl;
// addElement 接口添加的曝光时间的值与前一个曝光时间相同
exposureTimeArray->addElement();
// 数组容量,即当前数组中最多可以添加几个曝光时间。
std::cout << "ExposureTimeArray.capacity: " << exposureTimeArray->getCapacity() << std::endl;
// 数组大小,即当前数组中已经设置了几个曝光时间。
std::cout << "ExposureTimeArray.size: " << exposureTimeArray->getSize() << std::endl;
// 获取第二个曝光时间
ALSON::IntegerParameterNodePtr exposureTimeArray1 = parameterManager.getIntegerParameterNode("3dParameters.exposureTimeArray[1]");
std::cout << "ExposureTimeArray[1].value: " << exposureTimeArray1->getValue() << std::endl;
// 将第二个曝光时间设置为 5000
std::cout << "Set ExposureTimeArray[1] to 5000" << std::endl;
exposureTimeArray1->setValue(5000);
std::cout << "ExposureTimeArray[1].value: " << exposureTimeArray1->getValue() << std::endl;
std::cout << "Third Exposure Time" << std::endl;
std::cout << "Add element" << std::endl;
// 添加第三个曝光时间
exposureTimeArray->addElement();
// 数组容量,即当前数组中最多可以添加几个曝光时间。
std::cout << "ExposureTimeArray.capacity: " << exposureTimeArray->getCapacity() << std::endl;
// 数组大小,即当前数组中已经设置了几个曝光时间。
std::cout << "ExposureTimeArray.size: " << exposureTimeArray->getSize() << std::endl;
// 获取第三个曝光时间
ALSON::IntegerParameterNodePtr exposureTimeArray2 = parameterManager.getIntegerParameterNode("3dParameters.exposureTimeArray[2]");
std::cout << "ExposureTimeArray[2].value: " << exposureTimeArray2->getValue() << std::endl;
// 删除第三个曝光时间
std::cout << "Delete ExposureTimeArray[2]" << std::endl;
exposureTimeArray->deleteElementByIndex(2);
// 数组容量,即当前数组中最多可以添加几个曝光时间。
std::cout << "ExposureTimeArray.capacity: " << exposureTimeArray->getCapacity() << std::endl;
// 数组大小,即当前数组中已经设置了几个曝光时间。
std::cout << "ExposureTimeArray.size: " << exposureTimeArray->getSize() << std::endl;
// 重置曝光时间数组
std::cout << "Reset Exposure Time Array" << std::endl;
exposureTimeArray.reset();
// 数组容量,即当前数组中最多可以添加几个曝光时间。
std::cout << "ExposureTimeArray.capacity: " << exposureTimeArray->getCapacity() << std::endl;
// 数组大小,即当前数组中已经设置了几个曝光时间。
std::cout << "ExposureTimeArray.size: " << exposureTimeArray->getSize() << std::endl;
// 关闭设备。注意:close 接口一般情况下不需要调用,如果该接口被调用,下次调用 open 接口的时候就会比较耗时。
deviceController.close();
// 如果不再需要连接服务端,请及时调用 disconnect 断开连接。
client.disconnect();
}
catch (ALSON::CommonException& cause) {
// API 中的所有接口都有可能抛出异常,请按照这种方式进行捕获。否则异常发生时可能会导致程序崩溃。
std::cerr << cause.getStackTrace() << std::endl;
return -1;
}
return 0;
}