处理来自Qt串口的收到的消息

时间:2015-05-28 07:43:32

标签: c++ qt

我从my last question收到回复后 我在代码中做了一些更改。我的架构现在由信号和插槽机制管理。

我在我的应用程序中使用了3层/模块。

  • 应用层(用于处理gui)
  • 引擎(用于处理业务逻辑和协议)
  • 串行(用于处理从通信端口发送和读取的字节)

在我的引擎类中,我只想在从串口获得确认时才发送命令。我真的想了解如何发送一系列命令(每个命令取决于前一个命令),而这里的所有内容都是异步的。

这是正确的方法吗?

这段代码:(相关)

singlepulse.cpp //表单小部件

在构造函数中:

engine = new Engine();
connect(this,SIGNAL(sendMessageToEngine(int,QString)),
        engine,SLOT(onNewMessageFromAppReceived(int,QString)));
connect(engine,SIGNAL(sendMessageToApp(QString)),
        this,SLOT(onNewMessageFromEngineReceived(QString)));

其余代码:

void SinglePulse::on_btnStartOperation_clicked()
{
    emit sendMessageToEngine(1);
}

void SinglePulse::onNewMessageFromEngineReceived(QString s)
{
}

engine.cpp:

Engine::Engine(QObject *parent) : QObject(parent)
{
    serial = new Serial();
    connect(this,SIGNAL(sendMessageToSerial(QString)),
            serial,SLOT(onNewMessageToSendReceived(QString)));
    connect(serial,SIGNAL(sendMessageToEngine(QByteArray)),
            this,SLOT(onNewMessageFromSerialReceived(QByteArray)));
}

void Engine::onNewMessageFromAppReceived(int msgId)
{
    ackArrived = false;
    if (msgId == 1)
    {
        emit sendMessageToSerial("ee120301fe09000261");  
        // here I want to check if acknowledge is arrived and if so - send the next command    
        if (ackArrived)
        {
            emit sendMessageToSerial("ee120145512400261");  //send here the next command
        }
    }
}

void Engine::onNewMessageFromSerialReceived(QByteArray b)
{
    ackArrived = false;
    QString hexValues;
    QString decodedMsg;
    hexValues = b.toHex();

    if (hexValues == "12ee02010176")
    {
        qDebug() << QDateTime::currentDateTime() << "get ack" ;
        decodedMsg = "ACK";
        ackArrived = true;
    }
}

serial.cpp:

Serial::Serial()
{
    serial = new QSerialPort(this);
    serial->setPortName("ttyS0");
    serial->setBaudRate(QSerialPort::Baud115200);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->open(QIODevice::ReadWrite);

    connect(serial, SIGNAL(readyRead()),this,SLOT(serialReceived()));
}

void Serial::onNewMessageToSendReceived(QString s)
{
    QString sometext = s;
    QByteArray ba = QByteArray::fromHex(sometext.toLatin1());
    serial->write(ba);
}

void Serial::serialReceived()
{
    QByteArray ba;

    ba = serial->readAll();
    qDebug() << ba.toHex();
    for (int i=0; i<sizeof(ba);i++)
        queueBytes.enqueue(ba[i]);

    int item;
    int packetSize;
    QByteArray baToSend;
    QVector<int> vect = queueBytes.toVector();

    if (vect[2] == 2)   // flow control
    {
        packetSize = 6;
        if (vect.size() >= packetSize)
        {  // the whole packet arrived
            for (int i =0 ;i<packetSize;i++)
            {
                item = queueBytes.dequeue();
                baToSend.append(item);
            }
            emit sendMessageToEngine(baToSend);
        }
    }
    else if (vect[2] == 3)   // data token packet
    {
        packetSize = vect[3];
        if (vect.size() >= packetSize)
        {   // the whole packet arrived
            for (int i =0 ;i<packetSize;i++)
            {
                item = queueBytes.dequeue();
                baToSend.append(item);
            }
            emit sendMessageToEngine(baToSend);
        }
    }
}

0 个答案:

没有答案