首页 > 解决方案 > 试图通过 qserialport 将 arduino 与 pc 通信

问题描述

在此处输入图像描述为了在创建基于 qtquick 的应用程序后将 arduino 连接到 PC,我创建了 arduino.cpp 和 arduino.h 文件。代码中有问题,但我无法得到并解决它。请,如果有人可以提供帮助,这里是 .cpp 和 .h 文件。我不知道为什么不接受 serialsettings 作为类型名称,然后某些行会出错。

#include "Arduino.h"
#include<QSerialPort>
Arduino::Arduino()
{
}

   void Arduino::setupSerialPort(serialSettings *port){
       port->baudRate=QSerialPort::Baud9600;
       port->dataBits=QSerialPort::Data8;
       port->parity=QSerialPort::NoParity;
       port->stopBits=QSerialPort::OneStop;
       port->flowControl= QSerialPort::NoFlowControl;

   }


   void Arduino::setupCom(){

   serial1 = new QSerialPort();
   serial2 = new QSerialPort();
   serial3 = new QSerialPort();
   // Port 1 settings sudo echo "Test" > /dev/ttyS10

   port1Settings.name="ttyS10";
   setupSerialPort(&port1Settings);

   // Port 2 settings
   port2Settings.name="ttyUSB0";
   port2Settings.name="ttyS12";
   setupSerialPort(&port2Settings);
   // Port 3 settings
   port3Settings.name="ttyUSB3";
   port3Settings.name="ttyS14";
   setupSerialPort(&port3Settings);

   }



   bool Arduino::openSerialPort(QSerialPort *ser,serialSettings p)
   {
       ser->setPortName(p.name);
       bool serialWorking=false;
       if (ser->open(QIODevice::ReadWrite)) {
           if (ser->setBaudRate(p.baudRate)
                   && ser->setDataBits(p.dataBits)
                   && ser->setParity(p.parity)
                   && ser->setStopBits(p.stopBits)
                   && ser->setFlowControl(p.flowControl)) {

               //statusBar->showMessage(tr("Connected to %1 ").arg(p.name));
               qDebug()<<"connected to 1%";

               serialWorking=true;
              // connect(ser, SIGNAL(readyRead()), SLOT(showResponse()));
   //            port1Infos=QSerialPortInfo(p.name);
   //                    qDebug()<<"Connected to port : "<<port1Infos.productIdentifier()<<port1Infos.description()<<port1Infos.manufacturer();

           } else {
               ser->close();
               //QMessageBox::critical(this, tr("Erreur dans les parametres de communication Serie %1").arg(p.name), ser->errorString());
               qDebug()<<"Erreur dans les parametres de communication Serie";
               //statusBar->showMessage(tr("Open error"));
               qDebug()<<"open error";
           }
       } else {
           //QMessageBox::critical(this, tr("Erreur!"), ser->errorString());
        qDebug()<<"Erreur "<<ser->error()<<" .Impossible d'ouvrir le port Serie 1: "<<ser->errorString();
        //statusBar->showMessage(tr("Impossible d'ouvrir le port Serie %1: %2").arg(p.name).arg(ser->errorString()));
       }
       return serialWorking;

   }


   void Arduino::openSerialPorts(){



        qDebug()<<"ouvertture des Ports Serie: "<<port1Settings.name<<" "<<port2Settings.name<<" "<<port3Settings.name;

       if (!serial1->isOpen()){

           serial1Working=openSerialPort(serial1,port1Settings);
           if (!serial1Working){
              // arret(2,true);
           }
       }

//        if (!serial2->isOpen()){

//            serial2Working=openSerialPort(serial2,port2Settings);
//            if (!serial2Working){
//                arret(2,true);
//            }
//        }

//        if (!serial3->isOpen()){

//            serial3Working=openSerialPort(serial3,port3Settings);
//            if (!serial3Working){
//                arret(2,true);
//            }
//        }

       machineStarting=true;

       connect (this,SIGNAL(checkCom1()),this,SLOT(updateChargement()));

           if (!com1Working){
           doAction(QString("X"),serial1);
        // We wait for the correct response for 0.5s with a one shot timer
           startSingleShotTimer(checkCom1Timer,intervalleCheckCom);
           connect(checkCom1Timer, SIGNAL(timeout()), this, SLOT (updateChargement()));
           }

   }
   void updateChargement()
   { const QByteArray requestData;
       requestData.fromStdString(std::string("hi"));
       serial.write(requestData);
       if (serial.waitForBytesWritten(m_waitTimeout)) {
//! [8] //! [10]
           // read response
           if (serial.waitForReadyRead(currentWaitTimeout)) {
               QByteArray responseData = serial.readAll();
               while (serial.waitForReadyRead(10))
                   responseData += serial.readAll();

               const QString response = QString::fromUtf8(responseData);

QDebug()<<"reponse arduino""<< response;

   }
}}

#ifndef SERIALCOM_H
#define SERIALCOM_H
#include <QtSerialPort>
#include <QSerialPortInfo>
class Arduino
{
public:
   Arduino();
   {QString name;
       qint32 baudRate;
       QString stringBaudRate;
       QSerialPort::DataBits dataBits;
       QString stringDataBits;
       QSerialPort::Parity parity;
       QString stringParity;
       QSerialPort::StopBits stopBits;
       QString stringStopBits;
       QSerialPort::FlowControl flowControl;
       QString stringFlowControl;}
void setupini();
void setupCom();
void openSerialPorts();
void updateChargement();
private:
  

   void  setupSerialPort(serialSettings *port);

   serialSettings port1Settings;
   serialSettings port2Settings;
   serialSettings port3Settings;

   QSerialPortInfo port1Infos;
   QSerialPortInfo port2Infos;
   QSerialPortInfo port3Infos;

  QSerialPort *serial;// utilise pour les tests! a supprimer apres
  QSerialPort *serial1;// COM1
  QSerialPort *serial2;// COM2
  QSerialPort *serial3;// COM3
  bool serial1Working,serial2Working,serial3Working;

  bool openSerialPort(QSerialPort *ser, serialSettings p);

  void sendMsg(QSerialPort *ser,QString msg);

};

#endif // SERIALCOM_H

标签: c++arduinoqtserialportqqmlapplicationengineqtquick-designer

解决方案


推荐阅读