LibSerial 1.0.0
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
Loading...
Searching...
No Matches
SerialStreamBuf.cpp
1/******************************************************************************
2 * @file SerialStreamBuf.cpp *
3 * @copyright (C) 2004-2018 LibSerial Development Team. All rights reserved. *
4 * crayzeewulf@gmail.com *
5 * *
6 * Redistribution and use in source and binary forms, with or without *
7 * modification, are permitted provided that the following conditions *
8 * are met: *
9 * *
10 * 1. Redistributions of source code must retain the above copyright *
11 * notice, this list of conditions and the following disclaimer. *
12 * 2. Redistributions in binary form must reproduce the above copyright *
13 * notice, this list of conditions and the following disclaimer in *
14 * the documentation and/or other materials provided with the *
15 * distribution. *
16 * 3. Neither the name PX4 nor the names of its contributors may be *
17 * used to endorse or promote products derived from this software *
18 * without specific prior written permission. *
19 * *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS *
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE *
24 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, *
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, *
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS *
27 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED *
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN *
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE *
31 * POSSIBILITY OF SUCH DAMAGE. *
32 *****************************************************************************/
33
34#include "libserial/SerialStreamBuf.h"
35#include "libserial/SerialPort.h"
36
37#include <cassert>
38#include <cstring>
39#include <fcntl.h>
40#include <sys/ioctl.h>
41#include <unistd.h>
42
43namespace LibSerial
44{
49 {
50 public:
54 Implementation() = default ;
55
60
73 Implementation(const std::string& fileName,
74 const BaudRate& baudRate,
75 const CharacterSize& characterSize,
76 const FlowControl& flowControlType,
77 const Parity& parityType,
78 const StopBits& stopBits) ;
79
83 Implementation(const Implementation& otherImplementation) = delete ;
84
88 Implementation(const Implementation&& otherImplementation) = delete ;
89
93 Implementation& operator=(const Implementation& otherImplementation) = delete ;
94
98 Implementation& operator=(const Implementation&& otherImplementation) = delete ;
99
107 void Open(const std::string& fileName,
108 const std::ios_base::openmode& openMode) ;
109
114 void Close() ;
115
119 void DrainWriteBuffer() ;
120
124 void FlushInputBuffer() ;
125
129 void FlushOutputBuffer() ;
130
134 void FlushIOBuffers() ;
135
139 bool IsDataAvailable() ;
140
145 bool IsOpen() const ;
146
151
156 void SetBaudRate(const BaudRate& baudRate) ;
157
162 BaudRate GetBaudRate() const ;
163
168 void SetCharacterSize(const CharacterSize& characterSize) ;
169
174 CharacterSize GetCharacterSize() const ;
175
180 void SetFlowControl(const FlowControl& flowControlType) ;
181
186 FlowControl GetFlowControl() const ;
187
192 void SetParity(const Parity& parityType) ;
193
198 Parity GetParity() const ;
199
204 void SetStopBits(const StopBits& stopBits) ;
205
210 StopBits GetStopBits() const ;
211
216 void SetVMin(const short vmin) ;
217
223 short GetVMin() const ;
224
229 void SetVTime(const short vtime) ;
230
235 short GetVTime() const ;
236
241 void SetDTR(const bool dtrState) ;
242
247 bool GetDTR() const ;
248
253 void SetRTS(const bool rtsState) ;
254
259 bool GetRTS() const ;
260
265 bool GetCTS() ;
266
271 bool GetDSR() ;
272
277 int GetFileDescriptor() const ;
278
284
285#ifdef __linux__
291 std::vector<std::string> GetAvailableSerialPorts() const ;
292#endif
293
302 std::streamsize xsputn(const char_type* character,
303 std::streamsize numberOfBytes) ;
304
313 std::streamsize xsgetn(char_type* character,
314 std::streamsize numberOfBytes) ;
315
321 std::streambuf::int_type overflow(int_type character) ;
322
330 std::streambuf::int_type underflow() ;
331
339 std::streambuf::int_type uflow() ;
340
349 std::streambuf::int_type pbackfail(int_type character) ;
350
356 std::streamsize showmanyc() ;
357
363 bool mPutbackAvailable {false} ;
364
372 char mPutbackChar {0} ;
373
374 private:
375
379 SerialPort mSerialPort {} ;
380 } ;
381
383 : mImpl(new Implementation)
384 {
385 setbuf(nullptr, 0) ;
386 }
387
388 SerialStreamBuf::SerialStreamBuf(const std::string& fileName,
389 const BaudRate& baudRate,
390 const CharacterSize& characterSize,
391 const FlowControl& flowControlType,
392 const Parity& parityType,
393 const StopBits& stopBits)
394 : mImpl(new Implementation(fileName,
395 baudRate,
396 characterSize,
397 flowControlType,
398 parityType,
399 stopBits))
400 {
401 setbuf(nullptr, 0) ;
402 }
403
405
406 void
407 SerialStreamBuf::Open(const std::string& fileName,
408 const std::ios_base::openmode& openMode)
409 {
410 mImpl->Open(fileName,
411 openMode) ;
412 }
413
414 void
416 {
417 mImpl->Close() ;
418 }
419
420 void
422 {
423 mImpl->DrainWriteBuffer() ;
424 }
425
426 void
428 {
429 mImpl->FlushInputBuffer() ;
430 }
431
432 void
434 {
435 mImpl->FlushOutputBuffer() ;
436 }
437
438 void
440 {
441 mImpl->FlushIOBuffers() ;
442 }
443
444 bool
446 {
447 return mImpl->IsDataAvailable() ;
448 }
449
450 bool
452 {
453 return mImpl->IsOpen() ;
454 }
455
456 void
458 {
459 mImpl->SetDefaultSerialPortParameters() ;
460 }
461
462 void
463 SerialStreamBuf::SetBaudRate(const BaudRate& baudRate)
464 {
465 mImpl->SetBaudRate(baudRate) ;
466 }
467
468 BaudRate
470 {
471 return mImpl->GetBaudRate() ;
472 }
473
474 void
475 SerialStreamBuf::SetCharacterSize(const CharacterSize& characterSize)
476 {
477 mImpl->SetCharacterSize(characterSize) ;
478 }
479
480 CharacterSize
482 {
483 return mImpl->GetCharacterSize() ;
484 }
485
486 void
487 SerialStreamBuf::SetFlowControl(const FlowControl& flowControlType)
488 {
489 mImpl->SetFlowControl(flowControlType) ;
490 }
491
492 FlowControl
494 {
495 return mImpl->GetFlowControl() ;
496 }
497
498 void
499 SerialStreamBuf::SetParity(const Parity& parityType)
500 {
501 mImpl->SetParity(parityType) ;
502 }
503
504 Parity
506 {
507 return mImpl->GetParity() ;
508 }
509
510 void
511 SerialStreamBuf::SetStopBits(const StopBits& stopBits)
512 {
513 mImpl->SetStopBits(stopBits) ;
514 }
515
516 StopBits
518 {
519 return mImpl->GetStopBits() ;
520 }
521
522 void
523 SerialStreamBuf::SetVMin(const short vmin)
524 {
525 mImpl->SetVMin(vmin) ;
526 }
527
528 short
530 {
531 return mImpl->GetVMin() ;
532 }
533
534 void
535 SerialStreamBuf::SetVTime(const short vtime)
536 {
537 mImpl->SetVTime(vtime) ;
538 }
539
540 short
542 {
543 return mImpl->GetVTime() ;
544 }
545
546 void
547 SerialStreamBuf::SetDTR(const bool dtrState)
548 {
549 mImpl->SetDTR(dtrState) ;
550 }
551
552 bool
554 {
555 return mImpl->GetDTR() ;
556 }
557
558 void
559 SerialStreamBuf::SetRTS(const bool rtsState)
560 {
561 mImpl->SetRTS(rtsState) ;
562 }
563
564 bool
566 {
567 return mImpl->GetRTS() ;
568 }
569
570 bool
572 {
573 return mImpl->GetCTS() ;
574 }
575
576 bool
578 {
579 return mImpl->GetDSR() ;
580 }
581
582 int
584 {
585 return mImpl->GetFileDescriptor() ;
586 }
587
588 int
590 {
591 return mImpl->GetNumberOfBytesAvailable() ;
592 }
593
594#ifdef __linux__
595 std::vector<std::string>
596 SerialStreamBuf::GetAvailableSerialPorts() const
597 {
598 return mImpl->GetAvailableSerialPorts() ;
599 }
600#endif
601
602 std::streambuf*
603 SerialStreamBuf::setbuf(char_type* character, std::streamsize numberOfBytes)
604 {
605 return std::streambuf::setbuf(character, numberOfBytes) ;
606 }
607
608 std::streamsize
609 SerialStreamBuf::xsputn(const char_type* character, std::streamsize numberOfBytes)
610 {
611 return mImpl->xsputn(character, numberOfBytes) ;
612 }
613
614 std::streamsize
615 SerialStreamBuf::xsgetn(char_type* character, std::streamsize numberOfBytes)
616 {
617 return mImpl->xsgetn(character, numberOfBytes) ;
618 }
619
620 std::streambuf::int_type
621 SerialStreamBuf::overflow(const int_type character)
622 {
623 return mImpl->overflow(character) ;
624 }
625
626 std::streambuf::int_type
628 {
629 return mImpl->underflow() ;
630 }
631
632 std::streambuf::int_type
634 {
635 return mImpl->uflow() ;
636 }
637
638 std::streambuf::int_type
639 SerialStreamBuf::pbackfail(const int_type character)
640 {
641 return mImpl->pbackfail(character) ;
642 }
643
644 std::streamsize
646 {
647 return mImpl->showmanyc() ;
648 }
649
650
653 inline
655 const BaudRate& baudRate,
656 const CharacterSize& characterSize,
657 const FlowControl& flowControlType,
658 const Parity& parityType,
659 const StopBits& stopBits)
660 try : mSerialPort(fileName,
661 baudRate,
662 characterSize,
663 flowControlType,
664 parityType,
665 stopBits)
666 {
667 // empty
668 }
669 catch (const std::exception& err)
670 {
671 throw OpenFailed(err.what()) ;
672 }
673
674 inline
676 try
677 {
678 // Close the serial port if it is open.
679 if (IsOpen())
680 {
681 Close() ;
682 }
683 }
684 catch(...)
685 {
686 //
687 // :IMPORTANT: We do not let any exceptions escape the destructor.
688 // (see https://isocpp.org/wiki/faq/exceptions#dtors-shouldnt-throw)
689 //
690 // :TODO: Once we add logging to LibSerial, we should issue a warning
691 // if we reach here.
692 //
693 }
694
695 inline
696 void
697 SerialStreamBuf::Implementation::Open(const std::string& fileName,
698 const std::ios_base::openmode& openMode)
699 try
700 {
701 mSerialPort.Open(fileName,
702 openMode) ;
703
704 // @note - Stream communications need to happen in blocking mode.
705 mSerialPort.SetSerialPortBlockingStatus(true) ;
706 }
707 catch (const AlreadyOpen&)
708 {
709 throw ;
710 }
711 catch (const OpenFailed&)
712 {
713 throw ;
714 }
715 catch (const std::exception& err)
716 {
717 throw OpenFailed(err.what()) ;
718 }
719
720 inline
721 void
723 {
724 mSerialPort.Close() ;
725 }
726
727 inline
728 void
730 {
731 mSerialPort.DrainWriteBuffer() ;
732 }
733
734 inline
735 void
737 {
738 mSerialPort.FlushInputBuffer() ;
739 }
740
741 inline
742 void
744 {
745 mSerialPort.FlushOutputBuffer() ;
746 }
747
748 inline
749 void
751 {
752 mSerialPort.FlushIOBuffers() ;
753 }
754
755 inline
756 bool
758 {
759 return mSerialPort.IsOpen() ;
760 }
761
762 inline
763 bool
765 {
766 return mSerialPort.IsDataAvailable() ;
767 }
768
769 inline
770 void
772 {
773 mSerialPort.SetDefaultSerialPortParameters() ;
774 }
775
776 inline
777 void
779 {
780 mSerialPort.SetBaudRate(baudRate) ;
781 }
782
783 inline
784 BaudRate
786 {
787 return mSerialPort.GetBaudRate() ;
788 }
789
790 inline
791 void
792 SerialStreamBuf::Implementation::SetCharacterSize(const CharacterSize& characterSize)
793 {
794 mSerialPort.SetCharacterSize(characterSize) ;
795 }
796
797 inline
798 CharacterSize
800 {
801 return mSerialPort.GetCharacterSize() ;
802 }
803
804 inline
805 void
806 SerialStreamBuf::Implementation::SetFlowControl(const FlowControl& flowControlType)
807 {
808 mSerialPort.SetFlowControl(flowControlType) ;
809 }
810
811 inline
812 FlowControl
814 {
815 return mSerialPort.GetFlowControl() ;
816 }
817
818 inline
819 void
821 {
822 mSerialPort.SetParity(parityType) ;
823 }
824
825 inline
826 Parity
828 {
829 return mSerialPort.GetParity() ;
830 }
831
832 inline
833 void
835 {
836 mSerialPort.SetStopBits(stopBits) ;
837 }
838
839 inline
840 StopBits
842 {
843 return mSerialPort.GetStopBits() ;
844 }
845
846 inline
847 void
849 {
850 mSerialPort.SetVMin(vmin) ;
851 }
852
853 inline
854 short
856 {
857 return mSerialPort.GetVMin() ;
858 }
859
860 inline
861 void
863 {
864 mSerialPort.SetVTime(vtime) ;
865 }
866
867 inline
868 short
870 {
871 return mSerialPort.GetVTime() ;
872 }
873
874 inline
875 void
877 {
878 mSerialPort.SetDTR(dtrState) ;
879 }
880
881 inline
882 bool
884 {
885 return mSerialPort.GetDTR() ;
886 }
887
888 inline
889 void
891 {
892 mSerialPort.SetRTS(rtsState) ;
893 }
894
895 inline
896 bool
898 {
899 return mSerialPort.GetRTS() ;
900 }
901
902 inline
903 bool
905 {
906 return mSerialPort.GetCTS() ;
907 }
908
909 inline
910 bool
912 {
913 return mSerialPort.GetDSR() ;
914 }
915
916 inline
917 int
919 {
920 return mSerialPort.GetFileDescriptor() ;
921 }
922
923 inline
924 int
926 {
927 return mSerialPort.GetNumberOfBytesAvailable() ;
928 }
929
930#ifdef __linux__
931 inline
932 std::vector<std::string>
933 SerialStreamBuf::Implementation::GetAvailableSerialPorts() const
934 {
935 return mSerialPort.GetAvailableSerialPorts() ;
936 }
937#endif
938
939 inline
940 std::streamsize
941 SerialStreamBuf::Implementation::xsputn(const char_type* character,
942 std::streamsize numberOfBytes)
943 {
944 // Throw an exception if the serial port is not open.
945 if (!this->IsOpen())
946 {
947 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
948 }
949
950 // If n is less than 1 there is nothing to accomplish.
951 if (numberOfBytes <= 0)
952 {
953 return 0 ;
954 }
955
956 // Write the n characters to the serial port.
957 //
958 // :TODO: Consider using mSerialPort.Write() here instead of writing
959 // to the file descriptor directly.
960 const auto fd = mSerialPort.GetFileDescriptor() ;
961 ssize_t result = call_with_retry(write, fd, character, numberOfBytes) ;
962
963 // If the write failed then return 0.
964 if (result <= 0)
965 {
966 return 0 ;
967 }
968
969 // Otherwise, return the number of bytes actually written.
970 return result;
971 }
972
973 inline
974 std::streamsize
976 std::streamsize numberOfBytes)
977 {
978 // Throw an exception if the serial port is not open.
979 if (!this->IsOpen())
980 {
981 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
982 }
983
984 // If n is less than 1 there is nothing to accomplish.
985 if (numberOfBytes <= 0)
986 {
987 return 0 ;
988 }
989
990 if (character == nullptr) {
991 return 0 ;
992 }
993
994 // Try to read up to n characters in the array s.
995 ssize_t result = -1;
996
997 // If a putback character is available, then we need to read only
998 // n-1 character.
999 if (mPutbackAvailable)
1000 {
1001 // Put the mPutbackChar at the beginning of the array 's'.
1002 // Increment result to indicate that a character has been placed in s.
1003 character[0] = mPutbackChar;
1004 result++;
1005
1006 // The putback character is no longer available.
1007 mPutbackAvailable = false;
1008
1009 // If we need to read more than one character, then call read()
1010 // and try to read numberOfBytes-1 more characters and put them
1011 // at location starting from &character[1].
1012 if (numberOfBytes > 1)
1013 {
1014 //
1015 // :TODO: Consider using mSerialPort.Read() here instead of
1016 // using the file descriptor.
1017 //
1018 const auto fd = mSerialPort.GetFileDescriptor() ;
1019 result = call_with_retry(read, fd, &character[1], numberOfBytes-1) ;
1020
1021 // If read was successful, then we need to increment result by
1022 // one to indicate that the putback character was prepended to
1023 // the array, s. If read failed then leave result at -1.
1024 if (result != -1)
1025 {
1026 result++;
1027 }
1028 }
1029 }
1030 else
1031 {
1032 // If no putback character is available then we try to
1033 // read numberOfBytes characters.
1034 //
1035 // :TODO: Consider using mSerialPort.Read() here instead of
1036 // using the file descriptor.
1037 const auto fd = mSerialPort.GetFileDescriptor() ;
1038 result = call_with_retry(read, fd, character, numberOfBytes) ;
1039 }
1040
1041 // If result == -1 then the read call had an error, otherwise, if
1042 // result == 0 then we could not read the characters. In either
1043 // case, we return 0 to indicate that no characters could be read
1044 // from the serial port.
1045 if (result <= 0)
1046 {
1047 return 0 ;
1048 }
1049
1050 // Return the number of characters actually read from the serial port.
1051 return result;
1052 }
1053
1054 inline
1055 std::streambuf::int_type
1057 {
1058 // Throw an exception if the serial port is not open.
1059 if (!this->IsOpen())
1060 {
1061 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1062 }
1063
1064 // Try to write the specified character to the serial port.
1065 if (traits_type::eq_int_type(character, traits_type::eof()))
1066 {
1067 // If character is the eof character then we do nothing.
1068 return traits_type::eof() ;
1069 }
1070
1071 // Otherwise we write the character to the serial port.
1072 //
1073 // :TODO: Consider using a method of SerialPort class here instead
1074 // of using the file descriptor.
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) ;
1078
1079 // If the write failed then return eof.
1080 if (result <= 0)
1081 {
1082 return traits_type::eof() ;
1083 }
1084
1085 // Otherwise, return something other than eof().
1086 return traits_type::not_eof(character) ;
1087 }
1088
1089 inline
1090 std::streambuf::int_type
1092 {
1093 // Throw an exception if the serial port is not open.
1094 if (!this->IsOpen())
1095 {
1096 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1097 }
1098
1099 // Read the next character from the serial port.
1100 char next_char = 0 ;
1101 ssize_t result = -1;
1102
1103 // If a putback character is available then we return that
1104 // character. However, we are not supposed to change the value of
1105 // gptr() in this routine so we leave mPutbackAvailable set to true.
1106 if (mPutbackAvailable)
1107 {
1108 next_char = mPutbackChar;
1109 }
1110 else
1111 {
1112 // If no putback character is available then we need to read one
1113 // character from the serial port.
1114 //
1115 // :TODO: Consider using a method of SerialPort class here instead
1116 // of using the file descriptor.
1117 //
1118 const auto fd = mSerialPort.GetFileDescriptor() ;
1119 result = call_with_retry(read, fd, &next_char, 1) ;
1120
1121 // Make the next character the putback character. This has the
1122 // effect of returning the next character without changing gptr()
1123 // as required by the C++ standard.
1124 if (result == 1)
1125 {
1126 mPutbackChar = next_char;
1127 mPutbackAvailable = true ;
1128 }
1129 else if (result <= 0)
1130 {
1131 // If we had a problem reading the character, we return
1132 // traits::eof().
1133 return traits_type::eof() ;
1134 }
1135 }
1136
1137 // :NOTE: Wed Aug 9 21:26:51 2000 Pagey
1138 // The value of mPutbackAvailable is always true when the code
1139 // reaches here.
1140
1141 // Return the character as an int value as required by the C++
1142 // standard.
1143 return traits_type::to_int_type(next_char) ;
1144 }
1145
1146 inline
1147 std::streambuf::int_type
1149 {
1150 // Throw an exception if the serial port is not open.
1151 if (!this->IsOpen())
1152 {
1153 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1154 }
1155
1156 int_type next_char = underflow() ;
1157
1158 mPutbackAvailable = false;
1159
1160 return next_char;
1161 }
1162
1163 inline
1164 std::streambuf::int_type
1166 {
1167 // Throw an exception if the serial port is not open.
1168 if (!this->IsOpen())
1169 {
1170 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1171 }
1172
1173 // If a putback character is already available, then we cannot
1174 // do any more putback and hence need to return eof.
1175 if (mPutbackAvailable)
1176 {
1177 return traits_type::eof() ;
1178 }
1179 if (traits_type::eq_int_type(character, traits_type::eof()))
1180 {
1181 // If an eof character is passed in, then we are required to
1182 // backup one character. However, we cannot do this for a serial
1183 // port. Hence we return eof to signal an error.
1184 return traits_type::eof() ;
1185 }
1186 //
1187 // If no putback character is available at present, then make
1188 // "character" the putback character and return it.
1189 mPutbackChar = traits_type::to_char_type(character) ;
1190 mPutbackAvailable = true ;
1191 return traits_type::not_eof(character) ;
1192 }
1193
1194 inline
1195 std::streamsize
1197 {
1198 // Throw an exception if the serial port is not open.
1199 if (!this->IsOpen())
1200 {
1201 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1202 }
1203
1204 ssize_t number_of_bytes_available = 0 ;
1205
1206 // NOLINTNEXTLINE (cppcoreguidelines-pro-type-vararg)
1207 //
1208 // :TODO: Consider using a method of SerialPort class here instead
1209 // of using the file descriptor.
1210 const auto fd = mSerialPort.GetFileDescriptor() ;
1211 const auto result = call_with_retry(ioctl,
1212 fd,
1213 FIONREAD,
1214 &number_of_bytes_available) ;
1215
1216 if ((result >= 0) and (number_of_bytes_available > 0))
1217 {
1218 mPutbackAvailable = true ;
1219 }
1220
1221 return number_of_bytes_available;
1222 }
1223} // namespace LibSerial
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 ...
Definition SerialPort.h:56
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 & 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:...