-
Notifications
You must be signed in to change notification settings - Fork 1
/
usbworker.cpp
130 lines (105 loc) · 3.09 KB
/
usbworker.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
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
#include "usbworker.h"
#include "datmessage.h"
#include "supmessage.h"
#include <fcntl.h>
#include <iostream>
#include <termios.h>
#include <unistd.h>
UsbWorker::UsbWorker(QObject *parent)
: QThread(parent)
{
}
UsbWorker::~UsbWorker()
{
if(this->fd != -1) {
close(this->fd);
this->fd = -1;
}
wait();
}
void UsbWorker::run()
{
while(true) {
if(connect == true) {
if(fd == -1) {
initPort();
}
if(fd != -1) {
emit sendConnected(true);
int len = readMessage();
if(strncmp(this->buf,"CM2024 SUP", 10)==0) {
SupMessage sup = SupMessage(buf+10, len-10);
emit sendSup(sup);
} else if (strncmp(this->buf,"CM2024 DAT", 10)==0) {
DatMessage dat = DatMessage(buf+10, len-10);
emit sendDat(dat);
} else {
std::cout << "tf is this" << std::endl;
}
}
} else {
if(fd != -1) {
close(fd);
fd = -1;
emit sendConnected(false);
}
QThread::sleep(1);
}
}
}
void UsbWorker::initPort() {
this->fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if(this->fd == -1) {
std::cerr << "Error opening /dev/ttyUSB0. Check permissions and lsof /dev/ttyUSB0." << std::endl;
return;
} else {
struct termios tty;
memset (&tty, 0, sizeof tty);
if( tcgetattr(this->fd, &tty) != 0 ) {
std::cout << "Error " << errno << " from tcgetattr: " << strerror(errno) << std::endl;
return;
}
// set baud rate to 57600
cfsetospeed (&tty, (speed_t)B57600);
cfsetispeed (&tty, (speed_t)B57600);
// might need this stuff
/*
tty.c_cflag &= ~PARENB; // Make 8n1
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CSIZE;
tty.c_cflag |= CS8;
tty.c_cflag &= ~CRTSCTS; // no flow control
tty.c_cc[VMIN] = 1; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
tty.c_cflag |= CREAD | CLOCAL; // turn on READ & ignore ctrl lines
*/
// make raw
cfmakeraw(&tty);
// write attributes
tcflush( this->fd, TCIFLUSH );
if ( tcsetattr ( this->fd, TCSANOW, &tty ) != 0) {
std::cout << "Error " << errno << " from tcsetattr" << std::endl;
return;
}
emit sendConnected(true);
}
}
int UsbWorker::readMessage() {
unsigned char ch = 0;
unsigned char prevch = 0;
int n = 0;
int len = 0;
memset(this->buf, '\0', sizeof this->buf);
do {
prevch = ch;
n = read(this->fd, &ch, 1);
this->buf[len] = ch;
len += n;
} while((prevch!='\r' || ch!='\n') && len<=47);
return len;
}
void UsbWorker::toggleConnect() {
mutex.lock();
connect = !connect;
mutex.unlock();
}