-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (39 loc) · 1.02 KB
/
main.cpp
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
#include <iostream>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <cstring>
#include <fstream>
using namespace std;
int main() {
int usb = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (usb < 0) {
cerr << "Unable to open /dev/ttyUSB0" << endl;
return 1;
}
termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(usb, &tty) != 0) {
cerr << "Error setting termios attributes" << endl;
return 1;
}
tty.c_iflag = IGNCR;
tty.c_cflag = CS8;
tty.c_lflag = ICANON;
cfsetospeed(&tty, B921600);
cfsetispeed(&tty, B921600);
tcsetattr(usb, TCSANOW, &tty);
ofstream file;
file.open("../log", ios_base::out);
char buffer[100] = { '\0' };
int bytes_read;
// read first incomplete line
while (read(usb, buffer, 100) <= 0);
while (true) {
if ( (bytes_read = read(usb, buffer, 100)) > 0) {
buffer[bytes_read] = '\0';
file << buffer;
}
}
return 0;
}