16,819
社区成员




void Widget::readMyCom() //读取串口数据并显示出来
{
QByteArray temp = myCom->readAll();
qDebug() << "==========start=========";
for(int i=2; i<sizeof(temp); i++)
{
qDebug() << "Loop i=: "<<i;
if((temp[i-2]==174)&&(temp[i-1]==234))//AE=174,EA=234
{
qDebug()<<"Head found";
if(temp.at(i+6)==(temp.at(i)+temp.at(i+1)+temp.at(i+2)+temp.at(i+3)+temp.at(i+4)+temp.at(i+5)))//Check sum
{
qDebug()<<"Check SUM All Right";
qint16 x,y,z;
x=(unsigned char)temp.at(i+1);//X的高位
x = x<<8;; //移到高位
x |= (unsigned char)temp.at(i);//加上X的低位
qDebug() << "X=: "<<x;
y=(unsigned char)temp.at(i+3);//y的高位
y = y<<8;; //移到高位
y |= (unsigned char)temp.at(i+2);//加上y的低位
qDebug() << "Y=: "<<y;
z=(unsigned char)temp.at(i+5);//z的高位
z = z<<8;; //移到高位
z |= (unsigned char)temp.at(i+4);//加上z的低位
qDebug() << "Z=: "<<z;
//i+=7;
qDebug()<<"Date Transformed";
}
else
{
qDebug()<<"Check SUM Failed";
}
}
else
{
qDebug()<<"Head is not right";
qDebug() << "Loop i=: "<<i;
qDebug() << "======================";
}
}
//*/
//读取串口缓冲区的所有数据给临时变量temp
//ui->textBrowser->insertPlainText(temp);
//将串口的数据显示在窗口的文本浏览器中
}
#include <QtCore/QCoreApplication>
#include <QByteArray>
#include <qdebug.h>
class Data
{
private:
QByteArray temp;
QList<QByteArray> items;
QList<QList<int> > data;
void doItems();
void doData();
public:
Data(QByteArray rd);
QList<QByteArray> getItems();
QList<QList<int> > getData();
};
Data::Data(QByteArray rd)
{
temp = rd;
doItems();
doData();
}
void Data::doItems()
{
unsigned char* s = (unsigned char*)temp.data();
for(int i=0; i<temp.length(); i++)
{
if(*s != 0xEA)
{
s++;
continue;
}
unsigned char* s2 = s;
if(i+8 > temp.length()-1)
return;
s2 += 8;
if(*s2 != 0xAE)
{
s++;
continue;
}
unsigned char *buff = new unsigned char[9];
memcpy(buff, s, 9);
QByteArray ba((const char*)buff, 9);
items.append(ba);
s += 8;
i += 8;
}
}
void Data::doData()
{
foreach(QByteArray ba, items)
{
char sum=0;
for(int i=1; i<7; i++)
{
sum += ba.at(i);
}
Q_ASSERT(sum == ba.at(7));
QList<int> li;
for(int i=1; i<7; i+=2)
{
int x = (unsigned char)ba.at(i+1);
x = x<<8;
x |= ba.at(i);
li.append(x);
}
data.append(li);
}
}
QList<QByteArray> Data::getItems()
{
return items;
}
QList<QList<int> > Data::getData()
{
return data;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QByteArray temp = QByteArray::fromHex("458789EAEBFFF9FF560038AE88FDEAEBFFF9FF560038AE67");
Data d(temp);
foreach(QList<int> li, d.getData())
{
qDebug()<<"X:"<<li.at(0)<<"Y:"<<li[1]<<"Z:"<<li[2];
}
return a.exec();
}
char sum=0;
for(int i=1; i<7; i++)
{
sum += temp.at(i);
}
Q_ASSERT(sum == temp.at(7));// sum=0x38
char* cs = temp.data();
cs++;//去掉包头
qint16 buff[3];
memcpy(buff,cs,6);复制数据
qint16 x;
x=(unsigned char)temp.at(2);//X的高位
x = x<<8;; //移到高位
x |= (unsigned char)temp.at(1);//加上X的低位
qDebug()<<x;
char* cs = temp.data();
cs++;
for(int i=0;i<3;i++)
{
qint16* t = (qint16*)cs;
qDebug()<<*t;
cs+=2;
}