Loading...
Searching...
No Matches
SerialPort.h
/******************************************************************************
* @file SerialPort.h *
* @copyright (C) 2004-2018 LibSerial Development Team. All rights reserved. *
* crayzeewulf@gmail.com *
* *
* Redistribution and use in source and binary forms, with or without *
* modification, are permitted provided that the following conditions *
* are met: *
* *
* 1. Redistributions of source code must retain the above copyright *
* notice, this list of conditions and the following disclaimer. *
* 2. Redistributions in binary form must reproduce the above copyright *
* notice, this list of conditions and the following disclaimer in *
* the documentation and/or other materials provided with the *
* distribution. *
* 3. Neither the name PX4 nor the names of its contributors may be *
* used to endorse or promote products derived from this software *
* without specific prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
* POSSIBILITY OF SUCH DAMAGE. *
*****************************************************************************/
#pragma once
#include <libserial/SerialPortConstants.h>
#include <ios>
#include <memory>
namespace LibSerial
{
class SerialPort
{
public:
explicit SerialPort() ;
explicit SerialPort(const std::string& fileName,
const BaudRate& baudRate = BaudRate::BAUD_DEFAULT,
const CharacterSize& characterSize = CharacterSize::CHAR_SIZE_DEFAULT,
const FlowControl& flowControlType = FlowControl::FLOW_CONTROL_DEFAULT,
const Parity& parityType = Parity::PARITY_DEFAULT,
const StopBits& stopBits = StopBits::STOP_BITS_DEFAULT) ;
virtual ~SerialPort() noexcept;
SerialPort(const SerialPort& otherSerialPort) = delete ;
SerialPort(SerialPort&& otherSerialPort) ;
SerialPort& operator=(const SerialPort& otherSerialPort) = delete ;
SerialPort& operator=(SerialPort&& otherSerialPort) ;
void Open(const std::string& fileName,
const std::ios_base::openmode& openMode = std::ios_base::in | std::ios_base::out) ;
void Close() ;
void DrainWriteBuffer() ;
void FlushInputBuffer() ;
void FlushOutputBuffer() ;
void FlushIOBuffers() ;
bool IsDataAvailable() ;
bool IsOpen() const ;
void SetDefaultSerialPortParameters() ;
void SetBaudRate(const BaudRate& baudRate) ;
BaudRate GetBaudRate() const ;
void SetCharacterSize(const CharacterSize& characterSize) ;
CharacterSize GetCharacterSize() const ;
void SetFlowControl(const FlowControl& flowControlType) ;
FlowControl GetFlowControl() const ;
void SetParity(const Parity& parityType) ;
Parity GetParity() const ;
void SetStopBits(const StopBits& stopBits) ;
StopBits GetStopBits() const ;
void SetVMin(const short vmin) ;
short GetVMin() const ;
void SetVTime(const short vtime) ;
short GetVTime() const ;
void SetDTR(const bool dtrState = true) ;
bool GetDTR() const ;
void SetRTS(const bool rtsState = true) ;
bool GetRTS() const ;
bool GetCTS() ;
bool GetDSR() ;
int GetFileDescriptor() const ;
int GetNumberOfBytesAvailable() ;
#ifdef __linux__
std::vector<std::string> GetAvailableSerialPorts() const ;
#endif
void Read(DataBuffer& dataBuffer,
size_t numberOfBytes = 0,
size_t msTimeout = 0) ;
void Read(std::string& dataString,
size_t numberOfBytes = 0,
size_t msTimeout = 0) ;
void ReadByte(char& charBuffer,
size_t msTimeout = 0) ;
void ReadByte(unsigned char& charBuffer,
size_t msTimeout = 0) ;
void ReadLine(std::string& dataString,
char lineTerminator = '\n',
size_t msTimeout = 0) ;
void Write(const DataBuffer& dataBuffer) ;
void Write(const std::string& dataString) ;
void WriteByte(char charbuffer) ;
void WriteByte(unsigned char charbuffer) ;
void SetSerialPortBlockingStatus(bool blockingStatus) ;
bool GetSerialPortBlockingStatus() const ;
void SetModemControlLine(int modemLine,
bool lineState) ;
bool GetModemControlLine(int modemLine) ;
protected:
private:
class Implementation;
std::unique_ptr<Implementation> mImpl;
} ; // class SerialPort
template<typename Fn, typename... Args>
typename std::result_of<Fn(Args...)>::type
call_with_retry(Fn func, Args... args)
{
using result_type = typename std::result_of<Fn(Args...)>::type ;
result_type result ;
do {
result = func(std::forward<Args>(args)...);
} while((result == -1) and (errno == EINTR)) ;
return result ;
}
} // namespace LibSerial
LibSerial::SerialPort::Implementation
SerialPort::Implementation is the SerialPort implementation class.
Definition SerialPort.cpp:55
LibSerial::SerialPort
SerialPort allows an object oriented approach to serial port communication. A serial port object can ...
Definition SerialPort.h:56
LibSerial::SerialPort::SetBaudRate
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
Definition SerialPort.cpp:570
LibSerial::SerialPort::SetModemControlLine
void SetModemControlLine(int modemLine, bool lineState)
Set the specified modem control line to the specified value.
Definition SerialPort.cpp:792
LibSerial::SerialPort::SetParity
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
Definition SerialPort.cpp:606
LibSerial::SerialPort::Open
void Open(const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out)
Opens the serial port associated with the specified file name and the specified mode.
Definition SerialPort.cpp:514
LibSerial::SerialPort::GetCTS
bool GetCTS()
Get the status of the CTS line.
Definition SerialPort.cpp:678
LibSerial::SerialPort::GetSerialPortBlockingStatus
bool GetSerialPortBlockingStatus() const
Gets the current state of the serial port blocking status.
Definition SerialPort.cpp:786
LibSerial::SerialPort::WriteByte
void WriteByte(char charbuffer)
Writes a single byte to the serial port.
Definition SerialPort.cpp:768
LibSerial::SerialPort::FlushInputBuffer
void FlushInputBuffer()
Flushes the serial port input buffer.
Definition SerialPort.cpp:534
LibSerial::SerialPort::SetFlowControl
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
Definition SerialPort.cpp:594
LibSerial::SerialPort::SetDefaultSerialPortParameters
void SetDefaultSerialPortParameters()
Sets all serial port paramters to their default values.
Definition SerialPort.cpp:564
LibSerial::SerialPort::SerialPort
SerialPort()
Default Constructor.
Definition SerialPort.cpp:477
LibSerial::SerialPort::GetFlowControl
FlowControl GetFlowControl() const
Gets the current flow control setting.
Definition SerialPort.cpp:600
LibSerial::SerialPort::GetCharacterSize
CharacterSize GetCharacterSize() const
Gets the character size being used for serial communication.
Definition SerialPort.cpp:588
LibSerial::SerialPort::Write
void Write(const DataBuffer &dataBuffer)
Writes a DataBuffer to the serial port.
Definition SerialPort.cpp:756
LibSerial::SerialPort::GetDTR
bool GetDTR() const
Gets the status of the DTR line.
Definition SerialPort.cpp:660
LibSerial::SerialPort::SetRTS
void SetRTS(const bool rtsState=true)
Set the RTS line to the specified value.
Definition SerialPort.cpp:666
LibSerial::SerialPort::SetVMin
void SetVMin(const short vmin)
Sets the minimum number of characters for non-canonical reads.
Definition SerialPort.cpp:630
LibSerial::SerialPort::~SerialPort
virtual ~SerialPort() noexcept
Default Destructor for a SerialPort object. Closes the serial port associated with mFileDescriptor if...
LibSerial::SerialPort::GetStopBits
StopBits GetStopBits() const
Gets the number of stop bits currently being used by the serial.
Definition SerialPort.cpp:624
LibSerial::SerialPort::GetRTS
bool GetRTS() const
Get the status of the RTS line.
Definition SerialPort.cpp:672
LibSerial::SerialPort::Close
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
Definition SerialPort.cpp:522
LibSerial::SerialPort::SetCharacterSize
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
Definition SerialPort.cpp:582
LibSerial::SerialPort::IsDataAvailable
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
Definition SerialPort.cpp:552
LibSerial::SerialPort::GetBaudRate
BaudRate GetBaudRate() const
Gets the current baud rate for the serial port.
Definition SerialPort.cpp:576
LibSerial::SerialPort::SetVTime
void SetVTime(const short vtime)
Sets character buffer timeout for non-canonical reads in deciseconds.
Definition SerialPort.cpp:642
LibSerial::SerialPort::FlushIOBuffers
void FlushIOBuffers()
Flushes the serial port input and output buffers.
Definition SerialPort.cpp:546
LibSerial::SerialPort::FlushOutputBuffer
void FlushOutputBuffer()
Flushes the serial port output buffer.
Definition SerialPort.cpp:540
LibSerial::SerialPort::GetParity
Parity GetParity() const
Gets the parity type for the serial port.
Definition SerialPort.cpp:612
LibSerial::SerialPort::GetVMin
short GetVMin() const
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonic...
Definition SerialPort.cpp:636
LibSerial::SerialPort::GetModemControlLine
bool GetModemControlLine(int modemLine)
Get the current state of the specified modem control line.
Definition SerialPort.cpp:799
LibSerial::SerialPort::GetVTime
short GetVTime() const
Gets the current timeout value for non-canonical reads in deciseconds.
Definition SerialPort.cpp:648
LibSerial::SerialPort::Read
void Read(DataBuffer &dataBuffer, size_t numberOfBytes=0, size_t msTimeout=0)
Reads the specified number of bytes from the serial port. The method will timeout if no data is recei...
Definition SerialPort.cpp:710
LibSerial::SerialPort::GetDSR
bool GetDSR()
Get the status of the DSR line.
Definition SerialPort.cpp:684
LibSerial::SerialPort::ReadByte
void ReadByte(char &charBuffer, size_t msTimeout=0)
Reads a single byte from the serial port. If no data is available within the specified number of mill...
Definition SerialPort.cpp:730
LibSerial::SerialPort::GetNumberOfBytesAvailable
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
Definition SerialPort.cpp:696
LibSerial::SerialPort::GetFileDescriptor
int GetFileDescriptor() const
Gets the serial port file descriptor.
Definition SerialPort.cpp:690
LibSerial::SerialPort::SetDTR
void SetDTR(const bool dtrState=true)
Sets the DTR line to the specified value.
Definition SerialPort.cpp:654
LibSerial::SerialPort::ReadLine
void ReadLine(std::string &dataString, char lineTerminator='\n', size_t msTimeout=0)
Reads a line of characters from the serial port. The method will timeout if no data is received in th...
Definition SerialPort.cpp:746
LibSerial::SerialPort::DrainWriteBuffer
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
Definition SerialPort.cpp:528
LibSerial::SerialPort::IsOpen
bool IsOpen() const
Determines if the serial port is open for I/O.
Definition SerialPort.cpp:558
LibSerial::SerialPort::SetSerialPortBlockingStatus
void SetSerialPortBlockingStatus(bool blockingStatus)
Sets the current state of the serial port blocking status.
Definition SerialPort.cpp:780
LibSerial::SerialPort::SetStopBits
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.
Definition SerialPort.cpp:618