QSerialPort读取错误的数据计数

时间:2015-05-23 11:25:59

标签: c++ qt serial-port

这就是我想要做的事情:

我有一个测量压力的测量设备。它通过COM1连接。通过发送" ASCII字母9"数据,分别从该设备检索测量值。 这些测量应该在我的GUI中的QTableView子类中显示。我不希望GUI在从测量设备读取时冻结,所以我猜这称为非阻塞。

在我的代码和测试中,我想在for循环中检索10个测量值。但我总是得到6,有时是7.

我还将QSerialPort子类化。

代码如下所示。我暗示我的错误或者甚至更正我的代码的提示将非常感激。也可以随意评论代码的设计。

void MainWindow::startInspection()
{
    SauterFH_S *sauterFH_S;
    try
    {
        sauterFH_S = new SauterFH_S(new SerialPort(serialPort, 
        baudRate));
    }
    catch(QSerialPort::SerialPortError& e)
    {
        qDebug() << e;
    }

    connect(sauterFH_S, SIGNAL(measurandAvalaible(char*)),
        measurandTableWidget, SLOT(insertMeasurand(char*)));

    // Retrieve 10 measurements
    for(int i=0; i<10; ++i)
        sauterFH_S->getMeasurand();

    delete sauterFH_S;
}

子类化QSerialPort的构造函数如下:

TASte::IO::SerialPort::SerialPort(const QString &portName, qint32 
    baudRate, DataBits dataBits, Parity parity, StopBits stopBits,
    QIODevice::OpenMode openMode, QObject *parent)
:QSerialPort(parent)
{
    setPort(QSerialPortInfo(portName));
    setBaudRate(baudRate);
    setDataBits(dataBits);
    setParity(parity);
    setStopBits(stopBits);

    if( !open(openMode) ) throw error();
}

这里应该重要的其余部分:

TASte::Gauge::SauterFH_S::SauterFH_S(IO::SerialPort *port)
    :_port(port)
{

    connect(_port, SIGNAL(readyRead()),this, SLOT(onReadyRead()));
}

TASte::Gauge::SauterFH_S::~SauterFH_S()
{
    // delete _port;
}

void TASte::Gauge::SauterFH_S::getMeasurand()
{
    // typedef QByteArray SerialCommand
    IO::SerialCommand command("9");
    _port->write(command);
}


void TASte::Gauge::SauterFH_S::onReadyRead()
{
    // static const int DATA_LENGTH=8;
    char data[DATA_LENGTH];
    _port->read(data, DATA_LENGTH);

    emit measurandAvalaible(data);
}

提前致谢!

1 个答案:

答案 0 :(得分:2)

如果您在其他线程中发送和接收数据,则对象QSerialPort运行良好。在您的案例数据中,它可能会丢失,程序有时会冻结。我有同样的问题。我告诉你一些事情。我在UDP中使用transmition编写代码,但在serialport中是相同的概念。首先,您必须为串行端口创建线程。在我的情况下,我创建了therad但是为UDP。并且您必须定义您的all connections每个都是Interafe beetwen Threads MainWindow和SerialPort。

    thForUdp = new QThread();
    udp->moveToThread(thForUdp);
    thForUdp->start();

    connect(this , SIGNAL(SIGNAL_RefreshStatus()) , udp , SLOT(SLOT_refreshStatus()) , Qt::QueuedConnection);
    connect(udp , SIGNAL(SIGNAL_TransmitionFailed()) , this , SLOT(SLOT_TrasmitionFailed()) , Qt::QueuedConnection); //od
    connect(udp , SIGNAL(SIGNAL_ActualStatus(QByteArray)) , schema , SLOT(SLOT_ActualStatus(QByteArray)) , Qt::QueuedConnection);
    connect(udp , SIGNAL(SIGNAL_RefreshTimer()) , this , SLOT(SLOT_StartRefreshTimer()) , Qt::QueuedConnection ); //do
    connect(this , SIGNAL(SIGNAL_GetAllName()) , udp , SLOT(SLOT_GetAllName()) , Qt::QueuedConnection );
    connect(udp , SIGNAL(SIGNAL_AllName(QVector<QString>)) , schema , SLOT(SLOT_AllName(QVector<QString>)), Qt::QueuedConnection);
    connect(udp , SIGNAL(SIGNAL_setEnableRefresh(bool)) , this , SLOT(SLOT_setEnableRefresh(bool)) , Qt::QueuedConnection);

现在您必须创建从QSerialPort继承的对象。就我而言,我来自QUdpSocket

class Udp : public QUdpSocket , public Object
{
    Q_OBJECT
public:
    Udp(Mediator *medium);

private slots:
    void SLOT_ReadyToReadStatus();

signals:
    void SIGNAL_TransmitionFailed();
    void SIGNAL_RefreshTimer();
    void SIGNAL_ActualStatus(QByteArray stat);
    void SIGNAL_AllName(QVector<QString> vec);
    void SIGNAL_setEnableRefresh(bool state);

};

如您所见,Udp类具有所有SIGNAL,那么您可以在第一个块代码中看到。然后,您在串口类中创建右信号和插槽以发送和接收数据

在我的情况下,这是在construktor Udp

QObject::connect(this , SIGNAL(readyRead()) , this  , SLOT(SLOT_ReadyToReadStatus()));`

现在您的程序将按照此规则运行。 MainWindow形成胎面A发送信号(获取数据)---&gt;&gt;在线程B中的对象串行端口中发送数据,并且在线程B中接收数据,然后将串行端口发送信号发送到线程A(将接收到的数据发送到线程A)---&gt;&gt; MainWindow收到数据

非常重要是通过SIGNAL&amp;机制与MainWindow和SerialPort 进行通信。 SLOT 因为是两个不同的线程。这是QT规则。

这个解决方案是你的程序不会冻结并且数据被complet接收,因为另一个线程会照顾这个。

通常我建议您在发送数据后使用功能waitForReadyRead(),并通过waitForReadtRead

接收
{
    if(!this->waitForReadyRead(3000))
    {
     // here is wait for data maximum 3 second 
    // if recived your data find in slot SLOT_ReadyToReadStatus()

    }
// if data is correct receive from this block you send to Thread A via SIGNAL!!
QByteArray array
SIGNAL_Here_Data_To_To_Thread_A(array)


}

试试这个