Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
344 Kevin 1
#include "SerialController.h"
2
 
3
SerialController::SerialController(QObject *parent) : QObject(parent)
4
{
5
    connected = false;
6
    speeds << "9600" << "19200" << "38400" << "57600" << "115200" << "230400" << "250000";
7
    bufferInIndex = 0;
8
    bufferInOverflow = false;
9
}
10
 
11
SerialController::~SerialController()
12
{
13
    serialPort->close();
14
    delete serialPort;
15
}
16
 
17
void SerialController::Serial_QueryParameters()
18
{
19
    QList<QSerialPortInfo> portsList = QSerialPortInfo::availablePorts();
20
    QStringList ports;
21
    for (int i = 0; i < portsList.size(); i++) {
22
        ports.append(portsList[i].portName());
23
    }
24
    emit Serial_UpdateParameters(ports, speeds);
25
}
26
 
27
void SerialController::Serial_Connect(QString port, QString speed)
28
{
29
    if (!connected) {
30
        serialPort = new QSerialPort();
31
        connect(serialPort, SIGNAL(readyRead()), this, SLOT(Serial_ProcessIncomingData()));
32
        serialPort->setPortName(port);
33
        if (!serialPort->open(QIODevice::ReadWrite)) {
34
            Serial_Disconnect();
35
        } else {
36
            connected = true;
346 Kevin 37
            bool ok = false;
344 Kevin 38
 
346 Kevin 39
            int baud = speed.toInt(&ok);
40
            if (ok) serialPort->setBaudRate(baud);
41
 
344 Kevin 42
            serialPort->setDataBits(QSerialPort::Data8);
43
            serialPort->setParity(QSerialPort::NoParity);
345 Kevin 44
//            serialPort->setFlowControl(QSerialPort::HardwareControl);
344 Kevin 45
            serialPort->setStopBits(QSerialPort::OneStop);
46
 
47
            // See http://umforum.ultimaker.com/index.php?/topic/5886-um2-controller-resetreboot-when-opening-usb-port-on-linux/
48
            serialPort->setDataTerminalReady(1);
346 Kevin 49
 
50
            if (ok) emit Serial_Connected();
344 Kevin 51
        }
52
    }
53
}
54
 
55
void SerialController::Serial_Disconnect()
56
{
57
    serialPort->close();
58
    delete serialPort;
59
    connected = false;
60
    emit Serial_Disconnected();
61
}
62
 
63
void SerialController::Serial_TransmitString(QString string)
64
{
65
    serialPort->write(string.toStdString().c_str());
66
    serialPort->write(SERIAL_NEWLINE_CHAR);
67
}
68
 
69
void SerialController::Serial_ProcessIncomingData()
70
{
71
    char tmpBuffer[SERIAL_BUFFER_SIZE];
72
    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
73
    while (len > 0) {
74
        for (int i = 0; i < len; i++) {
75
            // Save received data into local buffer
76
            bufferIn[bufferInIndex] = tmpBuffer[i];
77
 
78
            // If newline char is received, end current string
79
            if (tmpBuffer[i] == '\n') {
80
                if (bufferInOverflow)
81
                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
82
                else
83
                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
84
                bufferInOverflow = false;
85
                bufferInIndex = 0;
86
                emit Serial_ReceivedString(currString);
87
            } else {
88
                bufferInIndex++;
89
            }
90
 
91
            // If received string is larger than our serial buffer, append to previous data
92
            if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
93
                bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
94
                if (bufferInOverflow)
95
                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
96
                else
97
                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
98
                bufferInOverflow = true;
99
                bufferInIndex = 0;
100
            }
101
        }
102
        // Check if there is more data to be read from the serial port
103
        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
104
    }
105
}