-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrxTxThread.cpp
121 lines (106 loc) · 2.86 KB
/
rxTxThread.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
#include "rxTxThread.h"
using namespace std;
void millisleep(int ms)
{
if (ms>0)
{
struct timeval tv;
tv.tv_sec=0;
tv.tv_usec=ms*1000;
select(0, 0, 0, 0, &tv);
}
}
rxTxThread::rxTxThread(QObject *parent) :
QObject(parent)
{
directionTx = false;
serPort = new QSerialPort(this);
initSerialPort();
connect(serPort, SIGNAL(error(QSerialPort::SerialPortError)), this,
SLOT(handleError(QSerialPort::SerialPortError)));
connect(serPort, SIGNAL(readyRead()), this, SLOT(readData()));
//serPort->flush();
//serPort->write("<SET><ID>1</ID><STATE>1</STATE></SET>");
}
rxTxThread::~rxTxThread()
{
turnEquipementOFF(1);
}
void rxTxThread::initSerialPort()
{
serPort->setPortName("/dev/ttyACM0");
// serPort->setPortName("/dev/ttyUSB0");
if (serPort->open(QIODevice::ReadWrite))
{
serPort->setBaudRate(QSerialPort::Baud9600,QSerialPort::AllDirections);
serPort->setDataBits(QSerialPort::Data8);
serPort->setParity(QSerialPort::NoParity);
serPort->setStopBits(QSerialPort::OneStop);
serPort->setFlowControl(QSerialPort::NoFlowControl);
}
else {
serPort->close();
/* QMessageBox::critical(this, tr("Error"),
tr("Can't configure the serial port: %1,\n"
"error code: %2")
.arg("/dev/ttyACM1").arg(serial->error()));
ui->statusBar->showMessage(tr("Open error"));*/
}
}
void rxTxThread::readData()
{
if(directionTx==false)
{
QString str="";
QByteArray rxBytes = serPort->readAll();
rxData.append(rxBytes);
if(rxData.contains("</STN>"))// || rxData.size()>60)
{
//QTextStream(stdout)<<rxLatest<<endl; //debug
rxLatest=rxData;
emit cardDetected();
emit cardRxLatest(rxLatest);
rxData = "";
}
}
}
void rxTxThread::handleError(QSerialPort::SerialPortError error)
{
if (error == QSerialPort::ResourceError) {
//QMessageBox::critical(this, tr("Critical Error"), serial->errorString());
//closeSerialPort();
}
}
void rxTxThread::turnEquipementON(int eqID)
{
directionTx=true;
QByteArray str = "<SET><ID>1</ID><STATE>1</STATE></SET>";
char c0;
int i;
serPort->flush();
millisleep(100);
for(i=0;i<str.size();i++)
{
c0 = str.at(i);
serPort->write(&c0,1);
serPort->waitForBytesWritten(10);
millisleep(10);
}
directionTx=false;
}
void rxTxThread::turnEquipementOFF(int eqID)
{
QByteArray str = "<SET><ID>1</ID><STATE>0</STATE></SET>";
char c0;
int i;
serPort->flush();
millisleep(100);
for(i=0;i<str.size();i++)
{
c0 = str.at(i);
serPort->write(&c0,1);
serPort->waitForBytesWritten(10);
millisleep(10);
}
directionTx=false;
}