Skip to content

Commit 71e8887

Browse files
authored
Merge pull request #34 from gbmhunter/develop
Release of v2.6.0.
2 parents 3f6c26f + a8dea5a commit 71e8887

File tree

5 files changed

+88
-6
lines changed

5 files changed

+88
-6
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
77

8+
## [v2.6.0] - 2023-02-02
9+
10+
- `Read()` and `ReadBinary()` now throw exceptions if they detect that the serial device has been disconnected (thanks to [aldoshkind](https://github.com/aldoshkind) for helping with this one).
11+
- Added Arduino testing instructions to the README.
12+
813
## [v2.5.0] - 2022-11-12
914

1015
- Replaced all tabs in code with spaces, which should fix the ugly code rendering in GitHub.

README.md

+41-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,47 @@ Attaching the Arduino Uno (need to be done with Admin priviliges the first time
143143
usbipd wsl attach --busid=1-5
144144
```
145145

146-
`/dev/ttyACM0` now appears inside WSL, and you can use CppLinuxSerial with this device like usual.
146+
`/dev/ttyACM0` now appears inside WSL, and you can use `CppLinuxSerial` with this device like usual.
147+
148+
NOTE: Sometimes `/dev/ttyACM0` is not part of the dialout group, so even with your user being part of that group, you will get permission denied errors when trying to access the serial port. Sometimes using `chmod` to change the permissions works:
149+
150+
```
151+
sudo chmod 666 /dev/ttyACM0
152+
```
153+
154+
## Tests
155+
156+
### Prerequisties
157+
158+
Install arduino:avr platform:
159+
160+
```
161+
$ arduino-cli core install arduino:avr
162+
```
163+
164+
Detect attached Arduino boards with:
165+
166+
```
167+
$ arduino-cli board list
168+
```
169+
170+
Compile:
171+
172+
```
173+
arduino-cli compile --fqbn arduino:avr:uno Basic/
174+
```
175+
176+
Upload:
177+
178+
```
179+
arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:uno Basic/
180+
```
181+
182+
### Running
183+
184+
```
185+
g++ main.cpp -lCppLinuxSerial
186+
```
147187

148188
## Changelog
149189

include/CppLinuxSerial/SerialPort.hpp

+6-1
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,19 @@ namespace mn {
175175
/// \param data The object the read characters from the COM port will be saved to.
176176
/// \param wait_ms The amount of time to wait for data. Set to 0 for non-blocking mode. Set to -1
177177
/// to wait indefinitely for new data.
178-
/// \throws CppLinuxSerial::Exception if state != OPEN.
178+
/// \note Use ReadBinary() if you want to interpret received data as binary.
179+
/// \throws
180+
/// CppLinuxSerial::Exception if state != OPEN.
181+
/// std::system_error() if device has been disconnected.
179182
void Read(std::string& data);
180183

181184
/// \brief Use to read binary data from the COM port.
182185
/// \param data The object the read uint8_t bytes from the COM port will be saved to.
183186
/// \param wait_ms The amount of time to wait for data. Set to 0 for non-blocking mode. Set to -1
184187
/// to wait indefinitely for new data.
188+
/// \note Use Read() if you want to interpret received data as a string.
185189
/// \throws CppLinuxSerial::Exception if state != OPEN.
190+
/// std::system_error() if device has been disconnected.
186191
void ReadBinary(std::vector<uint8_t>& data);
187192

188193
/// \brief Use to get number of bytes available in receive buffer.

src/SerialPort.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,16 @@ namespace CppLinuxSerial {
538538
// Read was unsuccessful
539539
throw std::system_error(EFAULT, std::system_category());
540540
}
541+
else if(n == 0) {
542+
// n == 0 means EOS, but also returned on device disconnection. We try to get termios2 to distinguish two these two states
543+
struct termios2 term2;
544+
int rv = ioctl(fileDesc_, TCGETS2, &term2);
541545

542-
if(n > 0) {
546+
if(rv != 0) {
547+
throw std::system_error(EFAULT, std::system_category());
548+
}
549+
}
550+
else if(n > 0) {
543551
data = std::string(&readBuffer_[0], n);
544552
}
545553

@@ -567,8 +575,16 @@ namespace CppLinuxSerial {
567575
// Read was unsuccessful
568576
throw std::system_error(EFAULT, std::system_category());
569577
}
578+
else if(n == 0) {
579+
// n == 0 means EOS, but also returned on device disconnection. We try to get termios2 to distinguish two these two states
580+
struct termios2 term2;
581+
int rv = ioctl(fileDesc_, TCGETS2, &term2);
570582

571-
if(n > 0) {
583+
if(rv != 0) {
584+
throw std::system_error(EFAULT, std::system_category());
585+
}
586+
}
587+
else if(n > 0) {
572588
copy(readBuffer_.begin(), readBuffer_.begin() + n, back_inserter(data));
573589
}
574590

test/arduino/main.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
using namespace mn::CppLinuxSerial;
44

5+
// Overload to be able to print vectors to std::cout
6+
template <typename T>
7+
std::ostream& operator<<( std::ostream& ostrm, const std::vector<T>& vec ){
8+
for( int j = 0, n = vec.size(); j < n; ++j ){
9+
ostrm << ",["[ !j ] << " " << vec[ j ];
10+
}
11+
return ostrm << " ]";
12+
}
13+
514
int main() {
615
// Create serial port object and open serial port
716
SerialPort serialPort("/dev/ttyACM0", BaudRate::B_9600, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE);
@@ -13,9 +22,16 @@ int main() {
1322
// serialPort0.Write("Hello");
1423

1524
// Read some data back
25+
// while(1) {
26+
// std::string readData;
27+
// serialPort.Read(readData);
28+
// std::cout << "Received data: " << readData;
29+
// }
30+
31+
// Read some data back (raw)
1632
while(1) {
17-
std::string readData;
18-
serialPort.Read(readData);
33+
std::vector<unsigned char> readData;
34+
serialPort.ReadBinary(readData);
1935
std::cout << "Received data: " << readData;
2036
}
2137

0 commit comments

Comments
 (0)