Subversion Repositories Code-Repo

Rev

Rev 344 | Blame | Last modification | View Log | RSS feed

#include "SerialController.h"

SerialController::SerialController(QObject *parent) : QObject(parent)
{
    connected = false;
    speeds << "9600" << "19200" << "38400" << "57600" << "115200" << "230400" << "250000";
    bufferInIndex = 0;
    bufferInOverflow = false;
}

SerialController::~SerialController()
{
    serialPort->close();
    delete serialPort;
}

void SerialController::Serial_QueryParameters()
{
    QList<QSerialPortInfo> portsList = QSerialPortInfo::availablePorts();
    QStringList ports;
    for (int i = 0; i < portsList.size(); i++) {
        ports.append(portsList[i].portName());
    }
    emit Serial_UpdateParameters(ports, speeds);
}

void SerialController::Serial_Connect(QString port, QString speed)
{
    if (!connected) {
        serialPort = new QSerialPort();
        connect(serialPort, SIGNAL(readyRead()), this, SLOT(Serial_ProcessIncomingData()));
        serialPort->setPortName(port);
        if (!serialPort->open(QIODevice::ReadWrite)) {
            Serial_Disconnect();
        } else {
            connected = true;
            if (speed == "9600")
                serialPort->setBaudRate(9600);
            else if (speed == "19200")
                serialPort->setBaudRate(19200);
            else if (speed == "38400")
                serialPort->setBaudRate(38400);
            else if (speed == "57600")
                serialPort->setBaudRate(57600);
            else if (speed == "115200")
                serialPort->setBaudRate(115200);
            else if (speed == "230400")
                serialPort->setBaudRate(230400);
            else if (speed == "250000")
                serialPort->setBaudRate(250000);

            serialPort->setDataBits(QSerialPort::Data8);
            serialPort->setParity(QSerialPort::NoParity);
//            serialPort->setFlowControl(QSerialPort::HardwareControl);
            serialPort->setStopBits(QSerialPort::OneStop);

            // See http://umforum.ultimaker.com/index.php?/topic/5886-um2-controller-resetreboot-when-opening-usb-port-on-linux/
            serialPort->setDataTerminalReady(1);
            emit Serial_Connected();
        }
    }
}

void SerialController::Serial_Disconnect()
{
    serialPort->close();
    delete serialPort;
    connected = false;
    emit Serial_Disconnected();
}

void SerialController::Serial_TransmitString(QString string)
{
    serialPort->write(string.toStdString().c_str());
    serialPort->write(SERIAL_NEWLINE_CHAR);
}

void SerialController::Serial_ProcessIncomingData()
{
    char tmpBuffer[SERIAL_BUFFER_SIZE];
    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
    while (len > 0) {
        for (int i = 0; i < len; i++) {
            // Save received data into local buffer
            bufferIn[bufferInIndex] = tmpBuffer[i];

            // If newline char is received, end current string
            if (tmpBuffer[i] == '\n') {
                if (bufferInOverflow)
                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
                else
                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
                bufferInOverflow = false;
                bufferInIndex = 0;
                emit Serial_ReceivedString(currString);
            } else {
                bufferInIndex++;
            }

            // If received string is larger than our serial buffer, append to previous data
            if (bufferInIndex == SERIAL_BUFFER_SIZE-1) {
                bufferIn[SERIAL_BUFFER_SIZE-1] = 0x0;
                if (bufferInOverflow)
                    currString.append(QString::fromLocal8Bit(bufferIn, bufferInIndex));
                else
                    currString = QString::fromLocal8Bit(bufferIn, bufferInIndex);
                bufferInOverflow = true;
                bufferInIndex = 0;
            }
        }
        // Check if there is more data to be read from the serial port
        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
    }
}