Skip to content

Commit

Permalink
feat: serialport add onHearbeat callback
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanshudong committed Dec 9, 2024
1 parent a36ce56 commit 498b289
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
18 changes: 17 additions & 1 deletion util/include/util/tc_serialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ class UTIL_DLL_API TC_SerialPort
* @brief 连接被关闭
*/
virtual void onClose() = 0;

/**
* @brief 心跳回调, 在串口通信线程中调用, 每当有收发数据时, 都会回调, 最长不超过
*/
virtual void onHeartbeat() {};
};

typedef shared_ptr<RequestCallback> RequestCallbackPtr;
Expand All @@ -150,7 +155,6 @@ class UTIL_DLL_API TC_SerialPort
//协议解析器
using onparser_callback = std::function<TC_NetWorkBuffer::PACKET_TYPE(TC_NetWorkBuffer &, vector<char> &)>;


struct Options
{
std::string portName; //串口地址
Expand Down Expand Up @@ -466,6 +470,12 @@ class UTIL_DLL_API TC_SerialPortGroup
*/
void initialize();

/**
* 设置心跳最大间隔(毫秒), 最小不低于10毫秒
* @param heartbeatMaxInterval
*/
void setHeartbeatMaxInterval(int heartbeatMaxInterval);

/**
* 创建某个串口
* @param options
Expand Down Expand Up @@ -513,6 +523,12 @@ class UTIL_DLL_API TC_SerialPortGroup


protected:

/**
* 心跳最大间隔(毫秒)
*/
int _heartbeatMaxInterval = 100;

#if !TARGET_PLATFORM_WINDOWS

/**
Expand Down
41 changes: 32 additions & 9 deletions util/src/tc_serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ void TC_SerialPortGroup::erase(const shared_ptr<TC_SerialPort> & sp)
}
}

void TC_SerialPortGroup::setHeartbeatMaxInterval(int heartbeatMaxInterval)
{
_heartbeatMaxInterval = heartbeatMaxInterval;
if(_heartbeatMaxInterval < 10)
{
_heartbeatMaxInterval = 10;
}
}

vector<string> TC_SerialPortGroup::getComPorts(const string &prefix)
{
vector<string> comPorts;
Expand Down Expand Up @@ -125,15 +134,22 @@ void TC_SerialPortGroup::run()
{
#if !TARGET_PLATFORM_WINDOWS
_epoller.idle([&]
{
std::lock_guard<std::mutex> lock(_mutex);
for (const auto &e: _serialPorts)
{
e.second->doRequest();
}
});
{
std::lock_guard<std::mutex> lock(_mutex);
for (const auto &e: _serialPorts)
{
e.second->doRequest();

_epoller.loop();
auto callback = e.second->getRequestCallbackPtr();

if(callback)
{
try { callback->onHeartbeat(); } catch(const std::exception& ex) { }
}
}
});

_epoller.loop(_heartbeatMaxInterval);

#else

Expand All @@ -144,7 +160,7 @@ void TC_SerialPortGroup::run()

while(true)
{
bool bFlag = GetQueuedCompletionStatus(_ioPort, &dwNumberOfBytesTransferred, (PULONG_PTR)(void*)&dwCompletionKey, &opOverlapped, 100);
bool bFlag = GetQueuedCompletionStatus(_ioPort, &dwNumberOfBytesTransferred, (PULONG_PTR)(void*)&dwCompletionKey, &opOverlapped, _heartbeatMaxInterval);

if(bFlag && dwCompletionKey == -1)
{
Expand All @@ -160,8 +176,15 @@ void TC_SerialPortGroup::run()

for (const auto &e: serialPorts)
{
auto callback = e.second->getRequestCallbackPtr();

if(callback)
{
try { callback->onHeartbeat(); } catch(const std::exception& ex) { }
}
try
{

if(bFlag)
{
if(opOverlapped == e.second->getOsRead())
Expand Down

0 comments on commit 498b289

Please sign in to comment.