PortSettings PositioningMethodSerialPortOptions::getPortSettings(const QString &APortName)
{
    PortSettings portSettings;
    OptionsNode node;
    if (Options::hasNode(OPV_POSITIONING_METHOD_SERIALPORT, APortName))
        node = Options::node(OPV_POSITIONING_METHOD_SERIALPORT, APortName);
    else
    {
        QSerialPort *serialPort = new QSerialPort(APortName);
        if (serialPort->open(QIODevice::ReadOnly))
        {
            portSettings.FBaudRate    = serialPort->baudRate();
            portSettings.FDataBits    = serialPort->dataBits();
            portSettings.FStopBits    = serialPort->stopBits();
            portSettings.FParity      = serialPort->parity();
            portSettings.FFlowControl = serialPort->flowControl();
            serialPort->close();
        }
        else // Failed to open the port
            node = Options::node(OPV_POSITIONING_METHOD_SERIALPORT, APortName);
        serialPort->deleteLater();
    }
    if (!node.isNull())
    {
        portSettings.FBaudRate = node.value("baud-rate").toLongLong();
        portSettings.FDataBits = (QSerialPort::DataBits)node.value("data-bits").toInt();
        portSettings.FStopBits = (QSerialPort::StopBits)node.value("stop-bits").toInt();
        portSettings.FParity = (QSerialPort::Parity)node.value("parity").toInt();
        portSettings.FFlowControl = (QSerialPort::FlowControl)node.value("flow-control").toInt();
    }
    return portSettings;
}
Ejemplo n.º 2
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    qRegisterMetaType<eegDataType::iterator>("eegDataType::iterator");


    /// server
    ui->serverPortSpinBox->setMaximum(65535);
    int hostCounter = 0;
    ui->serverAddressComboBox->addItem("local, 120");
    ui->serverAddressComboBox->setItemData(hostCounter++,
                                           QVariant("127.0.0.1:120"));

    ui->serverAddressComboBox->addItem("local, home");
    ui->serverAddressComboBox->setItemData(hostCounter++,
                                           QVariant("127.0.0.1:35577"));

    ui->serverAddressComboBox->addItem("Enceph");
    ui->serverAddressComboBox->setItemData(hostCounter++,
                                           QVariant("213.145.47.104:120"));

    ui->serverAddressComboBox->addItem("pew");
    ui->serverAddressComboBox->setItemData(hostCounter++,
                                           QVariant("192.168.0.104:120"));
    connect(ui->serverAddressComboBox, SIGNAL(highlighted(int)),
            this, SLOT(serverAddressSlot(int)));
    connect(ui->serverAddressComboBox, SIGNAL(currentIndexChanged(int)),
            this, SLOT(serverAddressSlot(int)));
#if MY_LINROWS
    ui->serverAddressComboBox->setCurrentText("pew");
#else
    ui->serverAddressComboBox->setCurrentText("Enceph");
#endif


//    ui->serverAddressComboBox->setCurrentText("pew"); /// fix via router, always 192.168.0.104


    /// com
    for(int i = 0; i < 9; ++i)
    {
        ui->comPortComboBox->addItem("COM"+QString::number(i+1));
    }
    ui->comPortComboBox->setCurrentText("COM5");

#if COM_IN_MAIN
    connect(ui->connectComPortPushButton, SIGNAL(clicked()),
            this, SLOT(comPortSlot()));

    comPort = new QSerialPort(this);
    connect(comPort, SIGNAL(error(QSerialPort::SerialPortError)),
            this, SLOT(serialPortErrorSlot(QSerialPort::SerialPortError)));
    connect(this->ui->comPortSendOnePushButton, SIGNAL(clicked()),
            this, SLOT(sendOne()));
    connect(this->ui->comPortSendTwoPushButton, SIGNAL(clicked()),
            this, SLOT(sendTwo()));
#endif



#if 0
    /// COM test
    QSerialPort * comPort;
    comPort = new QSerialPort(this);

    comPort->setPortName(ui->comPortComboBox->currentText());
    comPort->open(QIODevice::WriteOnly);

    if(comPort->isOpen())
    {
//        cout << comPort->errorString() << endl;
        cout << "serialPort opened: " + def::comPortName << endl;
        cout << "portName: " << comPort->portName().toStdString() << endl;
        cout << "dataBits: " << comPort->dataBits() << endl;
        cout << "baudRate: " << comPort->baudRate() << endl;
        cout << "dataTerminalReady: " << comPort->isDataTerminalReady() << endl;
        cout << "flowControl: " << comPort->flowControl() << endl;
        cout << "requestToSend: " << comPort->isRequestToSend() << endl;
        cout << "stopBits: " << comPort->stopBits() << endl << endl;
    }

    comPortDataStream.setDevice(comPort);
#endif



    /// socket
#if SOCKET_IN_MAIN
    socket = new QTcpSocket(this);
#endif

#if !DATA_READER
    socketDataStream.setDevice(socket);
    socketDataStream.setByteOrder(QDataStream::LittleEndian); // least significant bytes first
#endif

#if SOCKET_IN_MAIN
    connect(socket, SIGNAL(error(QAbstractSocket::SocketError)),
            this, SLOT(socketErrorSlot(QAbstractSocket::SocketError)));
    connect(socket, SIGNAL(connected()),
            this, SLOT(socketConnectedSlot()));
    connect(socket, SIGNAL(disconnected()),
            this, SLOT(socketDisconnectedSlot()));

    connect(ui->connectToServerPushButton, SIGNAL(clicked()),
            this, SLOT(connectSlot()));
    connect(ui->disconnectFromServerPushButton, SIGNAL(clicked()),
            this, SLOT(disconnectSlot()));
#endif
    /// "static" COM-port sending
    connect(this->ui->comPortSendOnePushButton, SIGNAL(clicked()),
            this, SLOT(sendOne()));
    connect(this->ui->comPortSendTwoPushButton, SIGNAL(clicked()),
            this, SLOT(sendTwo()));

    connect(this->ui->startPushButton, SIGNAL(clicked()),
            this, SLOT(startSlot()));
    connect(this->ui->endPushButton, SIGNAL(clicked()),
            this, SLOT(endSlot()));
    ui->fullDataCheckBox->setChecked(true);


    /// DataProcessor

    if(1)
    {
        def::eegData.resize(10 * def::windowLength);
//        cout << "list size = " << def::eegData.size() << endl;
        def::comPortName = ui->comPortComboBox->currentText();
        myNetThread = new QThread;
        myNetHandler = new NetHandler();

        myNetHandler->moveToThread(myNetThread);
        connect(myNetThread, SIGNAL(started()),
                myNetHandler, SLOT(startWork()));

        connect(myNetHandler, SIGNAL(finishWork()),
                myNetThread, SLOT(quit()));

        connect(myNetHandler, SIGNAL(finishWork()),
                myNetHandler, SLOT(deleteLater()));

        connect(myNetHandler, SIGNAL(finishWork()),
                myNetThread, SLOT(deleteLater()));

        myNetThread->start(QThread::HighestPriority); // veru fast
    }
    if(0)
    {
        myNetHandler->finishWork();
        myNetThread->wait();
        delete myNetThread;
    }
}