Guest User

Untitled

a guest
Dec 27th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.50 KB | None | 0 0
  1. // hubsan x4 tx frame decoder
  2. // input: data.txt = saleae logic spi decoder exported output.
  3. // output: dataout.txt = transmission data frames
  4.  
  5. #include <QtCore/QCoreApplication>
  6. #include <qfile.h>
  7. #include <qstringlist.h>
  8. /*
  9. Ex1: 20 00 00 00 80 00 7d 00 84 02 64 db 04 26 79 7b
  10. Ex2: 20 00 00 00 80 00 7d 00 84 02 64 db 04 26 79 7b
  11.      aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp
  12. cc : throttle observed range: 0x00 - 0xff (smaller is down)
  13. ee : rudder observed range: 0x34 - 0xcc (smaller is right)
  14. gg : elevator observed range: 0x3e - 0xbc (smaller is up)
  15. ii : aileron observed range: 0x45 - 0xc3 (smaller is right)
  16. llmmnnoo: Transmiter ID(?)
  17. pp : checksum
  18. Checksums:
  19. The checksum is calculated as 256 - ((sum of the 1st 15 bytes) modulo 256)
  20. */
  21. int main(int argc, char *argv[])
  22. {
  23.     // 1st phase, convert text output to data binary file
  24.     QFile in("data.txt");
  25.     QFile::remove("data.bin");
  26.     QFile out("data.bin");
  27.  
  28.     //int test = 256-((0xff + 0x4a + 0x36 + 0x32 + 0xfe + 0xba + 0x5e + 0x03 + 0xdc + 0x28)%256);
  29.     //QString hh = QString::number(test,16);
  30.  
  31.     in.open(QIODevice::ReadOnly);
  32.     out.open(QIODevice::WriteOnly);
  33.     QString line = in.readLine(); // skip first line
  34.     while(!in.atEnd())
  35.     {
  36.         line = in.readLine();
  37.         QString data = line.split("0x")[1].left(2);
  38.         bool ok;
  39.         unsigned char byte = data.toInt(&ok,16);
  40.         out.write((const char*)&byte,1);
  41.     }
  42.     out.close();
  43.     in.close();
  44.  
  45.     // 2nd phase, detect data packets and dump them to text file
  46.     out.open(QIODevice::ReadOnly);
  47.     QByteArray buffer = out.readAll();
  48.     out.close();
  49.     QFile::remove("E:\\Users\\goebish\\Desktop\\dataout.txt");
  50.     QFile out2("E:\\Users\\goebish\\Desktop\\dataout.txt");
  51.     out2.open(QIODevice::WriteOnly);
  52.     int pos=0;
  53.    
  54.     int line_size = 16; //16;
  55.     int offset = 3; //3;
  56.  
  57.     out2.write("    aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp\n");
  58.     while(pos<buffer.size()-line_size)
  59.     {
  60.         if( buffer.at(pos)   == (char)0xA0 // start of TX packet ?
  61.         ||
  62.         buffer.at(pos)   == (char)0x18  // start of RX packet ?
  63.         )
  64.         {
  65.             out2.write("\n");
  66.             if(buffer.at(pos) == (char)0xA0)
  67.                 out2.write("Tx: ");
  68.             else if(buffer.at(pos) == (char)0x18)
  69.                 out2.write("Rx: ");
  70.  
  71.             int pos2 = pos+offset; // start of data;
  72.             int dumped=0;
  73.             while(pos2 < buffer.size() && dumped < line_size)
  74.             {
  75.                 unsigned char data = buffer.at(pos2);
  76.                 QString hex = QString::number(data,16)+' ';
  77.                 if(data < 16)
  78.                     out2.write("0");
  79.                 out2.write(hex.toAscii());
  80.                 pos2++;
  81.                 dumped++;
  82.             }
  83.         }
  84.         pos++;
  85.     }
  86.     out2.close();
  87. }
Advertisement
Add Comment
Please, Sign In to add comment