如何在多小部件应用程序中使用qserialport [英] How to use qserialport in multi widget application

查看:59
本文介绍了如何在多小部件应用程序中使用qserialport的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在开发一个与嵌入式设备串行交互的多小部件Qt桌面应用程序。我是Qt和串口应用程序开发的新手。





我需要以其他形式重复使用已打开的串口实例进行读写。



我可以在何处以及如何配置串口以便以多种形式访问它而无需重复配置?



例如,如果我必须在第二个小部件中连接,如何访问串行对象?



欢迎任何帮助。



我尝试了什么:



我在一个模块(.cpp文件)中创建了实例,然后导出通过.h文件声明需要访问它的其他模块。



这是串口实现的 .h />


Hello all,

I'm developing a multi-widget Qt desktop application that serially interacts with an embedded device. I'm new to Qt and serial port application development.


I need to reuse the opened serial port instance in other forms as well for reading or writing.

Where and how I can configure the serial port in order to access it in multiple forms without repeating configuration?

for example, if I have to a connection in a second widget, how can I access the serial object??

Any help would be welcome.

What I have tried:

I created the instance in one module (.cpp file) and then export the declaration through a .h file which other modules who need to access it.

Here is the .h of the serial implementation

#ifndef TETRASERIAL_H
#define TETRASERIAL_H

#include <QObject>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QDebug>

class tetraSerial : public QObject
{
    Q_OBJECT
public:
    explicit tetraSerial(QObject *parent = nullptr);
  static QSerialPort *serialObject;

signals:

public slots:
};

#endif // TETRASERIAL_H





相应的 .cpp 文件如下。



我在该类文件中配置了串口。我想在不同的窗口中调用该实例。





The corresponding .cpp file as follows.

I configured the serial port in that class file. I want to call that instance in different windows.

#include "tetraserial.h"

 QSerialPort *tetraSerial::serialObject;


tetraSerial::tetraSerial(QObject *parent) : QObject(parent)
{

   serialObject = new QSerialPort(this);
   serialObject->setPortName("com5");
   serialObject->setBaudRate(1000000);
   serialObject->setDataBits(QSerialPort::Data8);
   serialObject->setParity(QSerialPort::NoParity);
   serialObject->setStopBits(QSerialPort::OneStop);
   serialObject->setFlowControl(QSerialPort::HardwareControl);
   serialObject->open(QIODevice::ReadWrite);

}





我试着用MainWindow中的`connect`来调用它,如下所示。





I tried to call this using `connect` in the MainWindow as follows.

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtSerialPort/QSerialPort>
#include "tetraserial.h"


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

  connect(tetraSerial::serialObject, SIGNAL(readyRead()),this,SLOT(serialRecived()));

}







我收到运行时错误并抛出此错误在控制台中。



无法连接< null> :: readData()Mainwindow :: serialRecived

推荐答案

一个串口一次只能被一个线程访问(打开;关闭)。



每个窗口都有自己的线程;所以他们不能共享端口。



您可以共享数据/并发队列;不是港口。
A serial port can only be accessed by one thread at a time (open; close).

Each Window has its own thread; so they cannot "share the port".

You can share the "data" / concurrent queues; not the port.


这篇关于如何在多小部件应用程序中使用qserialport的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆