试图通过qserialport将arduino与PC进行通信

问题描述

enter image description here

创建基于qtquick的应用程序后将arduino连接到PC,我创建了arduino.cpp和arduino.h文件代码中有错误,但是我无法解决它。请帮助,如果有人可以帮助,这些是.cpp和.h文件。我不知道为什么不接受序列设置作为类型名称, 那么某些行就会出错。

#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()),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

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)