Subversion Repositories Code-Repo

Rev

Rev 346 | Go to most recent revision | 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";
    dataBits << "8 Bits" << "7 Bits" << "6 Bits" << "5 Bits";
    stopBits << "1 Bit" << "1.5 Bits" << "2 Bits";
    parity << "None" << "Even" << "Odd" << "Space" << "Mark";
    flowControl << "None" << "Hardware" << "Software";
    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, dataBits, stopBits, parity, flowControl);
}

void SerialController::Serial_Connect(QString port, QString speed, QString dataBits,
                            QString stopBits, QString parity, QString flowControl)
{
    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;
            bool ok = false;

            int baud = speed.toInt(&ok);
            if (ok) serialPort->setBaudRate(baud);

            if (dataBits == "5 Bits")
                serialPort->setDataBits(QSerialPort::Data5);
            else if (dataBits == "6 Bits")
                serialPort->setDataBits(QSerialPort::Data6);
            else if (dataBits == "7 Bits")
                serialPort->setDataBits(QSerialPort::Data7);
            else
                serialPort->setDataBits(QSerialPort::Data8);

            if (stopBits == "1.5 Bits")
                serialPort->setStopBits(QSerialPort::OneAndHalfStop);
            else if (stopBits == "2 Bits")
                serialPort->setStopBits(QSerialPort::TwoStop);
            else
                serialPort->setStopBits(QSerialPort::OneStop);

            if (parity == "Even")
                serialPort->setParity(QSerialPort::EvenParity);
            else if (parity == "Odd")
                serialPort->setParity(QSerialPort::OddParity);
            else if (parity == "Space")
                serialPort->setParity(QSerialPort::SpaceParity);
            else if (parity == "Mark")
                serialPort->setParity(QSerialPort::MarkParity);
            else
                serialPort->setParity(QSerialPort::NoParity);

            if (flowControl == "Hardware")
                serialPort->setFlowControl(QSerialPort::HardwareControl);
            else if (flowControl == "Software")
                serialPort->setFlowControl(QSerialPort::SoftwareControl);
            else
                serialPort->setFlowControl(QSerialPort::NoFlowControl);

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

            if (ok) emit Serial_Connected();
            else Serial_Disconnect();
        }
    }
}

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));
    }
}