-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUART.cpp
More file actions
61 lines (48 loc) · 1.15 KB
/
UART.cpp
File metadata and controls
61 lines (48 loc) · 1.15 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
58
59
60
61
/*
* UART.cpp
*
* Created on: Mar 12, 2016
* Author: Christopher Jordan-Denny
*/
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "UART.h"
HardwareSerial::HardwareSerial(std::string id, unsigned long baud) {
this->device = id;
struct termios uart;
fd = open(this->device.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd < 0) {
exit(-1);
}
bzero(&uart, sizeof(uart));
uart.c_cflag = CS8 | CLOCAL | CREAD;
uart.c_iflag = IGNPAR;
uart.c_oflag = 0;
uart.c_lflag = ICANON; //Canonical mode, set to 0 if non-canonical desired
uart.c_ispeed = baud;
uart.c_ospeed = baud;
// uart.c_cc[VTIME] = 0;
// uart.c_cc[VMIN] = 1;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &uart);
}
std::string HardwareSerial::readData() {
int numRead = 0;
if((numRead = read(fd, buff, bufferSize)) < 0){
return std::string();
}
buff[numRead] = '\0';
std::string str(buff);
return str;
}
size_t HardwareSerial::writeCommand(std::string command){
return write(fd,command.c_str(),command.length());
}