-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSerialCommunication.cpp
70 lines (53 loc) · 1.33 KB
/
SerialCommunication.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
* File: SerialCommunication.cpp
* Author: Jan Dufek
*
* Created on May 2, 2017, 1:18 PM
*/
#include "SerialCommunication.hpp"
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
SerialCommunication::SerialCommunication(FotokiteState * fotokiteState, const char * serialPort) : Communication(fotokiteState) {
// Setup serial communication
serial.open(serialPort, 115200);
// Initialize Fotokite remote control mode
this->startRemoteControl();
// Start listener
this->startListener();
}
SerialCommunication::SerialCommunication(const SerialCommunication& orig) : Communication(orig) {
}
SerialCommunication::~SerialCommunication() {
close_connection();
}
/**
* Send message to Fotokite.
*
* @param message
*/
void SerialCommunication::send(string message) {
this->serial.print(message);
usleep(1000);
}
/**
* Receive message from Fotokite.
*
* @return
*/
string SerialCommunication::receive() {
return this->serial.readBytesUntil('\n', 1024);
}
/**
* Close connection with Fotokite.
*/
void SerialCommunication::close_connection() {
// Stop listening
listening = false;
// Join with listener
listener.join();
// Sleep to make sure the message gets accepted
usleep(500000);
// Stop remote control mode
send("stop\n");
}