Skip to content

Commit

Permalink
fix tc_serialport interface, use const *, size_t
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanshudong committed Dec 6, 2024
1 parent 6d5ac32 commit a36ce56
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
12 changes: 7 additions & 5 deletions util/include/util/tc_serialport.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,27 +181,29 @@ class UTIL_DLL_API TC_SerialPort

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

/**
* @brief 异步发送buffer(尽量使用这个函数, 减少一次内存copy), 如果无法打开串口, 则抛出异常TC_SerialPortException
* @sendRequest
* @param string & sBuffer
* @param char* sBuffer
* @param bool header, 是否把数据插入到队列头部, 默认数据都在尾部的!
*/
void sendRequest(const std::shared_ptr<TC_NetWorkBuffer::Buffer> & buff, bool header = false);

/**
* @brief 发送buffer, 并等待响应, 如果无法打开串口, 则抛出异常TC_SerialPortException, 需要在RequestCallback::onSucc中, 调用notify来唤醒
* @param string & sBuffer
* @param char* sBuffer
* @param size_t length
* @param vector<char> & response, 响应数据
* @param bool header, 是否把数据插入到队列头部, 默认数据都在尾部的!
* @return 返回cv_status, 如果超时返回cv_status::timeout, 否则返回cv_status::no_timeout
*/
std::cv_status sendRequestAndResponse(const std::string & sBuffer, vector<char> & response, bool header = false, uint32_t timeout = 3000);
std::cv_status sendRequestAndResponse(const char* sBuffer, size_t length, vector<char> & response, bool header = false, uint32_t timeout = 3000);

/**
* @brief 发送buffer(尽量使用这个函数, 减少一次内存copy), 并等待响应, 如果无法打开串口, 则抛出异常TC_SerialPortException,
Expand Down
10 changes: 5 additions & 5 deletions util/src/tc_serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,14 @@ TC_SerialPort::RequestCallbackPtr TC_SerialPort::getRequestCallbackPtr()
return _callbackPtr;
}

void TC_SerialPort::sendRequest(const string & sBuffer, bool header)
void TC_SerialPort::sendRequest(const char* sBuffer, size_t length, bool header)
{
if (sBuffer.empty())
if (sBuffer == nullptr || length == 0)
return;

shared_ptr<TC_NetWorkBuffer::Buffer> buff = std::make_shared<TC_NetWorkBuffer::Buffer>();

buff->addBuffer(sBuffer);
buff->addBuffer(sBuffer, length);

if(!isValid())
{
Expand All @@ -502,11 +502,11 @@ void TC_SerialPort::sendRequest(const shared_ptr<TC_NetWorkBuffer::Buffer> & buf
addSendReqBuffer(buff, header);
}

std::cv_status TC_SerialPort::sendRequestAndResponse(const std::string & buff, vector<char> & response, bool header, uint32_t timeout)
std::cv_status TC_SerialPort::sendRequestAndResponse(const char* sBuffer, size_t length, vector<char> & response, bool header, uint32_t timeout)
{
std::unique_lock<std::mutex> lock(_waitMutex);
_response.clear();
sendRequest(buff, header);
sendRequest(sBuffer, length, header);
auto status = _waitCond.wait_for(lock, std::chrono::milliseconds(timeout));
if(status == std::cv_status::no_timeout)
{
Expand Down

0 comments on commit a36ce56

Please sign in to comment.