0
我有以下方法(位於一個類)選自:https://gitorious.org/serial-port/serial-port/source/03e161e0b788d593773b33006e01333946aa7e13:1_simple/SimpleSerial.h#L46升壓:: ASIO高性能串行讀(RS232)
boost::asio::serial_port serial;
// [...]
std::string readLine() {
char c;
std::string result;
while(true) {
asio::read(serial,asio::buffer(&c,1));
switch(c)
{
case '\r':
result+=c;
break;
case '\n':
result+=c;
return result;
default:
result+=c;
}
}
return result;
}
因爲它是寫有「代碼爲簡單起見進行了優化,而不是速度」 。所以我正在考慮優化此代碼。但是我沒有得到任何有用的結果。我的一般做法是:
void readUntil(const std::string& delim) {
using namespace boost;
asio::streambuf bf;
size_t recBytes = asio::read_until(serial, bf, boost::regex(delim));
[...]
'delim'將是「\ n」。但我不知道如何將asio :: streambuf轉換爲std :: string。此外,我不知道用這種方法是否會失去角色。例如。如果我一次收到以下一段文字:
xxxxx\r\nyyyyyyy
我只是讀'xxxxx \ r \ n',其餘的都丟失了嗎?