-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerial.cpp
More file actions
140 lines (119 loc) · 3.03 KB
/
Copy pathSerial.cpp
File metadata and controls
140 lines (119 loc) · 3.03 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "Serial.hpp"
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include<sstream>
#include <iomanip>
Serial::Serial() : bufferIn{std::stringstream{}} {}
bool Serial::openConn(const std::string &device, int const baud) {
fd = open(device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) {
return false;
}
// This code taken from
// https://stackoverflow.com/questions/27609972/open-a-serial-port-with-arduino-using-c-with-xcode-on-mac
struct termios options;
fcntl(fd, F_SETFL, FNDELAY); // Open the device in nonblocking mode
tcgetattr(fd, &options); // Get the current options of the port
bzero(&options, sizeof(options)); // Clear all the options
speed_t Speed;
switch (baud) // Set the speed (baudRate)
{
case 110:
Speed = B110;
break;
case 300:
Speed = B300;
break;
case 600:
Speed = B600;
break;
case 1200:
Speed = B1200;
break;
case 2400:
Speed = B2400;
break;
case 4800:
Speed = B4800;
break;
case 9600:
Speed = B9600;
break;
case 19200:
Speed = B19200;
break;
case 38400:
Speed = B38400;
break;
case 57600:
Speed = B57600;
break;
case 115200:
Speed = B115200;
break;
default:
return false;
}
cfsetispeed(&options, Speed); // Set the baud rate at 115200 bauds
cfsetospeed(&options, Speed);
options.c_cflag |=
(CLOCAL | CREAD |
CS8); // Configure the device : 8 bits, no parity, no control
options.c_iflag |= (IGNPAR | IGNBRK);
options.c_cc[VTIME] = 0; // Timer unused
options.c_cc[VMIN] = 0; // At least on character before satisfy reading
tcsetattr(fd, TCSANOW, &options); // Activate the settings
return true;
}
void Serial::closeConn() { close(fd); }
std::string Serial::getASCIIData() {
int count;
ioctl(fd, FIONREAD, &count);
char readBytes[count];
if (count > 0) {
int countRead = read(fd, readBytes, count);
if (countRead > 0) {
for (int i = 0; i < countRead; i++) {
bufferIn << readBytes[i];
}
}
}
std::string bufferInTemp = bufferIn.str();
bufferIn.str("");
bufferIn.clear();
return std::move(bufferInTemp);
}
std::string Serial::getHEXData() {
int count;
ioctl(fd, FIONREAD, &count);
char readBytes[count];
if (count > 0) {
int countRead = read(fd, readBytes, count);
if (countRead > 0) {
for (int i = 0; i < countRead; i++) {
bufferIn << std::setfill('0') << std::setw(2) << std::hex <<
(0xff & (unsigned int) readBytes[i]) << std::dec << " ";
}
}
}
std::string bufferInTemp = bufferIn.str();
bufferIn.str("");
bufferIn.clear();
return std::move(bufferInTemp);
}
void Serial::sendData(const std::string& data){
int wlen = write(fd, data.c_str(), data.length());
if (wlen != data.length()) {
}
tcdrain(fd);
}
void Serial::sendDataHex( unsigned char * bytes, int len){
int wlen = write(fd, bytes, len);
if (wlen != len) {
}
tcdrain(fd);
}