-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArduinoDriver.cpp
More file actions
70 lines (57 loc) · 1.34 KB
/
ArduinoDriver.cpp
File metadata and controls
70 lines (57 loc) · 1.34 KB
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
//
// ArduinoDriver.cpp
//
// Created by roy_shilkrot on 8/28/13.
//
//
#include "ArduinoDriver.h"
#include <unistd.h>
#include <iostream>
#include <sstream>
#ifndef SSTR
#define SSTR( x ) dynamic_cast< std::ostringstream & >( \
( std::ostringstream() << x ) ).str()
#endif
ArduinoDriver::~ArduinoDriver() {
quit();
wait();
}
void ArduinoDriver::run() {
QextSerialPort port;
thread_port = &port;
port.setPortName(QString(m_portname.c_str()));
port.setBaudRate(BAUD9600);
connect(&port, SIGNAL(readyRead()), this, SLOT(onDataAvailable()));
if(!port.open(QIODevice::ReadWrite)) {
qDebug() << "cannot connect to " << port.portName();
return;
}
arduinoSetup = true;
exec();
//thread ended, cleanup
if (port.lineStatus() != 0) {
qDebug() << "closing port";
port.close();
}
}
void ArduinoDriver::onDataAvailable() {
QByteArray ba = thread_port->readAll();
qDebug() << ba.constData();
}
void ArduinoDriver::connectSerial(const std::string& portname) {
if(isRunning()) {
qDebug() << "thread is running. stopping...";
quit();
wait();
qDebug() << "stopped";
}
m_portname = portname;
start();
}
void ArduinoDriver::sendByte(uchar val) {
if(isSetup()) {
qDebug() << "Sending " << (int)val;
thread_port->write((char*)&val, 1);
thread_port->waitForBytesWritten(5);
}
}