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;
37
            if (speed == "9600")
38
                serialPort->setBaudRate(9600);
39
            else if (speed == "19200")
40
                serialPort->setBaudRate(19200);
41
            else if (speed == "38400")
42
                serialPort->setBaudRate(38400);
43
            else if (speed == "57600")
44
                serialPort->setBaudRate(57600);
45
            else if (speed == "115200")
46
                serialPort->setBaudRate(115200);
47
            else if (speed == "230400")
48
                serialPort->setBaudRate(230400);
49
            else if (speed == "250000")
50
                serialPort->setBaudRate(250000);
51
 
52
            serialPort->setDataBits(QSerialPort::Data8);
53
            serialPort->setParity(QSerialPort::NoParity);
54
            serialPort->setFlowControl(QSerialPort::HardwareControl);
55
            serialPort->setStopBits(QSerialPort::OneStop);
56
 
57
            // See http://umforum.ultimaker.com/index.php?/topic/5886-um2-controller-resetreboot-when-opening-usb-port-on-linux/
58
            serialPort->setDataTerminalReady(1);
59
            emit Serial_Connected();
60
        }
61
    }
62
}
63
 
64
void SerialController::Serial_Disconnect()
65
{
66
    serialPort->close();
67
    delete serialPort;
68
    connected = false;
69
    emit Serial_Disconnected();
70
}
71
 
72
void SerialController::Serial_TransmitString(QString string)
73
{
74
    serialPort->write(string.toStdString().c_str());
75
    serialPort->write(SERIAL_NEWLINE_CHAR);
76
}
77
 
78
void SerialController::Serial_ProcessIncomingData()
79
{
80
    char tmpBuffer[SERIAL_BUFFER_SIZE];
81
    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
82
    while (len > 0) {
83
        for (int i = 0; i < len; i++) {
84
            // Save received data into local buffer
85
            bufferIn[bufferInIndex] = tmpBuffer[i];
86
 
87
            // If newline char is received, end current string
88
            if (tmpBuffer[i] == '\n') {
89
                if (bufferInOverflow)
90
                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
91
                else
92
                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
93
                bufferInOverflow = false;
94
                bufferInIndex = 0;
95
                emit Serial_ReceivedString(currString);
96
            } else {
97
                bufferInIndex++;
98
            }
99
 
100
            // If received string is larger than our serial buffer, append to previous data
101
            if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
102
                bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
103
                if (bufferInOverflow)
104
                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
105
                else
106
                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
107
                bufferInOverflow = true;
108
                bufferInIndex = 0;
109
            }
110
        }
111
        // Check if there is more data to be read from the serial port
112
        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
113
    }
114
}