SBB Host
Host communication with the self balancing bike.
Loading...
Searching...
No Matches
Serial.h
Go to the documentation of this file.
1
2#ifndef SERIAL_H
3#define SERIAL_H
4
5#define FIFO_BUF_SIZE 262144 //FIFO buffer size (default)
6
7#if defined(WIN32) || defined(_WIN32) || defined(__WIN32) || defined(__WIN32__) || defined(WIN64) || defined(_WIN64) || defined(__WIN64) || defined(__WIN64__)
8#ifndef OS_WIN
9#define OS_WIN
10#endif
11#elif defined(linux) || defined(__linux) || defined(__linux__) || defined(__GNUC__) || defined(__GNUG__) || defined(unix) || defined(__unix) || defined(__unix__)
12#ifndef OS_LINUX
13#define OS_LINUX
14#endif
15#else
16#error OS not supported
17#endif
18
19#ifdef OS_WIN
20#define _WINSOCKAPI_
21#include <windows.h>
22#define PORT_STR "\\\\.\\COM%u"
23#else
24#include <fcntl.h> // Contains file controls like O_RDWR
25#include <termios.h> // Contains POSIX terminal control definitions
26#include <unistd.h> // write(), read(), close()
27#define sprintf_s(buf, len, ...) snprintf((buf), (len), __VA_ARGS__)
28#define PORT_STR "/dev/ttyACM%u"
29#endif
30
31//common include (both Win and Linux)
32#include <stdio.h>
33#include <vector>
34
41class Serial {
42 private:
43 #ifdef OS_WIN
44 HANDLE _serialPort;
45 DCB _serialParams = { 0 };
46 COMMTIMEOUTS _serialTimeout = { 0 };
47 #else
49 struct termios _serialParams;
50 #endif
51 bool _isInit = false;
52 unsigned int _timeout = TIMEOUT;
53 public:
54 static constexpr unsigned int TIMEOUT = 10;
55
60
65
71 static std::vector<int> get_availableSerialPorts(unsigned int maxPortNumber); //static to be available for all
72
80 bool begin(unsigned int port, unsigned int baud, unsigned int timeout);
81
88 bool begin(unsigned int port, unsigned int baud);
89
93 void flush();
94
99 bool end();
100
106 bool write(unsigned char c);
107
114 size_t write(unsigned char* buf, size_t len);
115
120 unsigned char read();
121
128 size_t read(unsigned char* buf, size_t len);
129
134 void setTimeout(unsigned int timeout);
135
140 unsigned int getTimeout();
141
146 operator bool() { return _isInit; }
147};
148#endif
A class for serial communication.
Definition: Serial.h:41
size_t read(unsigned char *buf, size_t len)
Read a buffer.
bool begin(unsigned int port, unsigned int baud, unsigned int timeout)
Begin the serial.
void setTimeout(unsigned int timeout)
Set the timeout.
int _serialPort
serial port
Definition: Serial.h:48
static std::vector< int > get_availableSerialPorts(unsigned int maxPortNumber)
Get the available serial ports.
bool end()
Close the serial.
Serial()
Constructor.
size_t write(unsigned char *buf, size_t len)
Send a buffer.
~Serial()
Desctructor.
bool begin(unsigned int port, unsigned int baud)
Begin the serial.
void flush()
Flush the serial.
static constexpr unsigned int TIMEOUT
Default timeout.
Definition: Serial.h:54
unsigned int _timeout
timeout of the serial port
Definition: Serial.h:52
unsigned char read()
Read a byte.
bool _isInit
true if serial has been initialized
Definition: Serial.h:51
struct termios _serialParams
serial param struct
Definition: Serial.h:49
bool write(unsigned char c)
Send a byte.
unsigned int getTimeout()
Get the timeout.