34#include "libserial/SerialStreamBuf.h"
35#include "libserial/SerialPort.h"
74 const BaudRate& baudRate,
75 const CharacterSize& characterSize,
76 const FlowControl& flowControlType,
77 const Parity& parityType,
78 const StopBits& stopBits) ;
107 void Open(
const std::string& fileName,
108 const std::ios_base::openmode& openMode) ;
192 void SetParity(
const Parity& parityType) ;
216 void SetVMin(
const short vmin) ;
241 void SetDTR(
const bool dtrState) ;
253 void SetRTS(
const bool rtsState) ;
291 std::vector<std::string> GetAvailableSerialPorts()
const ;
302 std::streamsize
xsputn(
const char_type* character,
303 std::streamsize numberOfBytes) ;
313 std::streamsize
xsgetn(char_type* character,
314 std::streamsize numberOfBytes) ;
321 std::streambuf::int_type
overflow(int_type character) ;
339 std::streambuf::int_type
uflow() ;
349 std::streambuf::int_type
pbackfail(int_type character) ;
389 const BaudRate& baudRate,
390 const CharacterSize& characterSize,
391 const FlowControl& flowControlType,
392 const Parity& parityType,
393 const StopBits& stopBits)
408 const std::ios_base::openmode& openMode)
410 mImpl->Open(fileName,
423 mImpl->DrainWriteBuffer() ;
429 mImpl->FlushInputBuffer() ;
435 mImpl->FlushOutputBuffer() ;
441 mImpl->FlushIOBuffers() ;
447 return mImpl->IsDataAvailable() ;
453 return mImpl->IsOpen() ;
459 mImpl->SetDefaultSerialPortParameters() ;
465 mImpl->SetBaudRate(baudRate) ;
471 return mImpl->GetBaudRate() ;
477 mImpl->SetCharacterSize(characterSize) ;
483 return mImpl->GetCharacterSize() ;
489 mImpl->SetFlowControl(flowControlType) ;
495 return mImpl->GetFlowControl() ;
501 mImpl->SetParity(parityType) ;
507 return mImpl->GetParity() ;
513 mImpl->SetStopBits(stopBits) ;
519 return mImpl->GetStopBits() ;
525 mImpl->SetVMin(vmin) ;
531 return mImpl->GetVMin() ;
537 mImpl->SetVTime(vtime) ;
543 return mImpl->GetVTime() ;
549 mImpl->SetDTR(dtrState) ;
555 return mImpl->GetDTR() ;
561 mImpl->SetRTS(rtsState) ;
567 return mImpl->GetRTS() ;
573 return mImpl->GetCTS() ;
579 return mImpl->GetDSR() ;
585 return mImpl->GetFileDescriptor() ;
591 return mImpl->GetNumberOfBytesAvailable() ;
595 std::vector<std::string>
596 SerialStreamBuf::GetAvailableSerialPorts()
const
598 return mImpl->GetAvailableSerialPorts() ;
605 return std::streambuf::setbuf(character, numberOfBytes) ;
611 return mImpl->xsputn(character, numberOfBytes) ;
617 return mImpl->xsgetn(character, numberOfBytes) ;
620 std::streambuf::int_type
623 return mImpl->overflow(character) ;
626 std::streambuf::int_type
629 return mImpl->underflow() ;
632 std::streambuf::int_type
635 return mImpl->uflow() ;
638 std::streambuf::int_type
641 return mImpl->pbackfail(character) ;
647 return mImpl->showmanyc() ;
655 const BaudRate& baudRate,
656 const CharacterSize& characterSize,
657 const FlowControl& flowControlType,
658 const Parity& parityType,
659 const StopBits& stopBits)
660 try : mSerialPort(fileName,
669 catch (
const std::exception& err)
698 const std::ios_base::openmode& openMode)
701 mSerialPort.Open(fileName,
705 mSerialPort.SetSerialPortBlockingStatus(
true) ;
715 catch (
const std::exception& err)
717 throw OpenFailed(err.what()) ;
724 mSerialPort.Close() ;
731 mSerialPort.DrainWriteBuffer() ;
738 mSerialPort.FlushInputBuffer() ;
745 mSerialPort.FlushOutputBuffer() ;
752 mSerialPort.FlushIOBuffers() ;
759 return mSerialPort.IsOpen() ;
766 return mSerialPort.IsDataAvailable() ;
773 mSerialPort.SetDefaultSerialPortParameters() ;
780 mSerialPort.SetBaudRate(baudRate) ;
787 return mSerialPort.GetBaudRate() ;
794 mSerialPort.SetCharacterSize(characterSize) ;
801 return mSerialPort.GetCharacterSize() ;
808 mSerialPort.SetFlowControl(flowControlType) ;
815 return mSerialPort.GetFlowControl() ;
822 mSerialPort.SetParity(parityType) ;
829 return mSerialPort.GetParity() ;
836 mSerialPort.SetStopBits(stopBits) ;
843 return mSerialPort.GetStopBits() ;
850 mSerialPort.SetVMin(vmin) ;
857 return mSerialPort.GetVMin() ;
864 mSerialPort.SetVTime(vtime) ;
871 return mSerialPort.GetVTime() ;
878 mSerialPort.SetDTR(dtrState) ;
885 return mSerialPort.GetDTR() ;
892 mSerialPort.SetRTS(rtsState) ;
899 return mSerialPort.GetRTS() ;
906 return mSerialPort.GetCTS() ;
913 return mSerialPort.GetDSR() ;
920 return mSerialPort.GetFileDescriptor() ;
927 return mSerialPort.GetNumberOfBytesAvailable() ;
932 std::vector<std::string>
933 SerialStreamBuf::Implementation::GetAvailableSerialPorts()
const
935 return mSerialPort.GetAvailableSerialPorts() ;
942 std::streamsize numberOfBytes)
947 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
951 if (numberOfBytes <= 0)
960 const auto fd = mSerialPort.GetFileDescriptor() ;
961 ssize_t result = call_with_retry(write, fd, character, numberOfBytes) ;
976 std::streamsize numberOfBytes)
981 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
985 if (numberOfBytes <= 0)
990 if (character ==
nullptr) {
999 if (mPutbackAvailable)
1003 character[0] = mPutbackChar;
1007 mPutbackAvailable =
false;
1012 if (numberOfBytes > 1)
1018 const auto fd = mSerialPort.GetFileDescriptor() ;
1019 result = call_with_retry(read, fd, &character[1], numberOfBytes-1) ;
1037 const auto fd = mSerialPort.GetFileDescriptor() ;
1038 result = call_with_retry(read, fd, character, numberOfBytes) ;
1055 std::streambuf::int_type
1061 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1065 if (traits_type::eq_int_type(character, traits_type::eof()))
1068 return traits_type::eof() ;
1075 const auto fd = mSerialPort.GetFileDescriptor() ;
1076 char out_char = traits_type::to_char_type(character) ;
1077 ssize_t result = call_with_retry(write, fd, &out_char, 1) ;
1082 return traits_type::eof() ;
1086 return traits_type::not_eof(character) ;
1090 std::streambuf::int_type
1096 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1100 char next_char = 0 ;
1101 ssize_t result = -1;
1106 if (mPutbackAvailable)
1108 next_char = mPutbackChar;
1118 const auto fd = mSerialPort.GetFileDescriptor() ;
1119 result = call_with_retry(read, fd, &next_char, 1) ;
1126 mPutbackChar = next_char;
1127 mPutbackAvailable = true ;
1129 else if (result <= 0)
1133 return traits_type::eof() ;
1143 return traits_type::to_int_type(next_char) ;
1147 std::streambuf::int_type
1153 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1158 mPutbackAvailable =
false;
1164 std::streambuf::int_type
1170 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1175 if (mPutbackAvailable)
1177 return traits_type::eof() ;
1179 if (traits_type::eq_int_type(character, traits_type::eof()))
1184 return traits_type::eof() ;
1189 mPutbackChar = traits_type::to_char_type(character) ;
1190 mPutbackAvailable = true ;
1191 return traits_type::not_eof(character) ;
1201 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1204 ssize_t number_of_bytes_available = 0 ;
1210 const auto fd = mSerialPort.GetFileDescriptor() ;
1211 const auto result = call_with_retry(ioctl,
1214 &number_of_bytes_available) ;
1216 if ((result >= 0) and (number_of_bytes_available > 0))
1218 mPutbackAvailable = true ;
1221 return number_of_bytes_available;
Exception error thrown when the serial port is already open.
Exception error thrown when the serial port is not open.
Exception error thrown when the serial port could not be opened.
SerialPort allows an object oriented approach to serial port communication. A serial port object can ...
SerialStreamBuf::Implementation is the SerialStreamBuf implementation class.
void SetRTS(const bool rtsState)
Sets the serial port RTS line status.
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
void SetVMin(const short vmin)
Sets the minimum number of characters for non-canonical reads.
bool GetRTS() const
Gets the serial port RTS line status.
StopBits GetStopBits() const
Gets the number of stop bits currently being used by the serial.
~Implementation()
Default Destructor.
Implementation & operator=(const Implementation &otherImplementation)=delete
Copy assignment is disallowed.
std::streamsize xsgetn(char_type *character, std::streamsize numberOfBytes)
Reads up to n characters from the serial port and returns them through the character array located at...
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
bool IsDataAvailable()
Determines if data is available at the serial port.
std::streamsize xsputn(const char_type *character, std::streamsize numberOfBytes)
Writes up to n characters from the character sequence at char s to the serial port associated with th...
void FlushIOBuffers()
Flushes the serial port input and output buffers.
std::streambuf::int_type uflow()
Reads and returns the next character from the associated serial port if one otherwise returns traits:...
CharacterSize GetCharacterSize() const
Gets the character size being used for serial communication.
BaudRate GetBaudRate() const
Gets the current baud rate for the serial port.
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
Implementation()=default
Default Constructor.
void SetVTime(const short vtime)
Sets character buffer timeout for non-canonical reads in deciseconds.
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
void SetDTR(const bool dtrState)
Sets the serial port DTR line status.
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
bool GetCTS()
Gets the serial port CTS line status.
FlowControl GetFlowControl() const
Get the current flow control setting.
std::streambuf::int_type overflow(int_type character)
Writes the specified character to the associated serial port.
int GetFileDescriptor() const
Gets the serial port file descriptor.
std::streambuf::int_type pbackfail(int_type character)
This function is called when a putback of a character fails. This must be implemented for unbuffered ...
bool mPutbackAvailable
True if a putback value is available in mPutbackChar.
short GetVMin() const
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonic...
std::streambuf::int_type underflow()
Reads and returns the next character from the associated serial port if one otherwise returns traits:...
bool GetDTR() const
Gets the serial port DTR line status.
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
bool GetDSR()
Gets the serial port DSR line status.
Implementation(const Implementation &otherImplementation)=delete
Copy construction is disallowed.
Implementation & operator=(const Implementation &&otherImplementation)=delete
Move assignment is disallowed.
char mPutbackChar
We use unbuffered I/O for the serial port. However, we need to provide the putback of at least one ch...
void FlushOutputBuffer()
Flushes the serial port output buffer.
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
void Open(const std::string &fileName, const std::ios_base::openmode &openMode)
Opens the serial port associated with the specified file name and the specified mode.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.
Implementation(const Implementation &&otherImplementation)=delete
Move construction is disallowed.
bool IsOpen() const
Determines if the serial port is open for I/O.
short GetVTime() const
Gets the current timeout value for non-canonical reads in deciseconds.
std::streamsize showmanyc()
Checks whether input is available on the port.
void FlushInputBuffer()
Flushes the serial port input buffer.
void SetDefaultSerialPortParameters()
Sets all serial port paramters to their default values.
Parity GetParity() const
Gets the parity type for the serial port.
CharacterSize GetCharacterSize() const
Gets the character size being used for serial communication.
bool GetCTS()
Get the status of the CTS line.
bool IsOpen() const
Determines if the serial port is open for I/O.
virtual ~SerialStreamBuf()
Default Destructor for a SerialStreamBuf object. Closes the serial port associated with mFileDescript...
virtual std::streambuf * setbuf(char_type *character, std::streamsize numberOfBytes) override
Performs an operation that is defined separately for each class derived from streambuf....
int GetNumberOfBytesAvailable()
Gets the number of bytes available in the read buffer.
virtual std::streamsize showmanyc() override
Checks whether input is available on the port. If you call SerialStream::in_avail,...
virtual int_type overflow(const int_type character) override
Writes the specified character to the associated serial port.
bool IsDataAvailable()
Checks if data is available at the input of the serial port.
virtual int_type pbackfail(const int_type character) override
This function is called when a putback of a character fails. This must be implemented for unbuffered ...
SerialStreamBuf()
Default Constructor.
void FlushOutputBuffer()
Flushes the serial port output buffer.
virtual std::streamsize xsgetn(char_type *character, std::streamsize numberOfBytes) override
Reads up to n characters from the serial port and returns them through the character array located at...
void SetDefaultSerialPortParameters()
Sets all serial port paramters to their default values.
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.
StopBits GetStopBits() const
Gets the number of stop bits currently being used by the serial.
void SetDTR(const bool dtrState=true)
Sets the DTR line to the specified value.
short GetVMin() const
Gets the VMIN value for the device, which represents the minimum number of characters for non-canonic...
bool GetDSR()
Get the status of the DSR line.
virtual std::streamsize xsputn(const char_type *character, std::streamsize numberOfBytes) override
Writes up to n characters from the character sequence at char s to the serial port associated with th...
void SetVTime(const short vtime)
Sets character buffer timeout for non-canonical reads in deciseconds.
bool GetRTS() const
Get the status of the RTS line.
virtual int_type underflow() override
Reads and returns the next character from the associated serial port if one otherwise returns traits:...
short GetVTime() const
Gets the current timeout value for non-canonical reads in deciseconds.
bool GetDTR() const
Gets the status of the DTR line.
void SetCharacterSize(const CharacterSize &characterSize)
Sets the character size for the serial port.
void SetVMin(const short vmin)
Sets the minimum number of characters for non-canonical reads.
FlowControl GetFlowControl() const
Gets the current flow control setting.
void DrainWriteBuffer()
Waits until the write buffer is drained and then returns.
void Close()
Closes the serial port. All settings of the serial port will be lost and no more I/O can be performed...
BaudRate GetBaudRate() const
Gets the current baud rate for the serial port.
void SetParity(const Parity &parityType)
Sets the parity type for the serial port.
void SetBaudRate(const BaudRate &baudRate)
Sets the baud rate for the serial port to the specified value.
Parity GetParity() const
Gets the parity type for the serial port.
void FlushIOBuffers()
Flushes the serial port input and output buffers.
void SetFlowControl(const FlowControl &flowControlType)
Sets flow control for the serial port.
void FlushInputBuffer()
Flushes the serial port input buffer.
int GetFileDescriptor() const
Gets the serial port file descriptor.
void SetStopBits(const StopBits &stopBits)
Sets the number of stop bits to be used with the serial port.
void SetRTS(const bool rtsState=true)
Set the RTS line to the specified value.
virtual int_type uflow() override
Reads and returns the next character from the associated serial port if one otherwise returns traits:...