wordpress 做大型网站,搜索引擎营销ppt,全国企业名录,莱特币做空 网站文章目录 背景解决方案注意事项 背景
目前在利用open62541.h/open62541.c编写了一个与PLC进行OPCUA通讯的上位机程序。 上位机这边会定时对PLC的某个opcua变量进行写操作。但是假如PLC离线或者说拔掉网线#xff0c;上位机就会直接崩溃死机#xff0c;并且报如下的错误… 文章目录 背景解决方案注意事项 背景
目前在利用open62541.h/open62541.c编写了一个与PLC进行OPCUA通讯的上位机程序。 上位机这边会定时对PLC的某个opcua变量进行写操作。但是假如PLC离线或者说拔掉网线上位机就会直接崩溃死机并且报如下的错误
[2024-08-12 10:07:25.528 (UTC0800)] warn/channel Connection 2516 | SecureChannel 28 | Receiving the response failed with StatusCode BadConnectionClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/client Received Publish Response with code BadSecureChannelClosed
[2024-08-12 10:07:25.528 (UTC0800)] warn/channel Connection 0 | SecureChannel 0 | Could not receive with StatusCode BadConnectionClosed
[2024-08-12 10:07:25.528 (UTC0800)] info/client Client Status: ChannelState: Closed, SessionState: Created, ConnectStatus: Good有没有什么办法可以监控client的状态了解其是什么时候掉线掉线了我就不写就行了。
解决方案
幸亏是有这么一个回调函数的参考官方的例子【open62541/examples/client_async.c】可以发现我们可以对UA_ClientConfig中的stateCallback进行赋值也就是注册一个状态回调函数从而获取客户端的状态变化。
static void
onConnect(UA_Client *client, UA_SecureChannelState channelState,UA_SessionState sessionState, UA_StatusCode connectStatus) {printf(Async connect returned with status code %s\n,UA_StatusCode_name(connectStatus));
}---UA_ClientConfig *cc UA_Client_getConfig(client);cc-stateCallback onConnect;一般检测channelState就行
// 状态变化回调函数可以通过这个监测客户端是否断开连接
// 此回调函数的线程貌似就是UA_Client_run_iterate所在的线程起始应该不是恐怕是在哪个线程调用了UA_Client相关的函数就在那个线程
static void
onStateChanged(UA_Client *client,UA_SecureChannelState channelState,UA_SessionState sessionState,UA_StatusCode connectStatus)
{if(channelState UA_SECURECHANNELSTATE_CLOSED) // 连接已断开{qDebug() callback thread: QThread::currentThread();qDebug() 连接已断开-------- QDateTime::currentDateTime();---}
}注意事项
这个opcua库对多线程的处理比较差很容易造成冲突。一定要自己加个线程锁否则程序很容易就崩掉。 参考 【open62541/examples/client_async.c】