-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitor.cpp
More file actions
80 lines (63 loc) · 1.84 KB
/
Monitor.cpp
File metadata and controls
80 lines (63 loc) · 1.84 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
//
// Created by eele on 9-6-2015.
//
#include <iostream>
#include <unistd.h>
#include <thread>
#include "Monitor.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <string.h>
#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyACM0"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
void Monitor::listen(SensorData &sensorData)
{
bool stop=false;
int fd,c, len;
struct termios oldtio,newtio;
char buf[2048];
len = 0;
std::string output="";
fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );
if (fd <0)
{
perror(MODEMDEVICE); return listen(sensorData);
}
tcgetattr(fd,&oldtio); /* save current port settings */
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 0; /* inter-character timer unused */
tcflush(fd, TCIFLUSH);
tcsetattr(fd,TCSANOW,&newtio);
while (true) { /* loop for input */
len = read(fd, buf, sizeof(buf)); /* returns after 5 chars have been input */
buf[len] = 0;
output += buf;
std::string fix = "";
int finalCommandIndex = 0;
for(int i = 0; i < output.length(); i++)
{
if(output[i] == '<')
finalCommandIndex = i;
}
fix = output.substr(finalCommandIndex, output.length() - finalCommandIndex);
// std::cout << buf << std::endl;
// std::cout << "Nieuw" << std::endl;
// std::cout << fix << std::endl;
sensorData.set(fix);
usleep(1000000);
output = "";
if (fd <0)
{
perror(MODEMDEVICE); return listen(sensorData);
}
}
}