-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComReceiver.cpp
49 lines (38 loc) · 1.21 KB
/
ComReceiver.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
#include "ComReceiver.h"
#include "lib-rs232/rs232.h"
ComReceiver::ComReceiver(const char* port) {
const char* portnr = std::string(port).substr(3).c_str();
sscanf(portnr, "%d", &portNumber);
portNumber--;
int baudrate = 9600, flowcntrl = 0;
char mode[] = {'8', 'N', '1' , 0};
if(RS232_OpenComport(portNumber, baudrate, mode, flowcntrl) == 0) {
printf("Opened port %s\n", port);
} else {
printf("Can not open port %s\n", port);
return;
}
portOpened = true;
}
ComReceiver::~ComReceiver() {
if (!portOpened)
return;
RS232_CloseComport(portNumber);
}
void ComReceiver::receiveLoop() {
if (!portOpened)
return;
unsigned char buffer[4096];
int received = RS232_PollComport(portNumber, buffer, 4096);
if (received <= 0)
return;
receiveBuffer.append((const char*)buffer, received);
std::size_t found = receiveBuffer.find_first_of("\r\n");
while (found != std::string::npos) {
std::string code = receiveBuffer.substr(0, found);
if (found > 0)
receivedCodes.push_back(code);
receiveBuffer.erase(0, found+1);
found = receiveBuffer.find_first_of("\r\n", found+1);
}
}