Subversion Repositories Code-Repo

Rev

Rev 353 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "SerialHelper.h"

SerialHelper::SerialHelper(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";
}

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

void SerialHelper::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 SerialHelper::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()));
        connect(serialPort, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(Serial_ProcessError(QSerialPort::SerialPortError)));
        serialPort->setPortName(port);
        if (serialPort->open(QIODevice::ReadWrite)) {
            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 SerialHelper::Serial_Disconnect()
{
    serialPort->disconnect();
    serialPort->close();
    delete serialPort;
    connected = false;
    emit Serial_Disconnected();
}

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

void SerialHelper::Serial_TransmitByteArray(QByteArray buffer)
{
    serialPort->write(buffer);
}

void SerialHelper::Serial_ProcessIncomingData()
{
    char tmpBuffer[SERIAL_BUFFER_SIZE];
    int len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
    while (len > 0) {
        for (int i = 0; i < len; i++) {
            emit Serial_ReceivedByte(tmpBuffer[i]);
        }
        // Check if there is more data to be read from the serial port
        len = serialPort->read(tmpBuffer, sizeof(tmpBuffer));
    }
}

void SerialHelper::Serial_ProcessError(QSerialPort::SerialPortError error)
{
    if (error == QSerialPort::NoError) return;

    switch(error) {
        case QSerialPort::DeviceNotFoundError:
            emit UpdateStatus("Serial Error: Nonexistant device");
            return;
        case QSerialPort::PermissionError:
            emit UpdateStatus("Serial Error: Device already opened or lacking permission");
            return;
        case QSerialPort::OpenError:
            emit UpdateStatus("Serial Error: Device already opened");
            return;
        case QSerialPort::NotOpenError:
            emit UpdateStatus("Serial Error: Device not opened");
            return;
        case QSerialPort::ParityError:
            emit UpdateStatus("Serial Error: Parity error detected");
            break;
        case QSerialPort::FramingError:
            emit UpdateStatus("Serial Error: Framing error detected");
            break;
        case QSerialPort::BreakConditionError:
            emit UpdateStatus("Serial Error: Break condition detected");
            break;
        case QSerialPort::WriteError:
            emit UpdateStatus("Serial Error: Unable to write to device");
            break;
        case QSerialPort::ReadError:
            emit UpdateStatus("Serial Error: Unable to read from device");
            break;
        case QSerialPort::ResourceError:
            emit UpdateStatus("Serial Error: Device missing or disconnected");
            break;
        case QSerialPort::UnsupportedOperationError:
            emit UpdateStatus("Serial Error: Operation not supported or prohibited");
            break;
        case QSerialPort::TimeoutError:
            emit UpdateStatus("Serial Error: Timeout occurred");
            break;
        case QSerialPort::UnknownError:
            emit UpdateStatus("Serial Error: Unknown error");
            break;
        default:
            break;
    }

    serialPort->clearError();
    Serial_Disconnect();
}