此類提供了一個便攜式的串口接口。
1. 構造器和解析器
serial::Serial::Serial ( **const std::string &** port = "",
**uint32_t** baudrate = 9600,
**Timeout** timeout = Timeout(),
**bytesize_t** bytesize = eightbits,
**parity_t** parity = parity_none,
**stopbits_t** stopbits = stopbits_one,
**flowcontrol_t** flowcontrol = flowcontrol_none
)
創建了要給Serial對象並且如果端口指定的話打開端口,否則端口保持關閉,知道serial::Serial::open被調用。
參數:
port 一個std::string包含了串口地址;
baudrate 一個表示波特率的無符號32位整數;
timeout 一個serial::Timeout結構體,定義了串口的暫停條件;
bytesize 每個位組在串口數據傳輸的大小,默認是八位,可能的值是五位,六位,七位,八位;
parity 奇偶檢驗的方法,默認是無,可能的值是:無,奇校驗,偶校驗;
stopbits 使用的停止位的數量,默認是一位停止位,可能的值是:一位,一點五位,兩位;
flowcontrol使用的控制流的類型,默認是無控制流,可能的值是:無,軟件控制,硬件控制。
2. 成員函數
**size_t** Serial::available()
{
return pimpl_->available ();
}
返回緩存中字符的數量;
**void** Serial::close()
{
pimpl_->close ();
}
關閉串口;
**void** Serial::flush()
{
ScopedReadLock(this->pimpl_);
ScopedWriteLock(this->pimpl_);
pimpl_->flush ();
read_cache_.clear ();
}
清理輸入和輸出的緩存;
**void** Serial::flushInput ()
{
ScopedReadLock(this->pimpl_);
pimpl_->flushInput ();
}
僅清楚輸入的緩存;
**void** Serial::flushOutput()
{
ScopedWriteLock(this->pimpl_);
pimpl_->flushOutput ();
read_cache_.clear ();
}
僅清除輸出的緩存;
**uint32_t** Serial::getBaudrate()const
{
return uint32_t(pimpl_->getBaudrate ());
}
獲得端口波特率;
**bytesize_t** Serial::getBytesize()const
{
return pimpl_->getBytesize ();
}
獲得數據傳輸位的大小;
**string** Serial::getPort () const
{
return pimpl_->getPort ();
}
獲得端口ID;
**bool** Serial::isOpen ( ) const
{
return pimpl_->isOpen ();
}
獲得串口的打開狀態,端口打開返回true,否則返回false;
**void** Serial::open()
{
pimpl_->open ();
}
打開串口,只要設置好端口且此端口沒有打開;如果端口在結構體中設置,那麼這個調用就不需要了;
**size_t** Serial::read (uint8_t* buffer , size_t size)
{
ScopedReadLock (this->pimpl_);
return this->pimpl_->read (buffer, size);
}
從串口讀取指定位數的數據到指定的緩存中;
讀取函數在以下三種情況的一種滿足即返回:
- 要求位數的數據讀取完成,在這種情況下,需要的位數和讀取返回的size_t一致;
- timeout(暫停)發生,在這種情況下,讀取返回的值和需要的值不匹配,但是沒有exception拋出;
- exception發生,這種情況下,實際的exception拋出;
參數:
buffer 至少是需要大小的一個unit8_t數組;
size 一個size_t,定義了要讀取得數據位數;
返回:
一個size_t,代表讀取結果的位的數量;
**size_t** Serial::read (std::string& buffer,size_t size = 1)
{
ScopedReadLock (this->pimpl_);
uint8_t *buffer_ = new uint8_t[size];
size_t bytes_read = this->pimpl_->read (buffer_, size);
buffer.append (reinterpret_cast<const char*>(buffer_), bytes_read);
delete[] buffer_;
return bytes_read;
}
讀取一個給定大小的數據到一個給定的緩存中;
返回與上個函數一樣;
**string** Serial::read ( size_t size = 1 )
A std::string containing the data read from the port.
{
std::string buffer;
this->read (buffer, size);
return buffer;
}
**size_t** Serial::write(const uint8_t* data,size_t size)
{
ScopedWriteLock(this->pimpl_);
return this->write_(data, size);
}
寫一個字符串到串口;
參數:
data 一個常量包含了寫入串口的數據;
size 指出從給定數據緩存中寫入串口的位數;
返回實際寫入串口的數據的位數;
**size_t** Serial::write(const std::vector<uint8_t>& data)
{
ScopedWriteLock(this->pimpl_);
return this->write_ (&data[0], data.size());
}
**size_t** serial::Serial::write(const std::string & data)
Write a string to the serial port.
Parameters:
data A const reference containing the data to be written to the serial port.
Returns:
A size_t representing the number of bytes actually written to the serial port.