-
Notifications
You must be signed in to change notification settings - Fork 0
/
serial.hpp
42 lines (40 loc) · 987 Bytes
/
serial.hpp
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
#include <string>
#include <exception>
#include <stdexcept>
#include <algorithm>
void msleep(unsigned msec);
class serial {
public:
serial(const std::string &name, int speed = -1);
~serial();
void set_speed(int speed);
bool readable();
serial &read(void *buf, std::streamsize n);
template <typename T>
T read_value() {
T v;
read_value(v);
return v;
}
template <typename T>
serial &read_value(T &v) { return read(&v, sizeof(T)); }
serial &write(const void *buf, std::streamsize n);
template <typename T>
serial &write_value(const T &v) {
return write(&v, sizeof(T));
}
template <typename T>
static T hton(volatile T v) {
std::reverse((unsigned char *)(&v), (unsigned char *)(&v+1));
return v;
}
template <typename T>
static T ntoh(volatile T v) {
std::reverse((unsigned char *)(&v), (unsigned char *)(&v+1));
return v;
}
private:
serial(const serial &);
serial &operator =(const serial &);
void *data;
};