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
74 Implementation(const std::string& fileName,
75 const BaudRate& baudRate,
76 const CharacterSize& characterSize,
77 const FlowControl& flowControlType,
78 const Parity& parityType,
79 const StopBits& stopBits,
80 bool exclusive) ;
81
85 Implementation(const Implementation& otherImplementation) = delete ;
86
90 Implementation(const Implementation&& otherImplementation) = delete ;
91
95 Implementation& operator=(const Implementation& otherImplementation) = delete ;
96
100 Implementation& operator=(const Implementation&& otherImplementation) = delete ;
101
110 void Open(const std::string& fileName,
111 const std::ios_base::openmode& openMode,
112 bool exclusive) ;
113
118 void Close() ;
119
123 void DrainWriteBuffer() ;
124
128 void FlushInputBuffer() ;
129
133 void FlushOutputBuffer() ;
134
138 void FlushIOBuffers() ;
139
143 bool IsDataAvailable() ;
144
149 bool IsOpen() const ;
150
155
160 void SetBaudRate(const BaudRate& baudRate) ;
161
166 BaudRate GetBaudRate() const ;
167
172 void SetCharacterSize(const CharacterSize& characterSize) ;
173
178 CharacterSize GetCharacterSize() const ;
179
184 void SetFlowControl(const FlowControl& flowControlType) ;
185
190 FlowControl GetFlowControl() const ;
191
196 void SetParity(const Parity& parityType) ;
197
202 Parity GetParity() const ;
203
208 void SetStopBits(const StopBits& stopBits) ;
209
214 StopBits GetStopBits() const ;
215
220 void SetVMin(const short vmin) ;
221
227 short GetVMin() const ;
228
233 void SetVTime(const short vtime) ;
234
239 short GetVTime() const ;
240
245 void SetDTR(const bool dtrState) ;
246
251 bool GetDTR() const ;
252
257 void SetRTS(const bool rtsState) ;
258
263 bool GetRTS() const ;
264
269 bool GetCTS() ;
270
275 bool GetDSR() ;
276
281 int GetFileDescriptor() const ;
282
288
289#ifdef __linux__
295 std::vector<std::string> GetAvailableSerialPorts() const ;
296#endif
297
306 std::streamsize xsputn(const char_type* character,
307 std::streamsize numberOfBytes) ;
308
317 std::streamsize xsgetn(char_type* character,
318 std::streamsize numberOfBytes) ;
319
325 std::streambuf::int_type overflow(int_type character) ;
326
334 std::streambuf::int_type underflow() ;
335
343 std::streambuf::int_type uflow() ;
344
353 std::streambuf::int_type pbackfail(int_type character) ;
354
360 std::streamsize showmanyc() ;
361
367 bool mPutbackAvailable {false} ;
368
376 char mPutbackChar {0} ;
377
378 private:
379
383 SerialPort mSerialPort {} ;
384 } ;
385
387 : mImpl(new Implementation)
388 {
389 setbuf(nullptr, 0) ;
390 }
391
392 SerialStreamBuf::SerialStreamBuf(const std::string& fileName,
393 const BaudRate& baudRate,
394 const CharacterSize& characterSize,
395 const FlowControl& flowControlType,
396 const Parity& parityType,
397 const StopBits& stopBits,
398 bool exclusive)
399 : mImpl(new Implementation(fileName,
400 baudRate,
401 characterSize,
402 flowControlType,
403 parityType,
404 stopBits,
405 exclusive))
406 {
407 setbuf(nullptr, 0) ;
408 }
409
411
412 void
413 SerialStreamBuf::Open(const std::string& fileName,
414 const std::ios_base::openmode& openMode,
415 bool exclusive)
416 {
417 mImpl->Open(fileName,
418 openMode,
419 exclusive) ;
420 }
421
422 void
424 {
425 mImpl->Close() ;
426 }
427
428 void
430 {
431 mImpl->DrainWriteBuffer() ;
432 }
433
434 void
436 {
437 mImpl->FlushInputBuffer() ;
438 }
439
440 void
442 {
443 mImpl->FlushOutputBuffer() ;
444 }
445
446 void
448 {
449 mImpl->FlushIOBuffers() ;
450 }
451
452 bool
454 {
455 return mImpl->IsDataAvailable() ;
456 }
457
458 bool
460 {
461 return mImpl->IsOpen() ;
462 }
463
464 void
466 {
467 mImpl->SetDefaultSerialPortParameters() ;
468 }
469
470 void
471 SerialStreamBuf::SetBaudRate(const BaudRate& baudRate)
472 {
473 mImpl->SetBaudRate(baudRate) ;
474 }
475
476 BaudRate
478 {
479 return mImpl->GetBaudRate() ;
480 }
481
482 void
483 SerialStreamBuf::SetCharacterSize(const CharacterSize& characterSize)
484 {
485 mImpl->SetCharacterSize(characterSize) ;
486 }
487
488 CharacterSize
490 {
491 return mImpl->GetCharacterSize() ;
492 }
493
494 void
495 SerialStreamBuf::SetFlowControl(const FlowControl& flowControlType)
496 {
497 mImpl->SetFlowControl(flowControlType) ;
498 }
499
500 FlowControl
502 {
503 return mImpl->GetFlowControl() ;
504 }
505
506 void
507 SerialStreamBuf::SetParity(const Parity& parityType)
508 {
509 mImpl->SetParity(parityType) ;
510 }
511
512 Parity
514 {
515 return mImpl->GetParity() ;
516 }
517
518 void
519 SerialStreamBuf::SetStopBits(const StopBits& stopBits)
520 {
521 mImpl->SetStopBits(stopBits) ;
522 }
523
524 StopBits
526 {
527 return mImpl->GetStopBits() ;
528 }
529
530 void
531 SerialStreamBuf::SetVMin(const short vmin)
532 {
533 mImpl->SetVMin(vmin) ;
534 }
535
536 short
538 {
539 return mImpl->GetVMin() ;
540 }
541
542 void
543 SerialStreamBuf::SetVTime(const short vtime)
544 {
545 mImpl->SetVTime(vtime) ;
546 }
547
548 short
550 {
551 return mImpl->GetVTime() ;
552 }
553
554 void
555 SerialStreamBuf::SetDTR(const bool dtrState)
556 {
557 mImpl->SetDTR(dtrState) ;
558 }
559
560 bool
562 {
563 return mImpl->GetDTR() ;
564 }
565
566 void
567 SerialStreamBuf::SetRTS(const bool rtsState)
568 {
569 mImpl->SetRTS(rtsState) ;
570 }
571
572 bool
574 {
575 return mImpl->GetRTS() ;
576 }
577
578 bool
580 {
581 return mImpl->GetCTS() ;
582 }
583
584 bool
586 {
587 return mImpl->GetDSR() ;
588 }
589
590 int
592 {
593 return mImpl->GetFileDescriptor() ;
594 }
595
596 int
598 {
599 return mImpl->GetNumberOfBytesAvailable() ;
600 }
601
602#ifdef __linux__
603 std::vector<std::string>
604 SerialStreamBuf::GetAvailableSerialPorts() const
605 {
606 return mImpl->GetAvailableSerialPorts() ;
607 }
608#endif
609
610 std::streambuf*
611 SerialStreamBuf::setbuf(char_type* character, std::streamsize numberOfBytes)
612 {
613 return std::streambuf::setbuf(character, numberOfBytes) ;
614 }
615
616 std::streamsize
617 SerialStreamBuf::xsputn(const char_type* character, std::streamsize numberOfBytes)
618 {
619 return mImpl->xsputn(character, numberOfBytes) ;
620 }
621
622 std::streamsize
623 SerialStreamBuf::xsgetn(char_type* character, std::streamsize numberOfBytes)
624 {
625 return mImpl->xsgetn(character, numberOfBytes) ;
626 }
627
628 std::streambuf::int_type
629 SerialStreamBuf::overflow(const int_type character)
630 {
631 return mImpl->overflow(character) ;
632 }
633
634 std::streambuf::int_type
636 {
637 return mImpl->underflow() ;
638 }
639
640 std::streambuf::int_type
642 {
643 return mImpl->uflow() ;
644 }
645
646 std::streambuf::int_type
647 SerialStreamBuf::pbackfail(const int_type character)
648 {
649 return mImpl->pbackfail(character) ;
650 }
651
652 std::streamsize
654 {
655 return mImpl->showmanyc() ;
656 }
657
658
661 inline
663 const BaudRate& baudRate,
664 const CharacterSize& characterSize,
665 const FlowControl& flowControlType,
666 const Parity& parityType,
667 const StopBits& stopBits,
668 bool exclusive)
669 try : mSerialPort(fileName,
670 baudRate,
671 characterSize,
672 flowControlType,
673 parityType,
674 stopBits,
675 exclusive)
676 {
677 // empty
678 }
679 catch (const std::exception& err)
680 {
681 throw OpenFailed(err.what()) ;
682 }
683
684 inline
686 try
687 {
688 // Close the serial port if it is open.
689 if (IsOpen())
690 {
691 Close() ;
692 }
693 }
694 catch(...)
695 {
696 //
697 // :IMPORTANT: We do not let any exceptions escape the destructor.
698 // (see https://isocpp.org/wiki/faq/exceptions#dtors-shouldnt-throw)
699 //
700 // :TODO: Once we add logging to LibSerial, we should issue a warning
701 // if we reach here.
702 //
703 }
704
705 inline
706 void
707 SerialStreamBuf::Implementation::Open(const std::string& fileName,
708 const std::ios_base::openmode& openMode,
709 bool exclusive)
710 try
711 {
712 mSerialPort.Open(fileName,
713 openMode,
714 exclusive) ;
715
716 // @note - Stream communications need to happen in blocking mode.
717 mSerialPort.SetSerialPortBlockingStatus(true) ;
718 }
719 catch (const AlreadyOpen&)
720 {
721 throw ;
722 }
723 catch (const OpenFailed&)
724 {
725 throw ;
726 }
727 catch (const std::exception& err)
728 {
729 throw OpenFailed(err.what()) ;
730 }
731
732 inline
733 void
735 {
736 mSerialPort.Close() ;
737 }
738
739 inline
740 void
742 {
743 mSerialPort.DrainWriteBuffer() ;
744 }
745
746 inline
747 void
749 {
750 mSerialPort.FlushInputBuffer() ;
751 }
752
753 inline
754 void
756 {
757 mSerialPort.FlushOutputBuffer() ;
758 }
759
760 inline
761 void
763 {
764 mSerialPort.FlushIOBuffers() ;
765 }
766
767 inline
768 bool
770 {
771 return mSerialPort.IsOpen() ;
772 }
773
774 inline
775 bool
777 {
778 return mSerialPort.IsDataAvailable() ;
779 }
780
781 inline
782 void
784 {
785 mSerialPort.SetDefaultSerialPortParameters() ;
786 }
787
788 inline
789 void
791 {
792 mSerialPort.SetBaudRate(baudRate) ;
793 }
794
795 inline
796 BaudRate
798 {
799 return mSerialPort.GetBaudRate() ;
800 }
801
802 inline
803 void
804 SerialStreamBuf::Implementation::SetCharacterSize(const CharacterSize& characterSize)
805 {
806 mSerialPort.SetCharacterSize(characterSize) ;
807 }
808
809 inline
810 CharacterSize
812 {
813 return mSerialPort.GetCharacterSize() ;
814 }
815
816 inline
817 void
818 SerialStreamBuf::Implementation::SetFlowControl(const FlowControl& flowControlType)
819 {
820 mSerialPort.SetFlowControl(flowControlType) ;
821 }
822
823 inline
824 FlowControl
826 {
827 return mSerialPort.GetFlowControl() ;
828 }
829
830 inline
831 void
833 {
834 mSerialPort.SetParity(parityType) ;
835 }
836
837 inline
838 Parity
840 {
841 return mSerialPort.GetParity() ;
842 }
843
844 inline
845 void
847 {
848 mSerialPort.SetStopBits(stopBits) ;
849 }
850
851 inline
852 StopBits
854 {
855 return mSerialPort.GetStopBits() ;
856 }
857
858 inline
859 void
861 {
862 mSerialPort.SetVMin(vmin) ;
863 }
864
865 inline
866 short
868 {
869 return mSerialPort.GetVMin() ;
870 }
871
872 inline
873 void
875 {
876 mSerialPort.SetVTime(vtime) ;
877 }
878
879 inline
880 short
882 {
883 return mSerialPort.GetVTime() ;
884 }
885
886 inline
887 void
889 {
890 mSerialPort.SetDTR(dtrState) ;
891 }
892
893 inline
894 bool
896 {
897 return mSerialPort.GetDTR() ;
898 }
899
900 inline
901 void
903 {
904 mSerialPort.SetRTS(rtsState) ;
905 }
906
907 inline
908 bool
910 {
911 return mSerialPort.GetRTS() ;
912 }
913
914 inline
915 bool
917 {
918 return mSerialPort.GetCTS() ;
919 }
920
921 inline
922 bool
924 {
925 return mSerialPort.GetDSR() ;
926 }
927
928 inline
929 int
931 {
932 return mSerialPort.GetFileDescriptor() ;
933 }
934
935 inline
936 int
938 {
939 return mSerialPort.GetNumberOfBytesAvailable() ;
940 }
941
942#ifdef __linux__
943 inline
944 std::vector<std::string>
945 SerialStreamBuf::Implementation::GetAvailableSerialPorts() const
946 {
947 return mSerialPort.GetAvailableSerialPorts() ;
948 }
949#endif
950
951 inline
952 std::streamsize
953 SerialStreamBuf::Implementation::xsputn(const char_type* character,
954 std::streamsize numberOfBytes)
955 {
956 // Throw an exception if the serial port is not open.
957 if (!this->IsOpen())
958 {
959 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
960 }
961
962 // If n is less than 1 there is nothing to accomplish.
963 if (numberOfBytes <= 0)
964 {
965 return 0 ;
966 }
967
968 // Write the n characters to the serial port.
969 //
970 // :TODO: Consider using mSerialPort.Write() here instead of writing
971 // to the file descriptor directly.
972 const auto fd = mSerialPort.GetFileDescriptor() ;
973 ssize_t result = call_with_retry(write, fd, character, numberOfBytes) ;
974
975 // If the write failed then return 0.
976 if (result <= 0)
977 {
978 return 0 ;
979 }
980
981 // Otherwise, return the number of bytes actually written.
982 return result;
983 }
984
985 inline
986 std::streamsize
988 std::streamsize numberOfBytes)
989 {
990 // Throw an exception if the serial port is not open.
991 if (!this->IsOpen())
992 {
993 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
994 }
995
996 // If n is less than 1 there is nothing to accomplish.
997 if (numberOfBytes <= 0)
998 {
999 return 0 ;
1000 }
1001
1002 if (character == nullptr) {
1003 return 0 ;
1004 }
1005
1006 // Try to read up to n characters in the array s.
1007 ssize_t result = -1;
1008
1009 // If a putback character is available, then we need to read only
1010 // n-1 character.
1011 if (mPutbackAvailable)
1012 {
1013 // Put the mPutbackChar at the beginning of the array 's'.
1014 // Increment result to indicate that a character has been placed in s.
1015 character[0] = mPutbackChar;
1016 result++;
1017
1018 // The putback character is no longer available.
1019 mPutbackAvailable = false;
1020
1021 // If we need to read more than one character, then call read()
1022 // and try to read numberOfBytes-1 more characters and put them
1023 // at location starting from &character[1].
1024 if (numberOfBytes > 1)
1025 {
1026 //
1027 // :TODO: Consider using mSerialPort.Read() here instead of
1028 // using the file descriptor.
1029 //
1030 const auto fd = mSerialPort.GetFileDescriptor() ;
1031 result = call_with_retry(read, fd, &character[1], numberOfBytes-1) ;
1032
1033 // If read was successful, then we need to increment result by
1034 // one to indicate that the putback character was prepended to
1035 // the array, s. If read failed then leave result at -1.
1036 if (result != -1)
1037 {
1038 result++;
1039 }
1040 }
1041 }
1042 else
1043 {
1044 // If no putback character is available then we try to
1045 // read numberOfBytes characters.
1046 //
1047 // :TODO: Consider using mSerialPort.Read() here instead of
1048 // using the file descriptor.
1049 const auto fd = mSerialPort.GetFileDescriptor() ;
1050 result = call_with_retry(read, fd, character, numberOfBytes) ;
1051 }
1052
1053 // If result == -1 then the read call had an error, otherwise, if
1054 // result == 0 then we could not read the characters. In either
1055 // case, we return 0 to indicate that no characters could be read
1056 // from the serial port.
1057 if (result <= 0)
1058 {
1059 return 0 ;
1060 }
1061
1062 // Return the number of characters actually read from the serial port.
1063 return result;
1064 }
1065
1066 inline
1067 std::streambuf::int_type
1069 {
1070 // Throw an exception if the serial port is not open.
1071 if (!this->IsOpen())
1072 {
1073 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1074 }
1075
1076 // Try to write the specified character to the serial port.
1077 if (traits_type::eq_int_type(character, traits_type::eof()))
1078 {
1079 // If character is the eof character then we do nothing.
1080 return traits_type::eof() ;
1081 }
1082
1083 // Otherwise we write the character to the serial port.
1084 //
1085 // :TODO: Consider using a method of SerialPort class here instead
1086 // of using the file descriptor.
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) ;
1090
1091 // If the write failed then return eof.
1092 if (result <= 0)
1093 {
1094 return traits_type::eof() ;
1095 }
1096
1097 // Otherwise, return something other than eof().
1098 return traits_type::not_eof(character) ;
1099 }
1100
1101 inline
1102 std::streambuf::int_type
1104 {
1105 // Throw an exception if the serial port is not open.
1106 if (!this->IsOpen())
1107 {
1108 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1109 }
1110
1111 // Read the next character from the serial port.
1112 char next_char = 0 ;
1113 ssize_t result = -1;
1114
1115 // If a putback character is available then we return that
1116 // character. However, we are not supposed to change the value of
1117 // gptr() in this routine so we leave mPutbackAvailable set to true.
1118 if (mPutbackAvailable)
1119 {
1120 next_char = mPutbackChar;
1121 }
1122 else
1123 {
1124 // If no putback character is available then we need to read one
1125 // character from the serial port.
1126 //
1127 // :TODO: Consider using a method of SerialPort class here instead
1128 // of using the file descriptor.
1129 //
1130 const auto fd = mSerialPort.GetFileDescriptor() ;
1131 result = call_with_retry(read, fd, &next_char, 1) ;
1132
1133 // Make the next character the putback character. This has the
1134 // effect of returning the next character without changing gptr()
1135 // as required by the C++ standard.
1136 if (result == 1)
1137 {
1138 mPutbackChar = next_char;
1139 mPutbackAvailable = true ;
1140 }
1141 else if (result <= 0)
1142 {
1143 // If we had a problem reading the character, we return
1144 // traits::eof().
1145 return traits_type::eof() ;
1146 }
1147 }
1148
1149 // :NOTE: Wed Aug 9 21:26:51 2000 Pagey
1150 // The value of mPutbackAvailable is always true when the code
1151 // reaches here.
1152
1153 // Return the character as an int value as required by the C++
1154 // standard.
1155 return traits_type::to_int_type(next_char) ;
1156 }
1157
1158 inline
1159 std::streambuf::int_type
1161 {
1162 // Throw an exception if the serial port is not open.
1163 if (!this->IsOpen())
1164 {
1165 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1166 }
1167
1168 int_type next_char = underflow() ;
1169
1170 mPutbackAvailable = false;
1171
1172 return next_char;
1173 }
1174
1175 inline
1176 std::streambuf::int_type
1178 {
1179 // Throw an exception if the serial port is not open.
1180 if (!this->IsOpen())
1181 {
1182 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1183 }
1184
1185 // If a putback character is already available, then we cannot
1186 // do any more putback and hence need to return eof.
1187 if (mPutbackAvailable)
1188 {
1189 return traits_type::eof() ;
1190 }
1191 if (traits_type::eq_int_type(character, traits_type::eof()))
1192 {
1193 // If an eof character is passed in, then we are required to
1194 // backup one character. However, we cannot do this for a serial
1195 // port. Hence we return eof to signal an error.
1196 return traits_type::eof() ;
1197 }
1198 //
1199 // If no putback character is available at present, then make
1200 // "character" the putback character and return it.
1201 mPutbackChar = traits_type::to_char_type(character) ;
1202 mPutbackAvailable = true ;
1203 return traits_type::not_eof(character) ;
1204 }
1205
1206 inline
1207 std::streamsize
1209 {
1210 // Throw an exception if the serial port is not open.
1211 if (!this->IsOpen())
1212 {
1213 throw NotOpen(ERR_MSG_PORT_NOT_OPEN) ;
1214 }
1215
1216 ssize_t number_of_bytes_available = 0 ;
1217
1218 // NOLINTNEXTLINE (cppcoreguidelines-pro-type-vararg)
1219 //
1220 // :TODO: Consider using a method of SerialPort class here instead
1221 // of using the file descriptor.
1222 const auto fd = mSerialPort.GetFileDescriptor() ;
1223 const auto result = call_with_retry(ioctl,
1224 fd,
1225 FIONREAD,
1226 &number_of_bytes_available) ;
1227
1228 if ((result >= 0) and (number_of_bytes_available > 0))
1229 {
1230 mPutbackAvailable = true ;
1231 }
1232
1233 return number_of_bytes_available;
1234 }
1235} // 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.
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:...