TOnDisconnectedCB
This callback function is used to check whether the connection with the robot controller has been terminated by an external cause or user intervention.
See Rules for callback functions.
Defined in: DRFLEx.h
// typedef (DRFL.h)
typedef void (*TOnDisconnectedCB)();
// registration API (internal + wrapper)
DRFL_API void _set_on_disconnected(LPROBOTCONTROL pCtrl, TOnDisconnectedCB pCallbackFunc);
void set_on_disconnected(TOnDisconnectedCB pCallbackFunc)
{
_set_on_disconnected(_rbtCtrl, pCallbackFunc);
};
Parameter
None
Return
None
Example
#include "DRFLEx.h"
#include <atomic>
#include <chrono>
#include <iostream>
#include <thread>
using namespace DRAFramework;
using namespace std;
CDRFLEx drfl;
atomic<bool> g_bDisconnected(false);
// Callback triggered when the robot controller connection is lost
void OnDisconnectedCB()
{
// Only record the event; main() reconnects
g_bDisconnected = true;
}
void OnMonitoringStateCB(const ROBOT_STATE eState)
{
cout << "[STATE] " << (int)eState << endl;
}
void RegisterCallbacks()
{
drfl.set_on_monitoring_state(OnMonitoringStateCB);
}
int main()
{
// Connect to the robot controller
if (!drfl.open_connection("192.168.137.100")) {
cout << "Failed to connect to controller." << endl;
return -1;
}
// Register disconnection callback; it stays registered after a disconnect
drfl.set_on_disconnected(OnDisconnectedCB);
RegisterCallbacks();
drfl.setup_monitoring_version(1);
// Keep program alive to handle potential disconnects
while (true)
{
if (g_bDisconnected.exchange(false)) {
cout << "[DISCONNECT] Lost connection with the robot controller." << endl;
if (drfl.open_connection("192.168.137.100")) {
cout << "[RECONNECT] Connection re-established successfully." << endl;
// The other callbacks were unregistered: register them again
RegisterCallbacks();
drfl.setup_monitoring_version(1);
}
else {
cout << "[ERROR] Failed to reconnect. Check network or controller power." << endl;
g_bDisconnected = true; // retry on the next pass
}
}
this_thread::sleep_for(chrono::seconds(1));
}
drfl.close_connection();
return 0;
}
Notes
This callback is invoked when the network session or controller link is terminated.
Use it to log the event, alert the user, or signal the application to reconnect. Call
open_connectionoutside the callback, as in the example.When a connection ends (
close_connection, loss of connection, oropen_connectionon an object that is already connected), every callback registered with aset_on_*function is unregistered, except those registered withset_on_disconnected,set_on_monitoring_ctrl_io_ex(with DRCF_VERSION 3, the default),set_on_rt_monitoring_dataandset_on_rt_log_alarm, which stay registered. After each successfulopen_connection, register the other callbacks again and callsetup_monitoring_versionagain.open_connectionon a connected object closes the existing connection first and prints “Already connected : <ip>”.If no data is received from the controller for about 2 seconds, the connection ends and the disconnect callback is called. See open_connection.
After the connection has ended, call only
open_connection.