diff --git a/unit-test/util/test_tc_serialport.cpp b/unit-test/util/test_tc_serialport.cpp index e7f52b7a..46f206d2 100644 --- a/unit-test/util/test_tc_serialport.cpp +++ b/unit-test/util/test_tc_serialport.cpp @@ -59,7 +59,7 @@ class SerialPortCallback : public TC_SerialPort::RequestCallback } }; -TC_NetWorkBuffer::PACKET_TYPE onParser(TC_NetWorkBuffer &buffer, vector &data) +TC_NetWorkBuffer::PACKET_TYPE onParser1(TC_NetWorkBuffer &buffer, vector &data) { if(buffer.empty()) { @@ -85,7 +85,7 @@ TEST_F(UtilSerialPortTest, list) cout << TC_Common::tostr(comPorts.begin(), comPorts.end(), ", ") << endl; } -TEST_F(UtilSerialPortTest, test) +TEST_F(UtilSerialPortTest, test1) { #if TARGET_PLATFORM_WINDOWS WSADATA wsadata; @@ -105,7 +105,7 @@ TEST_F(UtilSerialPortTest, test) options.stopBits = 0; options.parity = 0; - shared_ptr serialPort = serialPortGroup.create(options, onParser, make_shared()); + shared_ptr serialPort = serialPortGroup.create(options, onParser1, make_shared()); string msg_send = { 0x7e, 0x00, 0x08, 0x01, 0x00, 0x02, 0x01, (char)0xab, (char)0xcd }; // 7e000801000201abcd @@ -130,3 +130,64 @@ TEST_F(UtilSerialPortTest, test) std::cerr << e.what() << '\n'; } } + +TC_NetWorkBuffer::PACKET_TYPE onParser2(TC_NetWorkBuffer &buffer, vector &data) +{ + LOG_CONSOLE_DEBUG << "onParser2:" << buffer.getBufferLength() << endl; + if(buffer.empty()) + { + return TC_NetWorkBuffer::PACKET_LESS; + } + + data = buffer.getBuffers(); + buffer.moveHeader(data.size()); + return TC_NetWorkBuffer::PACKET_FULL; +} + +TEST_F(UtilSerialPortTest, test2) +{ +#if TARGET_PLATFORM_WINDOWS + WSADATA wsadata; + WSAStartup(MAKEWORD(2, 1), &wsadata); +#endif + try + { + TC_SerialPortGroup serialPortGroup; + serialPortGroup.initialize(); + + TC_SerialPort::Options options; + + // options.portName = "/dev/tty.usbmodem00000000050C1"; + options.portName = "//./COM11"; + + options.baudRate = 115200; + options.stopBits = 0; + options.parity = 0; + + shared_ptr serialPort = serialPortGroup.create(options, onParser2, make_shared()); + string msg_send = {0x2f,0x44,0x1f,0x1f,0x1f,0x23}; + + // string msg_send = { 0x7e, 0x00, 0x08, 0x01, 0x00, 0x02, 0x01, (char)0xab, (char)0xcd }; + + // 7e000801000201abcd + while(true) + { + try + { + std::unique_lock lock(mtx); + serialPort->sendRequest(msg_send); + cnd.wait_for(lock, std::chrono::seconds(1)); + } + catch(const std::exception& ex) + { + cout << "ex: " << ex.what() << endl; + } + + TC_Common::sleep(1); + } + } + catch(const std::exception& e) + { + std::cerr << e.what() << '\n'; + } +} diff --git a/util/src/tc_serialport.cpp b/util/src/tc_serialport.cpp index 31893330..35a28b42 100644 --- a/util/src/tc_serialport.cpp +++ b/util/src/tc_serialport.cpp @@ -717,14 +717,13 @@ void TC_SerialPort::recvSucc(uint32_t len) if((*it)->length() > 0) { assert((*it)->length() == len); - _recvBuffer.addBuffer(*it); - } else { (*it)->addWriteIdx(len); } + _recvBuffer.addBuffer(*it); _buffRecv.erase(it); doProtocolAnalysis(&_recvBuffer);