Skip to content

Commit

Permalink
Merge pull request #34 from gbmhunter/develop
Browse files Browse the repository at this point in the history
Release of v2.6.0.
  • Loading branch information
gbmhunter authored Feb 2, 2023
2 parents 3f6c26f + a8dea5a commit 71e8887
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [v2.6.0] - 2023-02-02

- `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).
- Added Arduino testing instructions to the README.

## [v2.5.0] - 2022-11-12

- Replaced all tabs in code with spaces, which should fix the ugly code rendering in GitHub.
Expand Down
42 changes: 41 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,47 @@ Attaching the Arduino Uno (need to be done with Admin priviliges the first time
usbipd wsl attach --busid=1-5
```

`/dev/ttyACM0` now appears inside WSL, and you can use CppLinuxSerial with this device like usual.
`/dev/ttyACM0` now appears inside WSL, and you can use `CppLinuxSerial` with this device like usual.

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:

```
sudo chmod 666 /dev/ttyACM0
```

## Tests

### Prerequisties

Install arduino:avr platform:

```
$ arduino-cli core install arduino:avr
```

Detect attached Arduino boards with:

```
$ arduino-cli board list
```

Compile:

```
arduino-cli compile --fqbn arduino:avr:uno Basic/
```

Upload:

```
arduino-cli upload -p /dev/ttyACM0 --fqbn arduino:avr:uno Basic/
```

### Running

```
g++ main.cpp -lCppLinuxSerial
```

## Changelog

Expand Down
7 changes: 6 additions & 1 deletion include/CppLinuxSerial/SerialPort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,14 +175,19 @@ namespace mn {
/// \param data The object the read characters from the COM port will be saved to.
/// \param wait_ms The amount of time to wait for data. Set to 0 for non-blocking mode. Set to -1
/// to wait indefinitely for new data.
/// \throws CppLinuxSerial::Exception if state != OPEN.
/// \note Use ReadBinary() if you want to interpret received data as binary.
/// \throws
/// CppLinuxSerial::Exception if state != OPEN.
/// std::system_error() if device has been disconnected.
void Read(std::string& data);

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

/// \brief Use to get number of bytes available in receive buffer.
Expand Down
20 changes: 18 additions & 2 deletions src/SerialPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,16 @@ namespace CppLinuxSerial {
// Read was unsuccessful
throw std::system_error(EFAULT, std::system_category());
}
else if(n == 0) {
// n == 0 means EOS, but also returned on device disconnection. We try to get termios2 to distinguish two these two states
struct termios2 term2;
int rv = ioctl(fileDesc_, TCGETS2, &term2);

if(n > 0) {
if(rv != 0) {
throw std::system_error(EFAULT, std::system_category());
}
}
else if(n > 0) {
data = std::string(&readBuffer_[0], n);
}

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

if(n > 0) {
if(rv != 0) {
throw std::system_error(EFAULT, std::system_category());
}
}
else if(n > 0) {
copy(readBuffer_.begin(), readBuffer_.begin() + n, back_inserter(data));
}

Expand Down
20 changes: 18 additions & 2 deletions test/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

using namespace mn::CppLinuxSerial;

// Overload to be able to print vectors to std::cout
template <typename T>
std::ostream& operator<<( std::ostream& ostrm, const std::vector<T>& vec ){
for( int j = 0, n = vec.size(); j < n; ++j ){
ostrm << ",["[ !j ] << " " << vec[ j ];
}
return ostrm << " ]";
}

int main() {
// Create serial port object and open serial port
SerialPort serialPort("/dev/ttyACM0", BaudRate::B_9600, NumDataBits::EIGHT, Parity::NONE, NumStopBits::ONE);
Expand All @@ -13,9 +22,16 @@ int main() {
// serialPort0.Write("Hello");

// Read some data back
// while(1) {
// std::string readData;
// serialPort.Read(readData);
// std::cout << "Received data: " << readData;
// }

// Read some data back (raw)
while(1) {
std::string readData;
serialPort.Read(readData);
std::vector<unsigned char> readData;
serialPort.ReadBinary(readData);
std::cout << "Received data: " << readData;
}

Expand Down

0 comments on commit 71e8887

Please sign in to comment.