暂时改成读取不传入len
This commit is contained in:
parent
be1766e590
commit
0f4041d8d9
@ -62,7 +62,7 @@ public:
|
|||||||
return send_size;
|
return send_size;
|
||||||
}
|
}
|
||||||
|
|
||||||
int read(char* data, int size)
|
int read(char* data)
|
||||||
{
|
{
|
||||||
bytes_transferred_ = 0;
|
bytes_transferred_ = 0;
|
||||||
io_.reset();
|
io_.reset();
|
||||||
@ -77,16 +77,11 @@ public:
|
|||||||
boost::asio::placeholders::bytes_transferred));
|
boost::asio::placeholders::bytes_transferred));
|
||||||
|
|
||||||
io_.run();
|
io_.run();
|
||||||
|
|
||||||
if (size < bytes_transferred_) {
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (bytes_transferred_ > 0) {
|
if (bytes_transferred_ > 0) {
|
||||||
std::memcpy(data, buffer_, bytes_transferred_);
|
std::memcpy(data, buffer_, bytes_transferred_);
|
||||||
return static_cast<int>(bytes_transferred_);
|
data[bytes_transferred_] = '\0';
|
||||||
}
|
}
|
||||||
return 0;
|
return bytes_transferred_;
|
||||||
}
|
}
|
||||||
const char* get_last_error() const { return error_; }
|
const char* get_last_error() const { return error_; }
|
||||||
void set_timeout(uint64_t timeout) { time_out_ = timeout; }
|
void set_timeout(uint64_t timeout) { time_out_ = timeout; }
|
||||||
@ -103,12 +98,13 @@ private:
|
|||||||
if (code) {
|
if (code) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
bytes_transferred_ = -1;
|
||||||
port_->cancel();
|
port_->cancel();
|
||||||
}
|
}
|
||||||
void handle_read(const boost::system::error_code& error, std::size_t bytes_transferred)
|
void handle_read(const boost::system::error_code& error, std::size_t bytes_transferred)
|
||||||
{
|
{
|
||||||
if (!error) {
|
if (!error) {
|
||||||
bytes_transferred_ = bytes_transferred;
|
bytes_transferred_ = static_cast<int>(bytes_transferred);
|
||||||
timer_.cancel();
|
timer_.cancel();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -123,7 +119,7 @@ private:
|
|||||||
serial_port::flow_control flow_control_;
|
serial_port::flow_control flow_control_;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::size_t bytes_transferred_{};
|
int bytes_transferred_{};
|
||||||
boost::asio::io_context io_{};
|
boost::asio::io_context io_{};
|
||||||
boost::asio::serial_port* port_{};
|
boost::asio::serial_port* port_{};
|
||||||
boost::system::error_code ec_{};
|
boost::system::error_code ec_{};
|
||||||
@ -238,9 +234,9 @@ void CSerialOpr::close()
|
|||||||
{
|
{
|
||||||
imp_->close();
|
imp_->close();
|
||||||
}
|
}
|
||||||
int CSerialOpr::read(char* data, int size)
|
int CSerialOpr::read(char* data)
|
||||||
{
|
{
|
||||||
return imp_->read(data, size);
|
return imp_->read(data);
|
||||||
}
|
}
|
||||||
int CSerialOpr::write(const char* data, int len)
|
int CSerialOpr::write(const char* data, int len)
|
||||||
{
|
{
|
||||||
|
@ -54,7 +54,7 @@ public:
|
|||||||
public:
|
public:
|
||||||
int open();
|
int open();
|
||||||
void close();
|
void close();
|
||||||
int read(char* data, int size);
|
int read(char* data);
|
||||||
int write(const char* data, int len);
|
int write(const char* data, int len);
|
||||||
const char* get_last_error() const;
|
const char* get_last_error() const;
|
||||||
|
|
||||||
|
2
main.cpp
2
main.cpp
@ -23,7 +23,7 @@ int main()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
int read_size = opr.read(buffer, sizeof(buffer));
|
int read_size = opr.read(buffer);
|
||||||
if (read_size > 0) {
|
if (read_size > 0) {
|
||||||
std::cout << "要发送的数据大小:" << strlen(buffer2) << "\n";
|
std::cout << "要发送的数据大小:" << strlen(buffer2) << "\n";
|
||||||
int write_size = opr.write(buffer2, strlen(buffer2));
|
int write_size = opr.write(buffer2, strlen(buffer2));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user