-
Notifications
You must be signed in to change notification settings - Fork 2
/
serialdevice.h
106 lines (90 loc) · 2.37 KB
/
serialdevice.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (C) 2023 Debayan Sutradhar (rnayabed) ([email protected])
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#ifndef SERIALDEVICE_H
#define SERIALDEVICE_H
#include <filesystem>
#include <iostream>
#include <fstream>
#include <cstring>
#include <errno.h>
#include <chrono>
#include <thread>
#include <vector>
#include <array>
#include <cmath>
#include "device.h"
#ifdef __linux
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#elif __WIN32
#define UNICODE
#include <Windows.h>
#include <stdio.h>
#include <string.h>
#elif __APPLE__
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <unistd.h>
#endif
class SerialDevice : public Device
{
public:
enum Error
{
NONE,
FAILED_TO_OPEN_DEVICE,
FAILED_TO_GET_FD_ATTRS,
FAILED_TO_SET_FD_ATTRS,
INVALID_BAUD_RATE,
NOT_SUPPORTED,
READ_FAILED,
WRITE_FAILED,
DEVICE_NOT_OPEN
};
struct DeviceProperties
{
bool parity = false;
int32_t stopBits = -1;
bool rtsCts = false;
int32_t bits = -1;
int32_t baudRate = -1;
};
constexpr static DeviceProperties ARIES{false, 1, false, 8, 115200};
SerialDevice(const std::filesystem::path& devicePath,
const DeviceProperties& deviceProperties,
const int32_t& readTimeout);
const Error& error();
std::string errorStr();
bool read(std::span<unsigned char> bytes);
bool write(std::span<const unsigned char> bytes);
bool open();
bool close();
private:
Error m_error;
const std::filesystem::path& m_devicePath;
const DeviceProperties& m_deviceProperties;
int32_t m_readTimeout;
#ifdef __linux
int32_t m_linuxFD = -1;
#elif __WIN32
HANDLE m_winHandle = NULL;
#elif __APPLE__
int32_t m_macFD = -1;
#endif
bool openLinux();
bool closeLinux();
};
#endif // SERIALDEVICE_H