Qt - 带有空条目的Bytearray?

时间:2017-04-27 10:07:10

标签: c++ qt qtserialport

我正在处理一个简单的函数,该函数能够使用发送到comport的信息在Qt中返回一个int。

我使用的QSerialPort类返回QBytearray

问题是我(有时)在QSerialPort.readAll返回的数组中获取空​​条目。这使我无法将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();
  }
}

终端监视器显示:

enter image description here

2 个答案:

答案 0 :(得分:0)

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

相反,您应该尝试将每个char转换为显示字节的十进制/十六进制表示而不是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

答案 1 :(得分:0)

我最终使用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);