修复分配的内存不够添加\0的问题
This commit is contained in:
parent
787a688af6
commit
9d718d9d41
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
.idea/*
|
||||
.vscode/*
|
||||
build/*
|
15
CMakeLists.txt
Normal file
15
CMakeLists.txt
Normal file
@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.26)
|
||||
|
||||
set(PROJECT_N demo)
|
||||
project(${PROJECT_N} VERSION 1.0)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 20)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
include_directories(${CMAKE_SOURCE_DIR}/include)
|
||||
|
||||
|
||||
add_executable(${PROJECT_N}
|
||||
${CMAKE_SOURCE_DIR}/third/serialib.cpp
|
||||
main.cc
|
||||
)
|
@ -98,7 +98,7 @@ public:
|
||||
std::this_thread::sleep_for(10ms);
|
||||
// char buffer[ser.available()] = {0};
|
||||
auto availableSize = ser.available();
|
||||
auto buffer = new char[availableSize];
|
||||
char* buffer = new char[availableSize+1];
|
||||
std::memset(buffer, 0, availableSize);
|
||||
auto size = ser.readBytes(buffer, availableSize, timeout);
|
||||
if(size > 0){
|
||||
@ -128,7 +128,7 @@ public:
|
||||
while(std::chrono::system_clock::now() < endTime){
|
||||
std::this_thread::sleep_for(10ms);
|
||||
auto availableSize = ser.available();
|
||||
auto buffer = new char[availableSize];
|
||||
auto buffer = new char[availableSize+1];
|
||||
auto size = ser.readBytes(buffer, availableSize, timeout);
|
||||
auto str = std::string(buffer);
|
||||
delete[] buffer;
|
||||
|
2
main.cc
2
main.cc
@ -17,7 +17,7 @@ int main(int argc, char** const argv){
|
||||
auto startTime = std::chrono::system_clock::now();
|
||||
// auto res = serial.GetAtUntil("AT+CFUN=1","OK", 200);
|
||||
// std::cout<<res.value_or("ERROR")<<std::endl;
|
||||
auto res = serial.GetAtResponse("AT+ECICCID", 200);
|
||||
auto res = serial.GetAtResponse("AT+CGSN=0", 200);
|
||||
std::cout<<res.value_or("ERROR")<<std::endl;
|
||||
auto endTime = std::chrono::system_clock::now();
|
||||
std::cout<<std::chrono::duration_cast<std::chrono::milliseconds>(endTime-startTime).count();
|
||||
|
Loading…
Reference in New Issue
Block a user