-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial.cpp
More file actions
57 lines (48 loc) · 1.26 KB
/
Serial.cpp
File metadata and controls
57 lines (48 loc) · 1.26 KB
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
#include "Serial.h"
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <iostream>
#include <bitset>
void Serial::send(std::bitset<16> data) {
auto instr_bin = uint16_t(data.to_ulong());
send(instr_bin);
}
void Serial::send(uint16_t data) {
auto upper = uint8_t(data >> 8u);
write(fd, &data, 1);
write(fd, &upper, 1);
}
void Serial::send_byte(uint8_t byte) {
write(fd, &byte, 1);
}
uint8_t Serial::receive() {
uint8_t buff;
read(fd, &buff, 1);
return buff;
}
Serial::Serial(const std::string &port) {
fd = open(port.c_str(), O_RDWR);
if(fd == -1) std::cerr << port << " unavailable" << std::endl;
termios options{};
tcgetattr(fd, &options);
// Set baud rate to 115200
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);
// Enable receiver and set local mode
options.c_cflag |= (CLOCAL | CREAD);
// 8 bits, no parity
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Raw input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_iflag &= ~(ICRNL | IGNCR | ICRNL | IGNBRK | BRKINT | PARMRK);
// No software flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
// Raw output
options.c_oflag &= ~OPOST;
tcsetattr(fd,TCSANOW,&options);
}