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