Skip to content

Commit

Permalink
fix serialport clear recvbuff when protocol error
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanshudong committed Nov 26, 2024
1 parent 6d1831b commit 15888ee
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 13 deletions.
41 changes: 31 additions & 10 deletions unit-test/util/test_tc_serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,17 +158,37 @@ TEST_F(UtilSerialPortTest, test1)
}
}

TC_NetWorkBuffer::PACKET_TYPE onParser2(TC_NetWorkBuffer &buffer, vector<char> &data)
TC_NetWorkBuffer::PACKET_TYPE onParser2(TC_NetWorkBuffer &buffer, vector<char> &out)
{
LOG_CONSOLE_DEBUG << "onParser2:" << buffer.getBufferLength() << endl;
if(buffer.empty())
{
return TC_NetWorkBuffer::PACKET_LESS;
}
// LOG_DEBUG << "onSerialParser:" << buffer.getBufferLength() << endl;
if(buffer.empty() || buffer.getBufferLength() < 4)
{
return TC_NetWorkBuffer::PACKET_LESS;
}

data = buffer.getBuffers();
buffer.moveHeader(data.size());
return TC_NetWorkBuffer::PACKET_FULL;
out = buffer.getBuffers();

if(out[0] == (char)0x64 || out[0] == 'v' || out[0] == 'p')
{
out = vector<char>(out.begin(), out.begin() + 4);
}
else if(out[0] == (char)0x2f || out[0] == (char)0x47)
{
if(out.size() < 6)
{
return TC_NetWorkBuffer::PACKET_LESS;
}
out = vector<char>(out.begin(), out.begin() + 6);
}
else
{
LOG_CONSOLE_DEBUG << "onSerialParser unknown packet size:" << out.size() << ", data:" << TC_Common::bin2str(out.data(), out.size()) << endl;
return TC_NetWorkBuffer::PACKET_ERR;
}

buffer.moveHeader(out.size());
LOG_CONSOLE_DEBUG << "onSerialParser size:" << out.size() << ", data:" << TC_Common::bin2str(out.data(), out.size()) << endl;
return TC_NetWorkBuffer::PACKET_FULL;
}

TEST_F(UtilSerialPortTest, test2)
Expand All @@ -192,7 +212,8 @@ TEST_F(UtilSerialPortTest, test2)
options.parity = 0;

shared_ptr<TC_SerialPort> serialPort = serialPortGroup.create(options, onParser2, make_shared<SerialPortCallback>());
string msg_send = {0x2f,0x44,0x1f,0x1f,0x1f,0x23};
// string msg_send = {0x2f,0x44,0x1f,0x1f,0x1f,0x23};
string msg_send = {0x2f,0x47,0x57,0x00,0x01,0x23};

// string msg_send = { 0x7e, 0x00, 0x08, 0x01, 0x00, 0x02, 0x01, (char)0xab, (char)0xcd };

Expand Down
17 changes: 14 additions & 3 deletions util/src/tc_serialport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ TC_SerialPort::~TC_SerialPort()

void TC_SerialPort::close()
{
_sendBuffer.clearBuffers();
_recvBuffer.clearBuffers();
_buffRecv.clear();

#if TARGET_PLATFORM_WINDOWS

if( _osRead.hEvent != NULL )
Expand All @@ -249,7 +253,6 @@ void TC_SerialPort::close()
#else
if (_serialFd >= 0)
{

::close(_serialFd);
_serialFd = -1;
}
Expand Down Expand Up @@ -307,8 +310,6 @@ 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 @@ -731,6 +732,7 @@ void TC_SerialPort::recvSucc(uint32_t len)
}
catch (exception & ex)
{
close();
auto callback = getCallbackPtr();
if (callback)
{
Expand Down Expand Up @@ -819,6 +821,11 @@ void TC_SerialPort::doRequest()
int TC_SerialPort::send(const void *buf, uint32_t len)
{
#if TARGET_PLATFORM_WINDOWS
if(!isValid())
{
return 0;
}

if(len == 0)
{
return 0;
Expand Down Expand Up @@ -856,6 +863,10 @@ int TC_SerialPort::send(const void *buf, uint32_t len)

int TC_SerialPort::recv()
{
if(!isValid())
{
return 0;
}
DWORD dwErrorFlags;
COMSTAT ComStat;
DWORD dwBytesRead;
Expand Down

0 comments on commit 15888ee

Please sign in to comment.