Hey,
I try to control a GPIO with Qt.
I use the example from GitHub:
https://github.com/cedoduarte/SerialPort
In this Template I can only control one GPIO with Qt but I want to control 3 ...the problem is that i don't know how.
Arduino Code:
#define PIN_LED 9
void setup()
{
pinMode(PIN_LED, OUTPUT);
analogWrite(PIN_LED, 0);
Serial.begin(9600);
}
void loop()
{
if (Serial.available()){
char led_specifier = Serial.read();
int led_brightness = Serial.parseInt();
analogWrite(PIN_LED, led_brightness);
}
}
Qt:
#include "Widget.h"
#include "ui_Widget.h"
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMessageBox>
#include <QDebug>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
arduino_is_available = false;
arduino_port_name = "";
arduino = new QSerialPort;
qDebug() << "Number of available ports: " << QSerialPortInfo::availablePorts().length();
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
qDebug() << "Has vendor ID: " << serialPortInfo.hasVendorIdentifier();
if(serialPortInfo.hasVendorIdentifier()){
qDebug() << "Vendor ID: " << serialPortInfo.vendorIdentifier();
}
qDebug() << "Has Product ID: " << serialPortInfo.hasProductIdentifier();
if(serialPortInfo.hasProductIdentifier()){
qDebug() << "Product ID: " << serialPortInfo.productIdentifier();
}
}
foreach(const QSerialPortInfo &serialPortInfo, QSerialPortInfo::availablePorts()){
if(serialPortInfo.hasVendorIdentifier() && serialPortInfo.hasProductIdentifier()){
if(serialPortInfo.vendorIdentifier() == arduino_uno_vendor_id){
if(serialPortInfo.productIdentifier() == arduino_uno_product_id){
arduino_port_name = serialPortInfo.portName();
qDebug() << "HALLLLLOO" + arduino_port_name;
arduino_is_available = true;
}
}
}
}
if(arduino_is_available){
// open and configure the serialport
arduino->setPortName(arduino_port_name);
arduino->open(QSerialPort::WriteOnly);
arduino->setBaudRate(QSerialPort::Baud9600);
arduino->setDataBits(QSerialPort::Data8);
arduino->setParity(QSerialPort::NoParity);
arduino->setStopBits(QSerialPort::OneStop);
arduino->setFlowControl(QSerialPort::NoFlowControl);
}else{
// give error message if not available
QMessageBox::warning(this, "Port error", "Couldn't find the Arduino!");
}
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_encenderPushButton_clicked()
{
if(arduino->isWritable()){
arduino->write("1");
}else{
qDebug() << "Couldn't write to serial!";
}
}
void Widget::on_apagarPushButton_clicked()
{
if(arduino->isWritable()){
arduino->write("0");
}else{
qDebug() << "Couldn't write to serial!";
}
}