Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
352 Kevin 1
#include "SerialHelper.h"
2
 
3
SerialHelper::SerialHelper(QObject *parent) : QObject(parent)
4
{
5
    connected = false;
6
    speeds << "9600" << "19200" << "38400" << "57600" << "115200" << "230400" << "250000";
7
    dataBits << "8 Bits" << "7 Bits" << "6 Bits" << "5 Bits";
8
    stopBits << "1 Bit" << "1.5 Bits" << "2 Bits";
9
    parity << "None" << "Even" << "Odd" << "Space" << "Mark";
10
    flowControl << "None" << "Hardware" << "Software";
363 Kevin 11
 
12
    serialPort = NULL;
352 Kevin 13
}
14
 
15
SerialHelper::~SerialHelper()
16
{
363 Kevin 17
    if (serialPort != NULL && serialPort->isOpen())
18
        serialPort->close();
352 Kevin 19
    delete serialPort;
20
}
21
 
362 Kevin 22
void SerialHelper::QueryParameters()
352 Kevin 23
{
24
    QList<QSerialPortInfo> portsList = QSerialPortInfo::availablePorts();
25
    QStringList ports;
26
    for (int i = 0; i < portsList.size(); i++) {
27
        ports.append(portsList[i].portName());
28
    }
362 Kevin 29
    emit UpdateParameters(ports, speeds, dataBits, stopBits, parity, flowControl);
352 Kevin 30
}
31
 
362 Kevin 32
void SerialHelper::Connect(QString port, QString speed, QString dataBits,
352 Kevin 33
                            QString stopBits, QString parity, QString flowControl)
34
{
35
    if (!connected) {
354 Kevin 36
        emit UpdateStatus("Connecting to " + port);
352 Kevin 37
        serialPort = new QSerialPort();
362 Kevin 38
        connect(serialPort, SIGNAL(readyRead()), this, SLOT(ProcessIncomingData()));
39
        connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(ProcessError(QSerialPort::SerialPortError)));
352 Kevin 40
        serialPort->setPortName(port);
41
        if (serialPort->open(QIODevice::ReadWrite)) {
42
            connected = true;
43
            bool ok = false;
44
 
45
            int baud = speed.toInt(&ok);
46
            if (ok) serialPort->setBaudRate(baud);
47
 
48
            if (dataBits == "5 Bits")
49
                serialPort->setDataBits(QSerialPort::Data5);
50
            else if (dataBits == "6 Bits")
51
                serialPort->setDataBits(QSerialPort::Data6);
52
            else if (dataBits == "7 Bits")
53
                serialPort->setDataBits(QSerialPort::Data7);
54
            else
55
                serialPort->setDataBits(QSerialPort::Data8);
56
 
57
            if (stopBits == "1.5 Bits")
58
                serialPort->setStopBits(QSerialPort::OneAndHalfStop);
59
            else if (stopBits == "2 Bits")
60
                serialPort->setStopBits(QSerialPort::TwoStop);
61
            else
62
                serialPort->setStopBits(QSerialPort::OneStop);
63
 
64
            if (parity == "Even")
65
                serialPort->setParity(QSerialPort::EvenParity);
66
            else if (parity == "Odd")
67
                serialPort->setParity(QSerialPort::OddParity);
68
            else if (parity == "Space")
69
                serialPort->setParity(QSerialPort::SpaceParity);
70
            else if (parity == "Mark")
71
                serialPort->setParity(QSerialPort::MarkParity);
72
            else
73
                serialPort->setParity(QSerialPort::NoParity);
74
 
75
            if (flowControl == "Hardware")
76
                serialPort->setFlowControl(QSerialPort::HardwareControl);
77
            else if (flowControl == "Software")
78
                serialPort->setFlowControl(QSerialPort::SoftwareControl);
79
            else
80
                serialPort->setFlowControl(QSerialPort::NoFlowControl);
81
 
82
            // See http://umforum.ultimaker.com/index.php?/topic/5886-um2-controller-resetreboot-when-opening-usb-port-on-linux/
83
            serialPort->setDataTerminalReady(1);
84
 
353 Kevin 85
            if (ok) {
354 Kevin 86
                QString status = "Connected (%1 @ %2 D: %3 S: %4 P: %5 FC: %6)";
353 Kevin 87
                emit UpdateStatus(status.arg(port).arg(speed).arg(dataBits).arg(stopBits).arg(parity).arg(flowControl));
364 Kevin 88
                emit Connected(true);
353 Kevin 89
            } else
362 Kevin 90
                Disconnect();
352 Kevin 91
        }
92
    }
93
}
94
 
362 Kevin 95
void SerialHelper::Disconnect(bool errored)
352 Kevin 96
{
97
    serialPort->disconnect();
98
    serialPort->close();
99
    delete serialPort;
100
    connected = false;
353 Kevin 101
    if (!errored)
354 Kevin 102
        emit UpdateStatus("Disconnected");
364 Kevin 103
    emit Connected(false);
352 Kevin 104
}
105
 
362 Kevin 106
void SerialHelper::TransmitString(QString string)
352 Kevin 107
{
108
    serialPort->write(string.toStdString().c_str());
109
    serialPort->write(SERIAL_NEWLINE_CHAR);
110
}
111
 
362 Kevin 112
void SerialHelper::TransmitByteArray(QByteArray data)
352 Kevin 113
{
354 Kevin 114
    serialPort->write(data);
352 Kevin 115
}
116
 
362 Kevin 117
void SerialHelper::ProcessIncomingData()
352 Kevin 118
{
119
    char tmpBuffer[SERIAL_BUFFER_SIZE];
120
    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
121
    while (len > 0) {
362 Kevin 122
        QByteArray data(tmpBuffer, len);
123
        emit ReceivedByte(data);
352 Kevin 124
        // Check if there is more data to be read from the serial port
125
        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
126
    }
127
}
128
 
362 Kevin 129
void SerialHelper::ProcessError(QSerialPort::SerialPortError error)
352 Kevin 130
{
131
    if (error == QSerialPort::NoError) return;
132
 
133
    switch(error) {
134
        case QSerialPort::DeviceNotFoundError:
354 Kevin 135
            emit UpdateStatus("(Error) Device not found");
352 Kevin 136
            return;
137
        case QSerialPort::PermissionError:
354 Kevin 138
            emit UpdateStatus("(Error) Device already opened or lacking permission");
352 Kevin 139
            return;
140
        case QSerialPort::OpenError:
354 Kevin 141
            emit UpdateStatus("(Error) Device already opened");
352 Kevin 142
            return;
143
        case QSerialPort::NotOpenError:
354 Kevin 144
            emit UpdateStatus("(Error) Device not opened");
352 Kevin 145
            return;
146
        case QSerialPort::ParityError:
354 Kevin 147
            emit UpdateStatus("(Error) Parity error detected");
352 Kevin 148
            break;
149
        case QSerialPort::FramingError:
354 Kevin 150
            emit UpdateStatus("(Error) Framing error detected");
352 Kevin 151
            break;
152
        case QSerialPort::BreakConditionError:
354 Kevin 153
            emit UpdateStatus("(Error) Break condition detected");
352 Kevin 154
            break;
155
        case QSerialPort::WriteError:
354 Kevin 156
            emit UpdateStatus("(Error) Unable to write to device");
352 Kevin 157
            break;
158
        case QSerialPort::ReadError:
354 Kevin 159
            emit UpdateStatus("(Error) Unable to read from device");
352 Kevin 160
            break;
161
        case QSerialPort::ResourceError:
354 Kevin 162
            emit UpdateStatus("(Error) Device missing or disconnected");
352 Kevin 163
            break;
164
        case QSerialPort::UnsupportedOperationError:
354 Kevin 165
            emit UpdateStatus("(Error) Operation not supported or prohibited");
352 Kevin 166
            break;
167
        case QSerialPort::TimeoutError:
354 Kevin 168
            emit UpdateStatus("(Error) Connection timeout");
352 Kevin 169
            break;
170
        case QSerialPort::UnknownError:
354 Kevin 171
            emit UpdateStatus("(Error) Unknown error");
352 Kevin 172
            break;
173
        default:
174
            break;
175
    }
176
 
177
    serialPort->clearError();
362 Kevin 178
    Disconnect(true);
352 Kevin 179
}