Created
January 30, 2018 04:05
-
-
Save esmarr58/f9bd4450dea6bd2da8d26c5d33dcff8c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void MainWindow::buscaYconectaArduino(){ | |
//Parte # 1, declaración inicial de las variables | |
arduino_esta_disponible = false; | |
arduino_puerto = ""; | |
arduino = new QSerialPort; | |
QString nombreDispositivoSerial = ""; | |
int nombreProductID = 0; | |
//Parte # 2,buscar puertos con los identificadores de Arduino | |
qDebug() << "Puertos disponibles: " << QSerialPortInfo::availablePorts().length(); | |
foreach (const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()) { | |
qDebug() << "Identificador del fabricante (VENDOR ID): " << serialPortInfo.hasVendorIdentifier(); | |
if(serialPortInfo.hasVendorIdentifier()){ | |
qDebug() << "ID Vendedor " << serialPortInfo.vendorIdentifier(); | |
qDebug() << "ID Producto: " << serialPortInfo.productIdentifier(); | |
if(serialPortInfo.productIdentifier() == arduino_UNO || serialPortInfo.productIdentifier() == arduino_MEGA){ | |
arduino_esta_disponible = true; | |
nombreDispositivoSerial = serialPortInfo.portName(); | |
nombreProductID = serialPortInfo.productIdentifier(); | |
} | |
} | |
} | |
//Parte # 3, conexión del puerto encontrado con Arduino | |
if(arduino_esta_disponible){ | |
arduino_puerto = nombreDispositivoSerial; | |
arduino ->setPortName(arduino_puerto); | |
arduino->open(QIODevice::ReadWrite); | |
arduino->setDataBits(QSerialPort::Data8); | |
arduino ->setBaudRate(QSerialPort::Baud115200); | |
arduino->setParity(QSerialPort::NoParity); | |
arduino->setStopBits(QSerialPort::OneStop); | |
arduino->setFlowControl(QSerialPort::NoFlowControl); | |
ui->label->clear(); | |
qDebug() << "Producto: " << nombreProductID; | |
if(nombreProductID == 67) ui->label->setText("Arduino UNO R3 conectado"); | |
else if(nombreProductID == 68) ui->label->setText("Arduino Mega conectado"); | |
else ui->label->setText("Error 3"); | |
} | |
else{ | |
ui->label->clear(); | |
ui->label->setText("No hay arduino"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment