项目作者: Tai-Min

项目描述 :
Boost's serial io_service wrapped so it resembles Arduino's Serial
高级语言: C++
项目地址: git://github.com/Tai-Min/Boost-Serial-Port.git
创建时间: 2018-07-23T18:55:01Z
项目社区:https://github.com/Tai-Min/Boost-Serial-Port

开源协议:MIT License

下载


Boost-Serial-Port

This header file provides asynchronous, non-blocking* access to utilize device’s hardware serial port in Arduino-like manner

Every write function returns instantly.

* Write will block and wait if write operation is already being performed.

Build

Linux

  • Requires boost_system and pthread linked.

Usage

Typedefs and enums

  1. typedef boost::asio::serial_port_base::flow_control::type flowControlType;

possible values:

  • hardware / software / none


    1. typedef boost::asio::serial_port_base::parity::type parityType;

    possible values:

  • odd / even / none


    1. typedef boost::asio::serial_port_base::stop_bits::type stopBitsType;

    possible values:

  • one / onepointfive / two

  1. typedef boost::system::errc::errc_t errorCode;

possible values:


  1. enum format;

possible values:

  • BIN / OCT / DEC / HEX


Open serial port

  1. void open(std::string name,
  2. unsigned int baudRate = 115200,
  3. flowControlType flowControl = flowControlType::none,
  4. unsigned int characterSize = 8,
  5. parityType parity = parityType::none,
  6. stopBitsType stopBits = stopBitsType::one);
  • name: Name of the serial port (i.e COM1)
  • Everything else: should be self explanatory

Check whether serial port is open

  1. bool isOpen() const;
  • returns: True if serial port is open, false otherwise

Close serial port

  1. void close();

Check whether error happened during async operation

  1. bool good() const;
  • returns: false if something happened, true on successful operation

Clear port’s error flag

  1. void clear();

Get error

  1. int getErr() const;

Write raw bytes

  1. unsigned int write(uint8_t data);
  • data: A byte to write

  • returns: Number of bytes written


  1. unsigned int write(std::vector<uint8_t> const & data);
  • data: Vector of bytes to write

  • returns: Number of bytes written


Print ascii formatted data

  1. template<typename T>
  2. unsigned int print(T const & data, unsigned int option = BoostSerial::DEC);
  • data: Some data to print.
  • option: For integral variables its used to specify the format of given data (see BoostSerial::format)
  • option: For floating point variables its used to specify the decimal precision.
  • option: For anything else this argument is useless.

  • returns: Number of characters written


Print ascii formatted data with newline

  1. template<typename T>
  2. unsigned int println(T const & data, unsigned int option = BoostSerial::DEC);
  • data: Some data to print.
  • option: For integral variables its used to specify the format of given data (see BoostSerial::format)
  • option: For floating point variables its used to specify the decimal precision.
  • option: For anything else this argument is useless.

  • returns: Number of characters written


Read raw byte or sequence of raw bytes from buffer

  1. int16_t read();
  • returns: First byte available in the buffer (removes it from the buffer) or -1 if the buffer is empty.


  1. std::vector<uint8_t> readBuffer();
  • returns: Current content of read buffer (clears the buffer)

  1. std::vector<uint8_t> readBytes(uint16_t len = 0xFFFF);
  • len: Number of bytes to read

  • returns: vector of bytes of size len or smaller if given number of bytes hadn’t been read before timeout (removes read characters from the buffer)


  1. std::vector<uint8_t> readBytesUntil(uint8_t terminator, uint16_t len = 0xFFFF);
  • terminator: Byte that will end the reading (this byte is not included in the return but is removed from the buffer)
  • len: Number of bytes to read

  • returns: Vector of bytes to given terminator or vector of size len if given terminator hadn’t been found in len bytes or smaller vector if timeout happened and any of the previous conditions hadn’t been met (read characters are removed from the buffer)


Read strings

  1. std::string readString();
  • returns: Everything from serial’s read buffer as string. Stops reading on ‘\0’ or on timeout


  1. std::string readStringUntil(char terminator = '\0');
  • terminator: Character that will end the reading (this character is not included in the return but is removed from the buffer)

  • returns: String to given terminator or smaller string if timeout happened and terminator hadn’t been found (read characters are removed from the buffer).


Check next character in read buffer

  1. int16_t peek() const;
  • returns: First byte in the buffer (doesn’t remove it) or -1 if the buffer is empty

Check number of bytes available in the read buffer

  1. unsigned int available() const;
  • returns: Number of bytes waiting in the read buffer

Check if serial port is in idle state

  1. bool idle() const;
  • returns: True if there is no write operation on serial port

Clear read buffer

  1. void flush();

Set parameters of the serial port

  1. void setBaud(unsigned int baud = 115200);
  2. void setFlowControl(flowControlType flowControl = flowControlType::none);
  3. void setCharacterSize(unsigned int charSize = 8);
  4. void setPraity(parityType parity = parityType::none);
  5. void setStopBits(stopBitsType stopBits = stopBitsType::one);
  6. void setBufferSize(unsigned int bufSize = 256);
  7. void setTimeout(unsigned int timeinms = 1000);

setBufferSize affects the size of the internal read buffer. If overflow happens, the data that appeared first will be lost


Get parameters of the serial port

  1. unsigned int getBaud() const;
  2. flowControlType getFlowControl() const;
  3. unsigned int getCharacterSize() const;
  4. parityType getParity() const;
  5. stopBitsType getStopBits() const;
  6. unsigned int getBufferSize() const;
  7. unsigned int getTimeout() const;