bool SerialCommunication::SerialDisconnect() { // serial disconnect if (serial.isOpen()) { serial.close(); return true; } return false; }
void SerialCommunication::rgbChange(int r, int g, int b) { data = ""; data.append("0" + QString::number(r) + "," + QString::number(g) + "," + QString::number(b) + "\n"); qDebug("Sending: " + data); if (serial.isOpen()) serial.write(data); }
bool SerialCommunication::SerialConnect(QString PortName) { // serial connect if (!serial.isOpen()) { serial.setPortName(PortName); serial.setBaudRate(QSerialPort::Baud9600); serial.setDataBits(QSerialPort::Data8); serial.setParity(QSerialPort::NoParity); serial.setStopBits(QSerialPort::OneStop); serial.setFlowControl(QSerialPort::NoFlowControl); serial.open(QIODevice::ReadWrite); serial.waitForBytesWritten(9000); if (serial.isWritable()) { return true; } } return false; }
void MainWindow::run() { QSerialPort availableport; while(true){ availableport.setPortName("COM6"); availableport.open(QIODevice::ReadOnly); availableport.setBaudRate(QSerialPort::Baud9600); availableport.setDataBits(QSerialPort::Data8); availableport.setParity(QSerialPort::NoParity); availableport.setStopBits(QSerialPort::OneStop); availableport.setFlowControl(QSerialPort::NoFlowControl); if(availableport.isOpen()){ //DEBUG MODE ON bioGuised->show(); delay(5000); } else{ bioGuised->hide(); } } }
GPSModule::GPSModule(QObject *parent) : QObject(parent) { QSerialPort* serial = new QSerialPort(this); serial->setPortName(QString("/dev/ttyUSB0")); serial->setBaudRate(QSerialPort::Baud9600); serial->setDataBits(QSerialPort::Data8); serial->setParity(QSerialPort::NoParity); serial->setStopBits(QSerialPort::OneStop); serial->open(QIODevice::ReadOnly); if(serial->isOpen()) { QNmeaPositionInfoSource *source = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::RealTimeMode); source->setDevice(serial); if(source){ connect(source, SIGNAL(positionUpdated(QGeoPositionInfo)), this, SLOT(positionUpdate(QGeoPositionInfo))); source->setPreferredPositioningMethods(QGeoPositionInfoSource::AllPositioningMethods); source->setUpdateInterval(2000); source->startUpdates(); } } }
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; } }
void SerialPortThread::run() { bool __isStopped = true; m_serialPortMutex.lock(); __isStopped = m_isStopped; m_serialPortMutex.unlock(); qDebug("thread running"); SerialPortSettings::Settings __mSettings; bool __isSettingsChanged = false; QByteArray __dataReceived; QByteArray __dataToSend; // unsigned char rx_buffer[256]; QSerialPort serial; while(!__isStopped) { m_serialPortMutex.lock(); if(m_isSettingsChanged){ m_isSettingsChanged =false; __isSettingsChanged = true; } m_serialPortMutex.unlock(); if(__isSettingsChanged){ __isSettingsChanged =false; if(serial.isOpen()){ serial.close(); } m_serialPortMutex.lock(); __mSettings = m_currentSettings; m_serialPortMutex.unlock(); serial.setPortName(__mSettings.name); serial.setBaudRate(__mSettings.baudRate); serial.setDataBits(__mSettings.dataBits); serial.setParity(__mSettings.parity); serial.setStopBits(__mSettings.stopBits); serial.setFlowControl(__mSettings.flowControl); emit message(QString("settings changed !")); }//settings changed if(!serial.isOpen()){ if(serial.open(QIODevice::ReadWrite)){ emit message(QString("open successfully !")); } else{ m_serialPortMutex.lock(); m_isStopped = true; m_serialPortMutex.unlock(); emit message(QString("open failed !")); break; } }//if not open , while loop break /*=== opened , processing data here ===*/ m_serialPortMutex.lock(); if(!m_dataToSend.isEmpty()){ __dataToSend.append(m_dataToSend); m_dataToSend.clear(); } m_serialPortMutex.unlock(); if(! __dataToSend.isEmpty()){ if(serial.write(__dataToSend) > 0){ if (serial.waitForBytesWritten(100) == true) { qDebug() <<"write successfully !"; __dataToSend.clear(); } else qDebug() <<"write time out !"; } } if (serial.waitForReadyRead(100)) { __dataReceived = serial.readAll(); while (serial.waitForReadyRead(10)) __dataReceived += serial.readAll(); } //unpack the data received if(__dataReceived.size() > 0){ emit message(__dataReceived); // for(int i = 0; i < __dataReceived.size(); i ++){ // if(FrameUnpack(__dataReceived.at(i),rx_buffer)){ // tHeader* pHeader = (tHeader*)rx_buffer; // if( pHeader->cmd == CMD_ID_SENSOR_INFO){ // // Cmd_Data *pData = (Cmd_Data *)&rx_buffer[sizeof(tHeader)];// // } // else{ // } // } // } }//unpack code end /*=== processing data end ===*/ m_serialPortMutex.lock(); __isStopped = m_isStopped; m_serialPortMutex.unlock(); }//while loop end if(serial.isOpen()){ serial.close(); emit message(QString("close successfully !")); } qDebug("thread exit"); }