Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // hubsan x4 tx frame decoder
- // input: data.txt = saleae logic spi decoder exported output.
- // output: dataout.txt = transmission data frames
- #include <QtCore/QCoreApplication>
- #include <qfile.h>
- #include <qstringlist.h>
- /*
- Ex1: 20 00 00 00 80 00 7d 00 84 02 64 db 04 26 79 7b
- Ex2: 20 00 00 00 80 00 7d 00 84 02 64 db 04 26 79 7b
- aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp
- cc : throttle observed range: 0x00 - 0xff (smaller is down)
- ee : rudder observed range: 0x34 - 0xcc (smaller is right)
- gg : elevator observed range: 0x3e - 0xbc (smaller is up)
- ii : aileron observed range: 0x45 - 0xc3 (smaller is right)
- llmmnnoo: Transmiter ID(?)
- pp : checksum
- Checksums:
- The checksum is calculated as 256 - ((sum of the 1st 15 bytes) modulo 256)
- */
- int main(int argc, char *argv[])
- {
- // 1st phase, convert text output to data binary file
- QFile in("data.txt");
- QFile::remove("data.bin");
- QFile out("data.bin");
- //int test = 256-((0xff + 0x4a + 0x36 + 0x32 + 0xfe + 0xba + 0x5e + 0x03 + 0xdc + 0x28)%256);
- //QString hh = QString::number(test,16);
- in.open(QIODevice::ReadOnly);
- out.open(QIODevice::WriteOnly);
- QString line = in.readLine(); // skip first line
- while(!in.atEnd())
- {
- line = in.readLine();
- QString data = line.split("0x")[1].left(2);
- bool ok;
- unsigned char byte = data.toInt(&ok,16);
- out.write((const char*)&byte,1);
- }
- out.close();
- in.close();
- // 2nd phase, detect data packets and dump them to text file
- out.open(QIODevice::ReadOnly);
- QByteArray buffer = out.readAll();
- out.close();
- QFile::remove("E:\\Users\\goebish\\Desktop\\dataout.txt");
- QFile out2("E:\\Users\\goebish\\Desktop\\dataout.txt");
- out2.open(QIODevice::WriteOnly);
- int pos=0;
- int line_size = 16; //16;
- int offset = 3; //3;
- out2.write(" aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp\n");
- while(pos<buffer.size()-line_size)
- {
- if( buffer.at(pos) == (char)0xA0 // start of TX packet ?
- ||
- buffer.at(pos) == (char)0x18 // start of RX packet ?
- )
- {
- out2.write("\n");
- if(buffer.at(pos) == (char)0xA0)
- out2.write("Tx: ");
- else if(buffer.at(pos) == (char)0x18)
- out2.write("Rx: ");
- int pos2 = pos+offset; // start of data;
- int dumped=0;
- while(pos2 < buffer.size() && dumped < line_size)
- {
- unsigned char data = buffer.at(pos2);
- QString hex = QString::number(data,16)+' ';
- if(data < 16)
- out2.write("0");
- out2.write(hex.toAscii());
- pos2++;
- dumped++;
- }
- }
- pos++;
- }
- out2.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment