Skip to content

Commit

Permalink
feat: serialport will reopen com when sendRequest and comm is close
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanshudong committed Nov 21, 2024
1 parent 504f5ce commit 133f10c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 34 deletions.
18 changes: 14 additions & 4 deletions unit-test/util/test_tc_serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ class SerialPortCallback : public TC_SerialPort::RequestCallback
cnd.notify_one();
}

void onOpen()
{
cout << "onOpen" << endl;
}

void onFailed(const string &info)
{
cout << "info: " << info << endl;
Expand Down Expand Up @@ -96,13 +101,18 @@ TEST_F(UtilSerialPortTest, test)
// 7e000801000201abcd
while(true)
{
try
{
std::unique_lock<std::mutex> lock(mtx);
serialPort->sendRequest(msg_send);
cnd.wait_for(lock, std::chrono::seconds(1));
}
catch(const std::exception& ex)
{
std::unique_lock<std::mutex> lock(mtx);
serialPort->sendRequest(msg_send);
cnd.wait(lock);
cout << "ex: " << ex.what() << endl;
}

TC_Common::sleep(2);
TC_Common::sleep(1);
}
}
catch(const std::exception& e)
Expand Down
73 changes: 46 additions & 27 deletions util/include/util/tc_serialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,33 +34,53 @@ namespace tars
*
* TC_SerialPortGroup::Options options;
* options.portName = ....
*
* shared_ptr<TC_SerialPort> serialPort = serialPortGroup.create(options);
*
* //定义一个协议解析器
TC_NetWorkBuffer::PACKET_TYPE parser(TC_NetWorkBuffer&buff, TC_SerialPort*)
{
const char *p = buff.mergeBuffers();
- 定义回调
const char *pos = TC_Port::strnstr(p, "\r\n", buff.getBufferLength());
class SerialPortCallback : public TC_SerialPort::RequestCallback
{
public:
void onSucc(const vector<char> &data)
{
cout << "data: " << TC_Common::bin2str(data.data(), data.size()) << endl;
}
if(pos == NULL)
void onOpen()
{
return TC_NetWorkBuffer::PACKET_LESS;
cout << "onOpen" << endl;
}
string out(p, pos);
void onFailed(const string &info)
{
cout << "info: " << info << endl;
}
//得到一个完整的串口输出类
void onClose()
{
cout << "onClose" << endl;
}
};
return TC_NetWorkBuffer::PACKET_FULL;
auto callbackPtr = std::make_shared<SerialPortCallback>();
- 定义一个协议解析器
TC_NetWorkBuffer::PACKET_TYPE parser(TC_NetWorkBuffer&buff, vector<char> &out)
{
if(buffer.empty())
{
return TC_NetWorkBuffer::PACKET_LESS;
}
data = buffer.getBuffers();
if(data[data.size() - 1] != 0x0d)
{
return TC_NetWorkBuffer::PACKET_LESS;
}
buffer.moveHeader(data.size());
return TC_NetWorkBuffer::PACKET_FULL;
}
* //设置读取串口后, 数据的解析器
* serialPort->setParserCallback(std::bind(parser, std::placeholders::_1, std::placeholders::_2));
* //发送数据
* shared_ptr<TC_SerialPort> serialPort = serialPortGroup.create(options, onparser, callbackPtr);
- 发送数据
* serialPort->sendRequest(...);
* //在协议解析器中获取读取到的数据
*/

/**
Expand All @@ -85,10 +105,9 @@ class UTIL_DLL_API TC_SerialPort
{
public:
/**
* @brief 异步请求回调对象
* @brief 异步请求回调对象, 除了onOpen可能在业务线程中回调外, 其他都在串口通信线程中回调
* onSucc: 成功回调
* onFailed: 失败回调, 无论什么失败都会被调用掉, 比如onSendTimeout, 也会在onFailed被调用的
* 注意, 一个请求
* onFailed: 失败回调, 无论什么失败都会被调用掉
*/
class RequestCallback
{
Expand Down Expand Up @@ -156,15 +175,14 @@ class UTIL_DLL_API TC_SerialPort
}

/**
* @brief 异步发送buffer
* @sendRequest
* @brief 异步发送buffer, 如果无法打开串口, 则抛出异常TC_SerialPortException
* @param string & sBuffer
* @param bool header, 是否把数据插入到队列头部, 默认数据都在尾部的!
*/
void sendRequest(const std::string & sBuffer, bool header = false);

/**
* @brief 异步发送buffer(尽量使用这个函数, 减少一次内存copy)
* @brief 异步发送buffer(尽量使用这个函数, 减少一次内存copy), 如果无法打开串口, 则抛出异常TC_SerialPortException
* @sendRequest
* @param string & sBu} * @param bool header, 是否把数据插入到队列头部, 默认数据都在尾部的!
*/
Expand All @@ -188,15 +206,16 @@ class UTIL_DLL_API TC_SerialPort
return _cookie;
}

/**
* 初始化串口
*/
void initialize();

protected:

friend class TC_SerialPortGroup;

/**
* 初始化串口
*/
void initialize();

/**
* sendRequest返回值
*/
Expand Down
18 changes: 15 additions & 3 deletions util/src/tc_serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ void TC_SerialPortGroup::run()
}
e.second->handleInputImp();
}
catch(const std::exception& e)
catch(const std::exception& ex)
{
e.second->getCallbackPtr()->onFailed(e.what());
e.second->getCallbackPtr()->onFailed(ex.what());
e.second->close();
}
}
Expand Down Expand Up @@ -260,6 +260,8 @@ void TC_SerialPort::initialize()
throw TC_SerialPortException("Failed to bind io port: " + _options.portName + ", error:" + TC_Exception::getSystemError());
}

_buffRecv.clear();

#else
if (_serialFd >= 0)
{
Expand Down Expand Up @@ -362,7 +364,9 @@ void TC_SerialPort::initialize()

_epollInfo->registerCallback(callbacks, EPOLLIN | EPOLLOUT);
#endif

_sendBuffer.clearBuffers();
_recvBuffer.clearBuffers();

_callbackPtr->onOpen();
}

Expand All @@ -380,6 +384,10 @@ void TC_SerialPort::sendRequest(const string & sBuffer, bool header)

buff->addBuffer(sBuffer);

if(!isValid())
{
initialize();
}
addSendReqBuffer(buff, header);
}

Expand All @@ -388,6 +396,10 @@ void TC_SerialPort::sendRequest(const shared_ptr<TC_NetWorkBuffer::Buffer> & buf
if (buff && buff->empty())
return;

if(!isValid())
{
initialize();
}
addSendReqBuffer(buff, header);
}

Expand Down

0 comments on commit 133f10c

Please sign in to comment.