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
 
19
void SerialHelper::Serial_QueryParameters()
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
    }
26
    emit Serial_UpdateParameters(ports, speeds, dataBits, stopBits, parity, flowControl);
27
}
28
 
29
void SerialHelper::Serial_Connect(QString port, QString speed, QString dataBits,
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();
35
        connect(serialPort, SIGNAL(readyRead()), this, SLOT(Serial_ProcessIncomingData()));
36
        connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(Serial_ProcessError(QSerialPort::SerialPortError)));
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));
85
                emit Serial_Connected();
86
            } else
87
                Serial_Disconnect();
352 Kevin 88
        }
89
    }
90
}
91
 
353 Kevin 92
void SerialHelper::Serial_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");
352 Kevin 100
    emit Serial_Disconnected();
101
}
102
 
103
void SerialHelper::Serial_TransmitString(QString string)
104
{
105
    serialPort->write(string.toStdString().c_str());
106
    serialPort->write(SERIAL_NEWLINE_CHAR);
107
}
108
 
354 Kevin 109
void SerialHelper::Serial_TransmitByteArray(QByteArray data)
352 Kevin 110
{
354 Kevin 111
    serialPort->write(data);
352 Kevin 112
}
113
 
114
void SerialHelper::Serial_ProcessIncomingData()
115
{
116
    char tmpBuffer[SERIAL_BUFFER_SIZE];
117
    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
118
    while (len > 0) {
119
        for (int i = 0; i < len; i++) {
120
            emit Serial_ReceivedByte(tmpBuffer[i]);
121
        }
122
        // Check if there is more data to be read from the serial port
123
        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
124
    }
125
}
126
 
127
void SerialHelper::Serial_ProcessError(QSerialPort::SerialPortError error)
128
{
129
    if (error == QSerialPort::NoError) return;
130
 
131
    switch(error) {
132
        case QSerialPort::DeviceNotFoundError:
354 Kevin 133
            emit UpdateStatus("(Error) Device not found");
352 Kevin 134
            return;
135
        case QSerialPort::PermissionError:
354 Kevin 136
            emit UpdateStatus("(Error) Device already opened or lacking permission");
352 Kevin 137
            return;
138
        case QSerialPort::OpenError:
354 Kevin 139
            emit UpdateStatus("(Error) Device already opened");
352 Kevin 140
            return;
141
        case QSerialPort::NotOpenError:
354 Kevin 142
            emit UpdateStatus("(Error) Device not opened");
352 Kevin 143
            return;
144
        case QSerialPort::ParityError:
354 Kevin 145
            emit UpdateStatus("(Error) Parity error detected");
352 Kevin 146
            break;
147
        case QSerialPort::FramingError:
354 Kevin 148
            emit UpdateStatus("(Error) Framing error detected");
352 Kevin 149
            break;
150
        case QSerialPort::BreakConditionError:
354 Kevin 151
            emit UpdateStatus("(Error) Break condition detected");
352 Kevin 152
            break;
153
        case QSerialPort::WriteError:
354 Kevin 154
            emit UpdateStatus("(Error) Unable to write to device");
352 Kevin 155
            break;
156
        case QSerialPort::ReadError:
354 Kevin 157
            emit UpdateStatus("(Error) Unable to read from device");
352 Kevin 158
            break;
159
        case QSerialPort::ResourceError:
354 Kevin 160
            emit UpdateStatus("(Error) Device missing or disconnected");
352 Kevin 161
            break;
162
        case QSerialPort::UnsupportedOperationError:
354 Kevin 163
            emit UpdateStatus("(Error) Operation not supported or prohibited");
352 Kevin 164
            break;
165
        case QSerialPort::TimeoutError:
354 Kevin 166
            emit UpdateStatus("(Error) Connection timeout");
352 Kevin 167
            break;
168
        case QSerialPort::UnknownError:
354 Kevin 169
            emit UpdateStatus("(Error) Unknown error");
352 Kevin 170
            break;
171
        default:
172
            break;
173
    }
174
 
175
    serialPort->clearError();
353 Kevin 176
    Serial_Disconnect(true);
352 Kevin 177
}