Skip to content

Commit 46a1bb1

Browse files
authored
Merge pull request #97 from mcsauder/c++14
Updated SerialPort Read() methods
2 parents eedbbd8 + fdb5e6a commit 46a1bb1

17 files changed

+1067
-742
lines changed

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
# Libserial
22

33
----
4-
LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
4+
Thanks for checking out LibSerial! LibSerial provides a convenient, object oriented approach to accessing serial ports on POSIX systems.
55

6-
You will need a recent g++ release, (anything after gcc-3.2 should work), to compile libserial, and you will also need to install Google Test (gtest) and the boost unit test library. For Debian users:
6+
After you get to know LibSerial a bit, if you find that you have ideas for improvement, please be sure to let us know!
7+
8+
If you simply want to use LibSerial and you already utilize a Debian Linux distribution, use apt to install the current release package:
9+
10+
```
11+
sudo apt install libserial-dev
12+
```
13+
14+
Otherwise, if you are a developer and would like to make use of the latest code, you will need to have a few packages installed to build LibSerial:
15+
a recent g++ release, (anything after gcc-3.2 should work), the python sip library, the boost unit test library, and Google Test (gtest). For Debian users:
716

817
```
9-
sudo apt install libgtest-dev libboost-dev
18+
sudo update
19+
sudo apt install build-essential libgtest-dev libboost-dev python-sip-dev
1020
```
1121
----
1222
If you get the source code from github and would like to install the library, you will need to generate the configure script first:
@@ -16,16 +26,26 @@ make -f Makefile.dist
1626
```
1727

1828
----
19-
You can skip this step if you are using a release package (which already contains the `configure` script). Once you have the `configure` script, run the following commands:
29+
Then run `configure`:
2030

2131
```
2232
./configure
33+
```
34+
35+
You can specify an installation directory different from the default, (/usr/local/), by adding `--prefix=/installation/directory/path/` to the configure command. For example:
36+
```
37+
./configure --prefix=/usr/include/
38+
```
39+
40+
Once you have the `configure` script, you can build the library with `make` and install with `make install`:
41+
42+
```
2343
make
24-
make install
44+
sudo make install
2545
```
2646

2747
----
28-
If you are a developer interested in utilizing the unit tests, ensure serial port names are appropriate for your hardware configuration in the UnitTests.cpp file:
48+
If you are interested in running the unit tests, ensure serial port names are appropriate for your hardware configuration in the UnitTests.cpp file:
2949

3050
```
3151
#define TEST_SERIAL_PORT_1 "/dev/ttyUSB0"
@@ -54,4 +74,4 @@ Alternatively, unit test executables built using the compile script can be run f
5474
Complete documentation is available [here](http://libserial.readthedocs.io/en/latest/index.html).
5575

5676
----
57-
(You can let people know that this Repository was useful to you by clicking the "Star" in the upper right of the repository home page!)
77+
(You can let people know that this Repository was useful to you by clicking the "star" in the upper right of the repository home page!)

compile.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/bin/bash
12
mkdir -p build
23
cd build
34
cmake ..

examples/main_page_example.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ int main()
2828
char read_byte_2 = 'B';
2929

3030
// Write a character.
31-
serial_port.Write(&write_byte_1, 1);
31+
serial_port.WriteByte(write_byte_1);
3232
serial_stream << write_byte_2;
3333

34+
size_t timeout_milliseconds = 5;
35+
3436
try
3537
{
3638
// Read a character.
37-
serial_port.Read(read_byte_1, 1);
39+
serial_port.ReadByte(read_byte_1, timeout_milliseconds);
3840
serial_stream >> read_byte_2;
3941
}
4042
catch (ReadTimeout)

examples/serial_port_read.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
#include <SerialPort.h>
66

7+
#include <cstring>
8+
#include <cstdlib>
79
#include <iostream>
810
#include <unistd.h>
9-
#include <cstdlib>
1011

1112
using namespace LibSerial;
1213

@@ -44,32 +45,46 @@ int main()
4445
}
4546

4647
// Timeout value in milliseconds to wait for data being read.
47-
size_t ms_timeout = 25;
48+
size_t ms_timeout = 250;
4849

49-
// Variable to store data coming from the serial port.
50+
// Char variable to store data coming from the serial port.
5051
char data_byte;
5152

52-
// Keep reading data from serial port and print it to the screen.
53-
while(serial_port.IsDataAvailable())
53+
// Read one byte from the serial port and print it to the terminal.
54+
try
5455
{
55-
try
56-
{
57-
// Read a single byte of data from the serial port.
58-
serial_port.ReadByte(data_byte, ms_timeout);
56+
// Read a single byte of data from the serial port.
57+
serial_port.ReadByte(data_byte, ms_timeout);
5958

60-
// Show the user what is being read from the serial port.
61-
std::cout << data_byte;
62-
}
63-
catch (ReadTimeout)
59+
// Show the user what is being read from the serial port.
60+
std::cout << data_byte << std::flush;
61+
}
62+
catch (ReadTimeout)
63+
{
64+
std::cerr << "\nThe ReadByte() call has timed out." << std::endl;
65+
}
66+
67+
// Wait a brief period for more data to arrive.
68+
usleep(1000);
69+
70+
DataBuffer read_buffer;
71+
72+
try
73+
{
74+
// Read as many bytes as are available during the timeout period.
75+
serial_port.Read(read_buffer, 0, ms_timeout);
76+
}
77+
catch (ReadTimeout)
78+
{
79+
for (size_t i = 0; i < read_buffer.size(); i++)
6480
{
65-
std::cerr << "The ReadByte() call has timed out." << std::endl;
81+
std::cout << read_buffer.at(i) << std::flush;
6682
}
6783

68-
// Wait a brief period for more data to arrive.
69-
usleep(1000);
84+
std::cerr << "The Read() call timed out waiting for additional data." << std::endl;
7085
}
7186

7287
// Successful program completion.
73-
std::cout << "Done." << std::endl;
88+
std::cout << "The example program successfully completed!" << std::endl;
7489
return EXIT_SUCCESS;
7590
}

examples/serial_port_read_write.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ int main()
6161
char read_byte_2 = ' ';
6262

6363
// Variables to store outgoing and incoming data.
64-
std::string write_string_1 = "\"Do what you can, with what you have, where you are.\" - Theodore Roosevelt";
65-
std::string write_string_2 = "\"Simplicity is prerequisite for reliability.\" - Edsger W. Dijkstra";
64+
std::string write_string_1 =
65+
"\"Do what you can, with what you have, where you are.\" - Theodore Roosevelt";
66+
67+
std::string write_string_2 =
68+
"\"Simplicity is prerequisite for reliability.\" - Edsger W. Dijkstra";
6669

6770
std::string read_string_1 = "";
6871
std::string read_string_2 = "";
@@ -75,6 +78,10 @@ int main()
7578
serial_port_1.WriteByte(write_byte_1);
7679
serial_port_2.WriteByte(write_byte_2);
7780

81+
// Wait until the data has actually been transmitted.
82+
serial_port_1.DrainWriteBuffer();
83+
serial_port_2.DrainWriteBuffer();
84+
7885
// Specify a read timeout value in milliseconds.
7986
size_t timeout_milliseconds = 25;
8087

@@ -106,6 +113,10 @@ int main()
106113
serial_port_1.Write(write_string_1);
107114
serial_port_2.Write(write_string_2);
108115

116+
// Wait until the data has actually been transmitted.
117+
serial_port_1.DrainWriteBuffer();
118+
serial_port_2.DrainWriteBuffer();
119+
109120
try
110121
{
111122
// Read the appropriate number of bytes from each serial port.
@@ -130,9 +141,13 @@ int main()
130141
std::string user_input;
131142
user_input.clear();
132143

144+
// Print to the terminal what will take place next.
145+
std::cout << "Using Write() and ReadLine() to write a string and "
146+
<< "read a line of data:" << std::endl << std::endl;
147+
133148
// Prompt the user for input.
134-
std::cout << "Type something you would like to send over serial,"
135-
<< " (enter \"Q\" or \"q\" to quit): " << std::flush;
149+
std::cout << "Enter something you would like to send over "
150+
<< "serial, (enter \"Q\" or \"q\" to quit): " << std::flush;
136151

137152
while(true)
138153
{
@@ -145,10 +160,6 @@ int main()
145160
break;
146161
}
147162

148-
// Print to the terminal what will take place next.
149-
std::cout << "Using Write() and ReadLine() to write a string and "
150-
<< "read a line of data:" << std::endl;
151-
152163
// Write the user input to the serial port.
153164
serial_port_1.Write(user_input + "\n");
154165

@@ -164,6 +175,7 @@ int main()
164175
serial_port_1.Close();
165176
serial_port_2.Close();
166177

178+
// Successful program completion.
167179
std::cout << "The example program successfully completed!" << std::endl;
168180
return EXIT_SUCCESS;
169181
}

examples/serial_port_write.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ int main(int argc, char** argv)
7474
// Write the data to the serial port.
7575
serial_port.WriteByte(data_byte);
7676

77+
// Wait until the data has actually been transmitted.
78+
serial_port.DrainWriteBuffer();
79+
7780
// Print to the terminal what is being written to the serial port.
7881
std::cout << data_byte;
7982
}
8083

8184
// Successful program completion.
82-
std::cout << std::endl << "Done." << std::endl;
85+
std::cout << "The example program successfully completed!" << std::endl;
8386
return EXIT_SUCCESS;
8487
}

examples/serial_stream_read_write.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ int main()
6969
serial_stream_1.write(write_string_1.c_str(), write_string_1.size());
7070
serial_stream_2.write(write_string_2.c_str(), write_string_2.size());
7171

72+
// Wait until the data has actually been transmitted.
73+
serial_stream_1.DrainWriteBuffer();
74+
serial_stream_2.DrainWriteBuffer();
75+
7276
// Char arrays to store incoming data.
7377
char* read_array_1 = new char[write_string_2.size()];
7478
char* read_array_2 = new char[write_string_1.size()];
@@ -94,6 +98,10 @@ int main()
9498
serial_stream_1 << write_string_1 << std::endl;
9599
serial_stream_2 << write_string_2 << std::endl;
96100

101+
// Wait until the data has actually been transmitted.
102+
serial_stream_1.DrainWriteBuffer();
103+
serial_stream_2.DrainWriteBuffer();
104+
97105
// Read a line at each serial port.
98106
std::getline(serial_stream_1, read_string_1);
99107
std::getline(serial_stream_2, read_string_2);
@@ -147,6 +155,7 @@ int main()
147155
serial_stream_1.Close();
148156
serial_stream_2.Close();
149157

158+
// Successful program completion.
150159
std::cout << "The example program successfully completed!" << std::endl;
151160
return EXIT_SUCCESS;
152161
}

examples/serial_stream_write.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,14 @@ int main(int argc, char** argv)
7474
// Write the data to the serial port.
7575
serial_stream.write(&data_byte, 1);
7676

77+
// Wait until the data has actually been transmitted.
78+
serial_stream.DrainWriteBuffer();
79+
7780
// Print to the terminal what is being written to the serial port.
7881
std::cout << data_byte;
7982
}
8083

8184
// Successful program completion.
82-
std::cout << std::endl << "Done." << std::endl;
85+
std::cout << "The example program successfully completed!" << std::endl;
8386
return EXIT_SUCCESS;
8487
}

sip/SerialPort.sip

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,10 @@ public:
106106
int
107107
GetFileDescriptor();
108108

109+
// NOTE: There is not a Python equivalent to std::vector.
109110
// std::vector<std::string>
110111
// GetAvailableSerialPorts();
111112

112-
// NOTE: Python3 provides a mechanism for method overloading, however Python2 does not.
113-
// void
114-
// Read(char& charBuffer,
115-
// const unsigned int numberOfBytes = 0,
116-
// const unsigned int msTimeout = 0);
117-
118-
void
119-
Read(unsigned char& charBuffer,
120-
const unsigned int numberOfBytes = 0,
121-
const unsigned int msTimeout = 0);
122-
123113
void
124114
Read(LibSerial::DataBuffer& dataBuffer,
125115
const unsigned int numOfBytes = 0,
@@ -144,27 +134,18 @@ public:
144134
const char lineTerminator = 10, // Sip does not like '\n'
145135
const unsigned int msTimeout = 0);
146136

147-
// NOTE: Python3 provides a mechanism for method overloading, however Python2 does not.
148-
// void
149-
// Write(const char* charBuffer,
150-
// const unsigned int numberOfBytes);
151-
152-
void
153-
Write(const unsigned char* charBuffer,
154-
const unsigned int numberOfBytes);
155-
156137
void
157138
Write(const LibSerial::DataBuffer& dataBuffer);
158139

159140
void
160141
Write(const std::string& dataString);
161142

143+
void
144+
WriteByte(const char charbuffer);
145+
162146
// NOTE: Python3 provides a mechanism for method overloading, however Python2 does not.
163147
// void
164-
// WriteByte(const char charbuffer);
165-
166-
void
167-
WriteByte(const unsigned char charbuffer);
148+
// WriteByte(const unsigned char charbuffer);
168149

169150
private:
170151
SerialPort(const SerialPort& otherSerialPort);

0 commit comments

Comments
 (0)