34#include "libserial/SerialStreamBuf.h"
35#include "libserial/SerialPort.h"
75 const BaudRate& baudRate,
76 const CharacterSize& characterSize,
77 const FlowControl& flowControlType,
78 const Parity& parityType,
79 const StopBits& stopBits,
110 void Open(
const std::string& fileName,
111 const std::ios_base::openmode& openMode,
196 void SetParity(
const Parity& parityType) ;
220 void SetVMin(
const short vmin) ;
245 void SetDTR(
const bool dtrState) ;
257 void SetRTS(
const bool rtsState) ;
295 std::vector<std::string> GetAvailableSerialPorts()
const ;
306 std::streamsize
xsputn(
const char_type* character,
307 std::streamsize numberOfBytes) ;
317 std::streamsize
xsgetn(char_type* character,
318 std::streamsize numberOfBytes) ;
325 std::streambuf::int_type
overflow(int_type character) ;
343 std::streambuf::int_type
uflow() ;
353 std::streambuf::int_type
pbackfail(int_type character) ;
393 const BaudRate& baudRate,
394 const CharacterSize& characterSize,
395 const FlowControl& flowControlType,
396 const Parity& parityType,
397 const StopBits& stopBits,
414 const std::ios_base::openmode& openMode,
417 mImpl->Open(fileName,
431 mImpl->DrainWriteBuffer() ;
437 mImpl->FlushInputBuffer() ;
443 mImpl->FlushOutputBuffer() ;
449 mImpl->FlushIOBuffers() ;
455 return mImpl->IsDataAvailable() ;
461 return mImpl->IsOpen() ;
467 mImpl->SetDefaultSerialPortParameters() ;
473 mImpl->SetBaudRate(baudRate) ;
479 return mImpl->GetBaudRate() ;
485 mImpl->SetCharacterSize(characterSize) ;
491 return mImpl->GetCharacterSize() ;
497 mImpl->SetFlowControl(flowControlType) ;
503 return mImpl->GetFlowControl() ;
509 mImpl->SetParity(parityType) ;
515 return mImpl->GetParity() ;
521 mImpl->SetStopBits(stopBits) ;
527 return mImpl->GetStopBits() ;
533 mImpl->SetVMin(vmin) ;
539 return mImpl->GetVMin() ;
545 mImpl->SetVTime(vtime) ;
551 return mImpl->GetVTime() ;
557 mImpl->SetDTR(dtrState) ;
563 return mImpl->GetDTR() ;
569 mImpl->SetRTS(rtsState) ;
575 return mImpl->GetRTS() ;
581 return mImpl->GetCTS() ;
587 return mImpl->GetDSR() ;
593 return mImpl->GetFileDescriptor() ;
599 return mImpl->GetNumberOfBytesAvailable() ;
603 std::vector<std::string>
604 SerialStreamBuf::GetAvailableSerialPorts()
const
606 return mImpl->GetAvailableSerialPorts() ;
613 return std::streambuf::setbuf(character, numberOfBytes) ;
619 return mImpl->xsputn(character, numberOfBytes) ;
625 return mImpl->xsgetn(character, numberOfBytes) ;
628 std::streambuf::int_type
631 return mImpl->overflow(character) ;
634 std::streambuf::int_type
637 return mImpl->underflow() ;
640 std::streambuf::int_type
643 return mImpl->uflow() ;
646 std::streambuf::int_type
649 return mImpl->pbackfail(character) ;
655 return mImpl->showmanyc() ;
663 const BaudRate& baudRate,
664 const CharacterSize& characterSize,
665 const FlowControl& flowControlType,
666 const Parity& parityType,
667 const StopBits& stopBits,
669 try : mSerialPort(fileName,
679 catch (
const std::exception& err)
708 const std::ios_base::openmode& openMode,
712 mSerialPort.Open(fileName,
717 mSerialPort.SetSerialPortBlockingStatus(
true) ;
727 catch (
const std::exception& err)
729 throw OpenFailed(err.what()) ;
736 mSerialPort.Close() ;
743 mSerialPort.DrainWriteBuffer() ;
750 mSerialPort.FlushInputBuffer() ;
757 mSerialPort.FlushOutputBuffer() ;
764 mSerialPort.FlushIOBuffers() ;
771 return mSerialPort.IsOpen() ;
778 return mSerialPort.IsDataAvailable() ;
785 mSerialPort.SetDefaultSerialPortParameters() ;
792 mSerialPort.SetBaudRate(baudRate) ;
799 return mSerialPort.GetBaudRate() ;
806 mSerialPort.SetCharacterSize(characterSize) ;
813 return mSerialPort.GetCharacterSize() ;
820 mSerialPort.SetFlowControl(flowControlType) ;
827 return mSerialPort.GetFlowControl() ;
834 mSerialPort.SetParity(parityType) ;
841 return mSerialPort.GetParity() ;
848 mSerialPort.SetStopBits(stopBits) ;
855 return mSerialPort.GetStopBits() ;
862 mSerialPort.SetVMin(vmin) ;
869 return mSerialPort.GetVMin() ;
876 mSerialPort.SetVTime(vtime) ;
883 return mSerialPort.GetVTime() ;
890 mSerialPort.SetDTR(dtrState) ;
897 return mSerialPort.GetDTR() ;
904 mSerialPort.SetRTS(rtsState) ;
911 return mSerialPort.GetRTS() ;
918 return mSerialPort.GetCTS() ;
925 return mSerialPort.GetDSR() ;
932 return mSerialPort.GetFileDescriptor() ;
939 return mSerialPort.GetNumberOfBytesAvailable() ;
944 std::vector<std::string>
945 SerialStreamBuf::Implementation::GetAvailableSerialPorts()
const
947 return mSerialPort.GetAvailableSerialPorts() ;
954 std::streamsize numberOfBytes)
959 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
963 if (numberOfBytes <= 0)
972 const auto fd = mSerialPort.GetFileDescriptor() ;
973 ssize_t result = call_with_retry(write, fd, character, numberOfBytes) ;
988 std::streamsize numberOfBytes)
993 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
997 if (numberOfBytes <= 0)
1002 if (character ==
nullptr) {
1007 ssize_t result = -1;
1011 if (mPutbackAvailable)
1015 character[0] = mPutbackChar;
1019 mPutbackAvailable =
false;
1024 if (numberOfBytes > 1)
1030 const auto fd = mSerialPort.GetFileDescriptor() ;
1031 result = call_with_retry(read, fd, &character[1], numberOfBytes-1) ;
1049 const auto fd = mSerialPort.GetFileDescriptor() ;
1050 result = call_with_retry(read, fd, character, numberOfBytes) ;
1067 std::streambuf::int_type
1073 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1077 if (traits_type::eq_int_type(character, traits_type::eof()))
1080 return traits_type::eof() ;
1087 const auto fd = mSerialPort.GetFileDescriptor() ;
1088 char out_char = traits_type::to_char_type(character) ;
1089 ssize_t result = call_with_retry(write, fd, &out_char, 1) ;
1094 return traits_type::eof() ;
1098 return traits_type::not_eof(character) ;
1102 std::streambuf::int_type
1108 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1112 char next_char = 0 ;
1113 ssize_t result = -1;
1118 if (mPutbackAvailable)
1120 next_char = mPutbackChar;
1130 const auto fd = mSerialPort.GetFileDescriptor() ;
1131 result = call_with_retry(read, fd, &next_char, 1) ;
1138 mPutbackChar = next_char;
1139 mPutbackAvailable = true ;
1141 else if (result <= 0)
1145 return traits_type::eof() ;
1155 return traits_type::to_int_type(next_char) ;
1159 std::streambuf::int_type
1165 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1170 mPutbackAvailable =
false;
1176 std::streambuf::int_type
1182 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1187 if (mPutbackAvailable)
1189 return traits_type::eof() ;
1191 if (traits_type::eq_int_type(character, traits_type::eof()))
1196 return traits_type::eof() ;
1201 mPutbackChar = traits_type::to_char_type(character) ;
1202 mPutbackAvailable = true ;
1203 return traits_type::not_eof(character) ;
1213 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1216 ssize_t number_of_bytes_available = 0 ;
1222 const auto fd = mSerialPort.GetFileDescriptor() ;
1223 const auto result = call_with_retry(ioctl,
1226 &number_of_bytes_available) ;
1228 if ((result >= 0) and (number_of_bytes_available > 0))
1230 mPutbackAvailable = true ;
1233 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.
void Open(const std::string &fileName, const std::ios_base::openmode &openMode, bool exclusive)
Opens the serial port associated with the specified file name and the specified mode.
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 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.
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 Open(const std::string &fileName, const std::ios_base::openmode &openMode=std::ios_base::in|std::ios_base::out, bool exclusive=true)
Opens the serial port associated with the specified file name and the specified mode.
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:...