-
Notifications
You must be signed in to change notification settings - Fork 0
/
comm.h
197 lines (170 loc) · 6.58 KB
/
comm.h
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* @file comm.h
* @brief C++ Interface: comm
*
* @details This file holds the Comm class which creates communication channel to microcontroller,
polls events from controller, realizes communication protocol.
*
* Copyright: See COPYING file that comes with this distribution
*
* @author Valdur Kaldvee (C) 2008
* @author Margus Ernits <[email protected]>, (C) 2008
* @author Mauno Pihelgas <[email protected]>, (C) 2010
*/
#ifndef COMM_H
#define COMM_H
#define MAX_DEVICES 16
#include <errno.h>
#include <termios.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <QMutex>
#include <QWaitCondition>
#include "config.h"
#define MSG_MC_DATA_NR_ELEMENTS 16
#define MSG_MC_DATA_HEADER '\a'
#define MSG_MC_DATA_CHECKSUM_DUMMY 1337
/**
* @brief This class creates communication channel to microcontroller, polls events from controller, realizes communication protocol.
* @author Valdur Kaldvee
* @author Margus Ernits
* @author Mauno Pihelgas
* @author Erik Kaju
*/
class Comm{
public:
// Comm();
~Comm();
int fd; ///< File descriptor for the port.
int fds[MAX_DEVICES]; ///< Array of file descriptors for the serial ports.
/**
* @brief Opens the serial device.
* @deprecated This function is deprecated. Use openSerial(const char *, const int) instead.
* @param modemDevice - Serial device address
* @return File descriptor for the port (fd)
*/
int openSerial(const char *);
/**
* @brief Opens the numbered (nr) serial device.
* @param modemDevice - Serial device address (Not in use!)
* @param nr - Serial device number
* @return File descriptor for the port (fd)
*/
int openSerial(const char *, const int);
/**
* @brief Sends commands to the serial device.
* @deprecated This function is deprecated. Use sendCommand(char, int, const int) instead.
* @param addr - ServoBasic address (multiple addresses on one controller)
* @param value - digitalBitMaskValue
* @return 0 | 1 (0 = SUCCESS, 1 = FAILURE)
*/
//TODO viita dokumentatsioonile robo wikis.
int sendCommand(char, int);
/**
* @brief Sends commands to the numbered (nr) serial device.
* @param addr - ServoBasic address (multiple addresses on one controller)
* @param value - digitalBitMaskValue
* @param nr - Serial device number
* @return 0 | 1 (0 = SUCCESS, 1 = FAILURE)
*/
int sendCommand(char, int, const int);
/**
* @brief Sends commands to the numbered (nr) serial device.
* @param addr - ServoBasic address (multiple addresses on one controller)
* @param value - digitalBitMaskValue
* @param nr - Serial device number
* @return 0 | 1 (0 = SUCCESS, 1 = FAILURE)
*/
int sendCommand3(int, int, int, int);
/**
* @brief Reads and returns an integer value from the serial device.
* @deprecated This function is deprecated. Use readSerial(char, const int) instead.
* @param addr - ServoBasic address (multiple addresses on one controller)
* @return Returns an integer value from the serial device.
*/
int readSerial(char);
/**
* @brief Reads and returns an integer value from the numbered (nr) serial device.
* @param addr - ServoBasic address (multiple addresses on one controller)
* @param nr - Serial device number
* @return Returns an integer value from the serial device.
*/
int readSerial(char, const int);
/**
* @brief Reads multiple values from the serial device and saves them in two arrays (analog & digital).
* @deprecated This function is deprecated. Use readSerialMulti(char, int *, int *, const int) instead.
* @param addr - ServoBasic address (multiple addresses on one controller)
* @param microControllerData - pointer to the array of microcontroller values
* @return 0 | 1 (0 = SUCCESS, 1 = FAILURE)
*/
int readSerialMulti(char, int *);
/**
* @brief Reads multiple values from the numbered (nr) serial device and saves them in two arrays (analog & digital).
* @param addr - ServoBasic address (multiple addresses on one controller)
* @param microControllerData - pointer to the array of microcontroller values
* @param nr - Serial device number
* @return 0 | 1 (0 = SUCCESS, 1 = FAILURE)
*/
int readSerialMulti(char, int *, const int);
/**
* @brief Requests a read start for multiple values from the serial device.
* @deprecated This function is deprecated. Use readSerialMulti(char, const int) instead.
* @param addr - ServoBasic address (multiple addresses on one controller)
* @return 0 | 1 (0 = SUCCESS, 1 = FAILURE)
*/
int requestSerialMulti(char);
/**
* @brief Requests a read start for multiple values from the numbered (nr) serial device.
* @param addr - ServoBasic address (multiple addresses on one controller)
* @param analogs - pointer to the array of 'analog' values
* @param digitals - pointer to the array of 'digital' values
* @param nr - Serial device number
* @return 0 | 1 (0 = SUCCESS, 1 = FAILURE)
*/
int requestSerialMulti(char, const int);
/**
* @brief Reads multiple values from the numbered (nr) serial device and saves them in two arrays (analog & digital).
* @param addr - ServoBasic address (multiple addresses on one controller)
* @param analogs - pointer to the array of 'analog' values
* @param digitals - pointer to the array of 'digital' values
* @param nr - Serial device number
* @return 0 | 1 (0 = SUCCESS, 1 = FAILURE)
*/
int serialMultiResponse(int *, int);
static Comm & getComm()
{
static Comm comm;
return comm;
}
protected:
Config * conf;
private:
QMutex mutex; //TODO Erinevatel serialitel võiks olla erinev "mutex", et saaks samaaegselt erinevatesse serialitesse kirjutada / lugeda.
QWaitCondition isLocked;
Comm();
Comm(Comm const&);
Comm& operator=(Comm const&);
uint8_t fletcherChecksum(int *, int);
/**
* @brief Closes and tries to reopen the serial device.
* @deprecated This function is deprecated. Use reopen(const char *, const int) instead.
* @param device - Serial device address
*/
inline void reopen(const char *);
/**
* @brief Closes and tries to reopen the numbered (nr) serial device.
* @param device - Serial device address
* @param nr - Serial device number
*/
inline void reopen(const char *, const int);
int len;
char buf[255];
};
#endif