Qt - 带有空条目的字节数组?

Qt - Bytearray with empty entries?

我正在开发一个简单的函数,它能够 return Qt 中的一个 int 使用发送到 comport 的信息。

我正在使用 QSerialPort class,其中 return 是 QBytearray

问题是我似乎(有时)在 QSerialPort.readAll return 的数组中得到空条目。这使我无法将 bytearray 转换为 int。

基本功能是:要求 Arduino 发送温度或湿度。

Qt代码:

#include <QCoreApplication>
#include <QSerialPortInfo>
#include <QSerialPort>
#include <iostream>
#include <string>
#include <windows.h>
#include <math.h>
using namespace std;


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString comPort = "COM6";
    QSerialPortInfo ArduinoInfo(comPort);

    cout << "Manufacturer: " << ArduinoInfo.manufacturer().toStdString() << endl;
    cout << "Product Identifier: " << ArduinoInfo.productIdentifier() << endl;
    cout << "Vendor Identifier: " << ArduinoInfo.vendorIdentifier() << endl;

    QSerialPort Arduino(ArduinoInfo);

    Arduino.setBaudRate(QSerialPort::Baud9600);
    Arduino.open(QSerialPort::ReadWrite);

    Sleep(1000);

    if(Arduino.isDataTerminalReady())
        cout << "Great Sucess" << endl;

    char sending = 'H';


    cout << sending << endl;

    Arduino.write(&sending, 1);

    //int maxSize = Arduino.bytesAvailable();

    while(!Arduino.waitForReadyRead()){}
    Sleep(100);

    QByteArray rawDataArry = Arduino.readAll();
    cout << "Shit has been read." << endl;


    // Form here on its just write functions, used for debug

    cout << "rawData:" << endl;
    for(int i=0; i < rawDataArry.size(); i++)
        cout << "[" << i << "] "<< rawDataArry[i] << endl;

    cout << "All data:" << endl;
    for(char s:rawDataArry){
        cout << s;
    }
    cout << endl;

    cout << "Converted data:" << endl;
    bool ok;
    int returnVar = rawDataArry.toInt(&ok, 10);
    cout << returnVar << endl;
    cout << "Convertion Status:" << ok;

    Arduino.close();

    return a.exec();
}

Arduino代码超级简单

#include <dht.h>

dht DHT;

#define PIN_7 7

void setup() {
  Serial.begin(9600);
}

void loop()
{
  String impString;

  while(Serial.available() != 1);
  impString = Serial.readString();

  DHT.read11(PIN_7);

  if(impString == "T")
  {
    int temp = DHT.temperature;
    Serial.println(temp);
  }
  else if(impString == "H")
  {
    int humid = DHT.humidity;
    Serial.println(humid); 
  }

  emptyReceiveBuf();
  delay(100);
}

void emptyReceiveBuf()
{
  int x;
  delay(200);  // vent lige 200 ms paa at alt er kommet over

  while (0 < Serial.available())
  {
    x = Serial.read();
  }
}

终端监视器显示:

看起来 rawDataArry.size() 返回 4,这意味着数组中有 4 个字节。但是,当您调用 rawDataArry[i] 时,它 returns 是一个字符。并非每个 char 值都可以表示为 ascii 字符。这就是最后 2 个字节显示为空的原因。

相反,您应该尝试将每个字符转换为显示字节 decimal/hex 表示而不是 ascii 表示的值。

或者,您可以将这 4 个字节直接转换为 int 并完成它:

//Big Endian
quint32 myValue(0);
for(int i=0; i<4; i++)
    myValue = myValue + (quint8(rawDataArry.at(i)) << (3-i)*8);
//myValue should now have your integer value

我最终使用了 QByteArray 的一个成员来清理数组中的宽空间!

看起来像这样!

bool ok;
int returnVal;

do
{
  Arduino.write(&imputChar, 1); // Arduino input Char

  Arduino.waitForReadyRead();

  QByteArray rawDataArry(Arduino.readAll()); // Empties buffer into rawDataArry 
  QByteArray dataArray(rawDataArry.simplified()); // Removes all widespaces!

  returnVal = dataArray.toInt(&ok, 10); // Retuns ByteConvertion - &OK is true if convertion has completed - Widespaces will ruin the conversion

  qDebug() << "Converted data:";
  qDebug() << returnVal;
  qDebug() << "Convertion Status:" << ok;
  }while(ok != 1);

  return(returnVal);