我正在开发一个项目,我从串口接收数据。数据正在从Arduino Mega ADK生成和发送。 arduino数据以字符“S”,“N”,“P”开头,用于标识数据包的开头。接下来是18个字节的数据,存储在9个quint16变量中。接下来是3个字节存储在quint8值中,接着是20个字节存储在4个int32值中。
我在将数据作为字符串发送时有这个工作,但我似乎无法找到一种方法来使数据作为单独的字节发送,并将它们重建为它们的值。
从我的阅读中,Arduino是小端,所以我按照以下方式在接收端构造值:
解析数据的函数在这里:
void MainWindow::NoseAviByte()
{
//Get data from nose cone avi bay
readNoseConeAvi();
//Look for SNP packet header
int curCount = 0;
int maxCount = dataBytes.size();
//Loop through array of bytes
while(curCount < maxCount)
{
//If packet header is found, parse data vals
if(((dataBytes.at(curCount) == 'S') && (dataBytes.at(curCount + 1) == 'N') && (dataBytes.at(curCount + 2) == 'P')) && (curCount + 62) < maxCount)
{
//All incoming values should be in little endian order
curCount += 3;
/***Start Low-g MEMS MPU-9250***/
quint16 accX = 0;
quint16 accY = 0;
quint16 accZ = 0;
quint16 gyroX = 0;
quint16 gyroY = 0;
quint16 gyroZ = 0;
quint16 magX = 0;
quint16 magY = 0;
quint16 magZ = 0;
/***End Low-g MEMS MPU-9250***/
/***Start High-g ACC***/
quint8 accHX = 0;
quint8 accHY = 0;
quint8 accHZ = 0;
/***End High-g ACC***/
/***Start Hgh precision Alt***/
quint32 highPresAlt = 0;
/***End Hgh precision Alt***/
/***Start GPS***/
quint32 gpsLat = 0;
quint32 gpsLong = 0;
quint32 gpsAlt = 0;
quint32 gpsTime = 0;
accX = (dataBytes.at(curCount) + (dataBytes.at(curCount + 1) << 8));
curCount += 2;
accY = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8));
curCount += 2;
accZ = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8));
curCount += 2;
gyroX = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8));
curCount += 2;
gyroY = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8));
curCount += 2;
gyroZ = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8));
curCount += 2;
magX = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8));
curCount += 2;
magY = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8));
curCount += 2;
magZ = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8));
curCount += 2;
accHX = dataBytes.at(curCount);
curCount += 1;
accHY = dataBytes.at(curCount);
curCount += 1;
accHZ = dataBytes.at(curCount);
curCount += 1;
highPresAlt = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8) | (dataBytes.at(curCount + 2) << 16 ) | (dataBytes.at(curCount + 3) << 24));
curCount += 4;
gpsLat = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8) | (dataBytes.at(curCount + 2) << 16 ) | (dataBytes.at(curCount + 3) << 24));
curCount += 4;
gpsLong = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8) | (dataBytes.at(curCount + 2) << 16 ) | (dataBytes.at(curCount + 3) << 24));
curCount += 4;
gpsAlt = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8) | (dataBytes.at(curCount + 2) << 16 ) | (dataBytes.at(curCount + 3) << 24));
curCount += 4;
gpsTime = (dataBytes.at(curCount) | (dataBytes.at(curCount + 1) << 8) | (dataBytes.at(curCount + 2) << 16 ) | (dataBytes.at(curCount + 3) << 24));
curCount += 4;
}
else
{
curCount++;
}
}
}
填充QByteArray的函数是:
void MainWindow::readNoseConeAvi()
{
QByteArray noseData = serial->readAll();
QDataStream stream(noseData);
dataBytes = noseData;
inDataStream = &stream;
qDebug() << "Reading data bytes from telemetry source: ";
}
我开始制作电子表格来跟踪任何不良值,并立即发现问题。看来我在重新组装32位值时做错了,但无法弄清楚原因。 这是输出:
答案 0 :(得分:1)
问题是,QByteArray::at()
返回的char
可能会消极。在将char
添加到quint8
之前,先将其转换为quint32 myValue = quint8(dataBytes.at(curCount)) | quint8(dataBytes.at(curCount+1)) << 8 | quint8(dataBytes.at(curCount+2)) << 16 | quint8(dataBytes.at(curCount+3)) << 24;
。
quint32
这意味着您的所有转化都可能出错,而不仅仅是quint8
。请务必在其他转化中指定Tools>Refresh
类型。
我建议你创建转换功能,这样你就不必一遍又一遍地写出来。