Subversion Repositories Code-Repo

Rev

Rev 354 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 354 Rev 362
Line 14... Line 14...
14
{
14
{
15
    serialPort->close();
15
    serialPort->close();
16
    delete serialPort;
16
    delete serialPort;
17
}
17
}
18
 
18
 
19
void SerialHelper::Serial_QueryParameters()
19
void SerialHelper::QueryParameters()
20
{
20
{
21
    QList<QSerialPortInfo> portsList = QSerialPortInfo::availablePorts();
21
    QList<QSerialPortInfo> portsList = QSerialPortInfo::availablePorts();
22
    QStringList ports;
22
    QStringList ports;
23
    for (int i = 0; i < portsList.size(); i++) {
23
    for (int i = 0; i < portsList.size(); i++) {
24
        ports.append(portsList[i].portName());
24
        ports.append(portsList[i].portName());
25
    }
25
    }
26
    emit Serial_UpdateParameters(ports, speeds, dataBits, stopBits, parity, flowControl);
26
    emit UpdateParameters(ports, speeds, dataBits, stopBits, parity, flowControl);
27
}
27
}
28
 
28
 
29
void SerialHelper::Serial_Connect(QString port, QString speed, QString dataBits,
29
void SerialHelper::Connect(QString port, QString speed, QString dataBits,
30
                            QString stopBits, QString parity, QString flowControl)
30
                            QString stopBits, QString parity, QString flowControl)
31
{
31
{
32
    if (!connected) {
32
    if (!connected) {
33
        emit UpdateStatus("Connecting to " + port);
33
        emit UpdateStatus("Connecting to " + port);
34
        serialPort = new QSerialPort();
34
        serialPort = new QSerialPort();
35
        connect(serialPort, SIGNAL(readyRead()), this, SLOT(Serial_ProcessIncomingData()));
35
        connect(serialPort, SIGNAL(readyRead()), this, SLOT(ProcessIncomingData()));
36
        connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(Serial_ProcessError(QSerialPort::SerialPortError)));
36
        connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(ProcessError(QSerialPort::SerialPortError)));
37
        serialPort->setPortName(port);
37
        serialPort->setPortName(port);
38
        if (serialPort->open(QIODevice::ReadWrite)) {
38
        if (serialPort->open(QIODevice::ReadWrite)) {
39
            connected = true;
39
            connected = true;
40
            bool ok = false;
40
            bool ok = false;
41
 
41
 
Line 80... Line 80...
80
            serialPort->setDataTerminalReady(1);
80
            serialPort->setDataTerminalReady(1);
81
 
81
 
82
            if (ok) {
82
            if (ok) {
83
                QString status = "Connected (%1 @ %2 D: %3 S: %4 P: %5 FC: %6)";
83
                QString status = "Connected (%1 @ %2 D: %3 S: %4 P: %5 FC: %6)";
84
                emit UpdateStatus(status.arg(port).arg(speed).arg(dataBits).arg(stopBits).arg(parity).arg(flowControl));
84
                emit UpdateStatus(status.arg(port).arg(speed).arg(dataBits).arg(stopBits).arg(parity).arg(flowControl));
85
                emit Serial_Connected();
85
                emit Connected();
86
            } else
86
            } else
87
                Serial_Disconnect();
87
                Disconnect();
88
        }
88
        }
89
    }
89
    }
90
}
90
}
91
 
91
 
92
void SerialHelper::Serial_Disconnect(bool errored)
92
void SerialHelper::Disconnect(bool errored)
93
{
93
{
94
    serialPort->disconnect();
94
    serialPort->disconnect();
95
    serialPort->close();
95
    serialPort->close();
96
    delete serialPort;
96
    delete serialPort;
97
    connected = false;
97
    connected = false;
98
    if (!errored)
98
    if (!errored)
99
        emit UpdateStatus("Disconnected");
99
        emit UpdateStatus("Disconnected");
100
    emit Serial_Disconnected();
100
    emit Disconnected();
101
}
101
}
102
 
102
 
103
void SerialHelper::Serial_TransmitString(QString string)
103
void SerialHelper::TransmitString(QString string)
104
{
104
{
105
    serialPort->write(string.toStdString().c_str());
105
    serialPort->write(string.toStdString().c_str());
106
    serialPort->write(SERIAL_NEWLINE_CHAR);
106
    serialPort->write(SERIAL_NEWLINE_CHAR);
107
}
107
}
108
 
108
 
109
void SerialHelper::Serial_TransmitByteArray(QByteArray data)
109
void SerialHelper::TransmitByteArray(QByteArray data)
110
{
110
{
111
    serialPort->write(data);
111
    serialPort->write(data);
112
}
112
}
113
 
113
 
114
void SerialHelper::Serial_ProcessIncomingData()
114
void SerialHelper::ProcessIncomingData()
115
{
115
{
116
    char tmpBuffer[SERIAL_BUFFER_SIZE];
116
    char tmpBuffer[SERIAL_BUFFER_SIZE];
117
    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
117
    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
118
    while (len > 0) {
118
    while (len > 0) {
119
        for (int i = 0; i < len; i++) {
119
        QByteArray data(tmpBuffer, len);
120
            emit Serial_ReceivedByte(tmpBuffer[i]);
120
        emit ReceivedByte(data);
121
        }
-
 
122
        // Check if there is more data to be read from the serial port
121
        // Check if there is more data to be read from the serial port
123
        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
122
        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
124
    }
123
    }
125
}
124
}
126
 
125
 
127
void SerialHelper::Serial_ProcessError(QSerialPort::SerialPortError error)
126
void SerialHelper::ProcessError(QSerialPort::SerialPortError error)
128
{
127
{
129
    if (error == QSerialPort::NoError) return;
128
    if (error == QSerialPort::NoError) return;
130
 
129
 
131
    switch(error) {
130
    switch(error) {
132
        case QSerialPort::DeviceNotFoundError:
131
        case QSerialPort::DeviceNotFoundError:
Line 171... Line 170...
171
        default:
170
        default:
172
            break;
171
            break;
173
    }
172
    }
174
 
173
 
175
    serialPort->clearError();
174
    serialPort->clearError();
176
    Serial_Disconnect(true);
175
    Disconnect(true);
177
}
176
}