Skip to content

Commit 121c88a

Browse files
committed
-readme updated
1 parent 4f71895 commit 121c88a

File tree

2 files changed

+73
-63
lines changed

2 files changed

+73
-63
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2023 SergeyTatarchenko
3+
Copyright (c) 2025 Siarhei Tatarchanka
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 72 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[![CMake](https://github.com/SergeyTatarchenko/simple-serial-port/actions/workflows/cmake-multi-platform.yml/badge.svg)](https://github.com/SergeyTatarchenko/simple-serial-port/actions/workflows/cmake-multi-platform.yml)
44

5-
A small library written in modern C++ for working with a serial port with limited configuration options, designed to work with chips such as ft232, ch340, etc. Can be built for Windows and Linux.
5+
A small library written in modern C++ for working with a serial port with limited configuration options, designed to work with chips such as ft232, ch340, etc. Can be built for Windows, MacOS or Linux.
66

77
## Table of contents
88
* [Features](#features)
@@ -16,17 +16,12 @@ Supported baudrates: 9600, 19200, 38400, 57600, 115200
1616

1717
## Building
1818

19-
In the example, configuration is done using Cmake. Ninja is used as the default build tool. However, any other build tool can also be used.
19+
In the example, configuration is done using Cmake.
2020

21-
**Configure for Windows:**
21+
**Configure**
2222

2323
```sh
24-
cmake -DTARGET_WINDOWS=ON -Bbuild
25-
```
26-
**Configure for Linux:**
27-
28-
```sh
29-
cmake -DTARGET_LINUX=ON -Bbuild
24+
cmake -Bbuild
3025
```
3126

3227
**Building:**
@@ -36,70 +31,85 @@ cmake --build build
3631

3732
## Examples
3833

39-
**SerialPort:**
40-
34+
Create virtual com pair with Socat:
35+
```sh
36+
socat -d -d pty,raw,echo=0 pty,raw,echo=0
37+
```
38+
Add this project as a library:
39+
```Cmake
40+
add_subdirectory(simple-serial-port)
41+
target_link_libraries (${PROJECT_NAME} simple-serial-port)
42+
```
43+
Example:
4144
```c++
4245
#include <iostream>
46+
#include <string>
47+
#include <system_error>
4348
#include "serial_port.hpp"
4449

45-
int main()
50+
int main(int argc, char* argv[])
4651
{
47-
//create serial port instance
48-
sp::SerialPort serial_port;
49-
//test string to send and read
50-
std::string test = "Hello world!";
51-
//test buffer to read data from port
52-
std::vector<std::uint8_t> buffer;
53-
//open serial port
54-
auto error = serial_port.open("COM1");
55-
if(error.value() == 0)
52+
if(argc != 3)
5653
{
57-
//setup port in case of no errors
58-
//default config : 9600 baudrate, 8 databits, 1 stop bit, no parity, 1s timeout
59-
error = serial_port.setup(sp::PortConfig());
60-
if(error.value() == 0)
61-
{
62-
//write data to port
63-
serial_port.port.writeString(test);
64-
//reading data from port, read size used the same as test string has,
65-
//because we are trying to read the same data in case of TX/RX pin shorting
66-
auto bytes_read = serial_port.port.readBinary(buffer,test.size());
67-
//create string from raw received data
68-
std::string responce(buffer.begin(),buffer.end());
69-
std::cout<<"read from port :"<<responce<<"\n";
70-
//close port
71-
serial_port.port.closePort();
72-
}
54+
std::printf("incorrect arguments list passed, exit...\n");
55+
return 0;
7356
}
74-
return 0;
75-
}
76-
```
77-
78-
**SerialDevice:**
79-
80-
```c++
81-
#include <iostream>
82-
#include "serial_port.hpp"
83-
84-
int main(void)
85-
{
86-
//create instance of serial device
87-
sp::SerialDevice serial_device;
88-
//get list of available serial ports in the system
89-
auto actual_list = serial_device.getListOfAvailableDevices();
90-
if(actual_list.size() > 0)
57+
// strings with port names, by default it will search in /dev/ on Linux and Apple machines
58+
std::string port_1_path = argv[1];
59+
std::string port_2_path = argv[2];
60+
61+
// default port config was created in constructor
62+
sp::PortConfig config;
63+
std::error_code stat;
64+
// port instances
65+
sp::SerialPort test_port_1;
66+
sp::SerialPort test_port_2;
67+
68+
stat = test_port_1.open(port_1_path);
69+
if(stat)
70+
{
71+
std::cout<<"failed to open port " + port_1_path + "\n";
72+
std::cout<<"error: "<<stat.message()<<"\n";
73+
return 0;
74+
}
75+
stat = test_port_1.setup(config);
76+
if(stat)
77+
{
78+
std::cout<<"failed to setup port " + port_1_path + "\n";
79+
std::cout<<"error: "<<stat.message()<<"\n";
80+
return 0;
81+
}
82+
stat = test_port_2.open(port_2_path);
83+
if(stat)
84+
{
85+
std::cout<<"failed to open port " + port_2_path + "\n";
86+
std::cout<<"error: "<<stat.message()<<"\n";
87+
return 0;
88+
}
89+
stat = test_port_2.setup(config);
90+
if(stat)
9191
{
92-
//print available ports
93-
for(auto i = 0; i < actual_list.size(); ++i)
94-
{
95-
std::cout<<" available device : "<<actual_list[i]<<" | index : "<<i<<"\n";
96-
}
92+
std::cout<<"failed to setup port " + port_2_path + "\n";
93+
std::cout<<"error: "<<stat.message()<<"\n";
94+
return 0;
9795
}
98-
else
96+
if((test_port_1.getState() == sp::PortState::Open) && (test_port_2.getState() == sp::PortState::Open))
9997
{
100-
std::cout<<"no available devises found, exit..."<<"\n";
98+
// we will sent data to test_port_1 and read it back in test_port_2
99+
std::cout<<"port on path " <<test_port_1.getPath()<<" opened successfully." <<"\n";
100+
std::cout<<"port on path " <<test_port_2.getPath()<<" opened successfully." <<"\n";
101+
std::string data_to_send = "This is a test string with length more than 32 bytes";
102+
std::vector<std::uint8_t> data_to_read;
103+
std::cout<<"DATA SENT :"<<data_to_send<<"\n"<<"\n";
104+
test_port_1.writeString(data_to_send);
105+
auto bytes_read = test_port_2.readBinary(data_to_read,data_to_send.size());
106+
std::cout<<"BYTES READ :"<<bytes_read<<"\n";
107+
std::string received_data(data_to_read.begin(),data_to_read.end());
108+
std::cout<<"DATA READ :"<<received_data<<"\n"<<"\n";
109+
std::cout<<" test finished, exit..." <<"\n";
101110
}
102-
111+
test_port_1.close();
112+
test_port_2.close();
103113
return 0;
104114
}
105115
```

0 commit comments

Comments
 (0)