Skip to content

Commit cd183a0

Browse files
committed
-readme updated
1 parent a6d1875 commit cd183a0

File tree

4 files changed

+153
-62
lines changed

4 files changed

+153
-62
lines changed

README.md

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
# simple-serial-port
2-
A small library written in 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.
2+
3+
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.
4+
5+
## Table of contents
6+
* [Features](#features)
7+
* [Building](#building)
8+
* [Examples](#examples)
9+
* [License](#license)
10+
11+
## Features
12+
13+
Supported baudrates: 9600, 19200, 38400, 57600, 115200
314

415
## Building
16+
517
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.
618

719
**Configure for Windows:**
@@ -18,4 +30,78 @@ cmake -DTARGET_LINUX=ON -Bbuild
1830
**Building:**
1931
```sh
2032
cmake --build build
21-
```
33+
```
34+
35+
## Examples
36+
37+
**SerialPort:**
38+
39+
```c++
40+
#include <iostream>
41+
#include "serial_port.hpp"
42+
43+
int main()
44+
{
45+
//create serial port instance
46+
sp::SerialPort serial_port;
47+
//test string to send and read
48+
std::string test = "Hello world!";
49+
//test buffer to read data from port
50+
std::vector<std::uint8_t> buffer;
51+
//open serial port
52+
auto error = serial_port.open("COM1");
53+
if(error.value() == 0)
54+
{
55+
//setup port in case of no errors
56+
//default config : 9600 baudrate, 8 databits, 1 stop bit, no parity, 1s timeout
57+
error = serial_port.setup(sp::PortConfig());
58+
if(error.value() == 0)
59+
{
60+
//write data to port
61+
serial_port.port.writeString(test);
62+
//reading data from port, read size used the same as test string has,
63+
//because we are trying to read the same data in case of TX/RX pin shorting
64+
auto bytes_read = serial_port.port.readBinary(buffer,test.size());
65+
//create string from raw received data
66+
std::string responce(buffer.begin(),buffer.end());
67+
std::cout<<"read from port :"<<responce<<"\n";
68+
//close port
69+
serial_port.port.closePort();
70+
}
71+
}
72+
return 0;
73+
}
74+
```
75+
76+
**SerialDevice:**
77+
78+
```c++
79+
#include <iostream>
80+
#include "serial_port.hpp"
81+
82+
int main(void)
83+
{
84+
//create instance of serial device
85+
sp::SerialDevice serial_device;
86+
//get list of available serial ports in the system
87+
auto actual_list = serial_device.getListOfAvailableDevices();
88+
if(actual_list.size() > 0)
89+
{
90+
//print available ports
91+
for(auto i = 0; i < actual_list.size(); ++i)
92+
{
93+
std::cout<<" available device : "<<actual_list[i]<<" | index : "<<i<<"\n";
94+
}
95+
}
96+
else
97+
{
98+
std::cout<<"no available devises found, exit..."<<"\n";
99+
}
100+
101+
return 0;
102+
}
103+
```
104+
105+
## License
106+
107+
[MIT](https://choosealicense.com/licenses/mit/)

examples/read_write.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
int main(void)
1414
{
1515

16-
SerialDevice sys_serial;
16+
sp::SerialDevice sys_serial;
1717
auto actual_list = sys_serial.getListOfAvailableDevices();
1818
if(actual_list.size() > 0)
1919
{
@@ -27,7 +27,7 @@ int main(void)
2727
if((index >= 0) && (index <= actual_list.size() - 1))
2828
{
2929
std::cout<<"selected index : "<<index<<"\n";
30-
SerialPort test(actual_list[index],sp::PortConfig());
30+
sp::SerialPort test(actual_list[index],sp::PortConfig());
3131
if(test.getState() == sp::PortState::Open)
3232
{
3333
std::cout<<"port on path " <<test.getPath()<<" opened succesfully." <<"\n";

inc/serial_port.hpp

Lines changed: 61 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -24,66 +24,69 @@
2424
#error "target platform not defined."
2525
#endif
2626

27-
class SerialPort
27+
namespace sp
2828
{
29-
public:
30-
/// @brief default coustructor
31-
SerialPort() = default;
32-
/// @brief constructor that will open port
33-
/// @param name port name to open
34-
SerialPort(std::string name);
35-
/// @brief constructor that will open port and configure it
36-
/// @param name port name to open
37-
/// @param config port configuration
38-
SerialPort(std::string name, sp::PortConfig config);
39-
/// @brief open port with passed name
40-
/// @param name port name
41-
/// @return error code
42-
std::error_code open(const std::string name);
43-
/// @brief setup port with passed configuration (if port is open)
44-
/// @param config port configuration
45-
/// @return error enum
46-
std::error_code setup(sp::PortConfig config);
47-
/// @brief request for port system path
48-
/// @return actual path
49-
std::string getPath() const {return path;};
50-
/// @brief request for port configuration
51-
/// @return struct with configuration
52-
sp::PortConfig getConfig() const {return config;};
53-
/// @brief request for port state
54-
/// @return actual port state
55-
sp::PortState getState() const {return state;};
56-
#if defined(PLATFORM_LINUX)
57-
SerialPortLinux port;
58-
#elif defined(PLATFORM_WINDOWS)
59-
SerialPortWindows port;
60-
#endif
29+
class SerialPort
30+
{
31+
public:
32+
/// @brief default coustructor
33+
SerialPort() = default;
34+
/// @brief constructor that will open port
35+
/// @param name port name to open
36+
SerialPort(std::string name);
37+
/// @brief constructor that will open port and configure it
38+
/// @param name port name to open
39+
/// @param config port configuration
40+
SerialPort(std::string name, sp::PortConfig config);
41+
/// @brief open port with passed name
42+
/// @param name port name
43+
/// @return error code
44+
std::error_code open(const std::string name);
45+
/// @brief setup port with passed configuration (if port is open)
46+
/// @param config port configuration
47+
/// @return error enum
48+
std::error_code setup(sp::PortConfig config);
49+
/// @brief request for port system path
50+
/// @return actual path
51+
std::string getPath() const {return path;};
52+
/// @brief request for port configuration
53+
/// @return struct with configuration
54+
sp::PortConfig getConfig() const {return config;};
55+
/// @brief request for port state
56+
/// @return actual port state
57+
sp::PortState getState() const {return state;};
58+
#if defined(PLATFORM_LINUX)
59+
SerialPortLinux port;
60+
#elif defined(PLATFORM_WINDOWS)
61+
SerialPortWindows port;
62+
#endif
6163

62-
private:
63-
/// @brief actual port state
64-
sp::PortState state = sp::PortState::Close;
65-
/// @brief actual port path
66-
std::string path = "dev/null";
67-
/// @brief actual port config
68-
sp::PortConfig config = sp::PortConfig();
69-
};
64+
private:
65+
/// @brief actual port state
66+
sp::PortState state = sp::PortState::Close;
67+
/// @brief actual port path
68+
std::string path = "dev/null";
69+
/// @brief actual port config
70+
sp::PortConfig config = sp::PortConfig();
71+
};
7072

71-
class SerialDevice
72-
{
73-
public:
74-
SerialDevice(){updateAvailableDevices();};
75-
/// @brief plaform depended call to update list with serial port devices
76-
void updateAvailableDevices();
77-
/// @brief request for list with serial port devices in system
78-
/// @return referense to a vector with devices
79-
std::vector<std::string>& getListOfAvailableDevices(){return devices;};
80-
/// @brief request for list with serial port devices in system
81-
/// @return vector with devices
82-
std::vector<std::string> getListOfAvailableDevices() const {return devices;};
83-
84-
private:
85-
/// @brief vertor with actual list of available serial ports
86-
std::vector<std::string> devices;
87-
};
73+
class SerialDevice
74+
{
75+
public:
76+
SerialDevice(){updateAvailableDevices();};
77+
/// @brief plaform depended call to update list with serial port devices
78+
void updateAvailableDevices();
79+
/// @brief request for list with serial port devices in system
80+
/// @return referense to a vector with devices
81+
std::vector<std::string>& getListOfAvailableDevices(){return devices;};
82+
/// @brief request for list with serial port devices in system
83+
/// @return vector with devices
84+
std::vector<std::string> getListOfAvailableDevices() const {return devices;};
85+
86+
private:
87+
/// @brief vertor with actual list of available serial ports
88+
std::vector<std::string> devices;
89+
};
90+
}
8891

8992
#endif //SERIAL_PORT_H

src/serial_port.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#error "target platform not defined."
1818
#endif
1919

20+
using namespace sp;
21+
2022
SerialPort::SerialPort(std::string name)
2123
{
2224
(void)open(name);

0 commit comments

Comments
 (0)