此类提供了一个便携式的串口接口。
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.