logserialport.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "logserialport.h"
  2. #include <QCoreApplication>
  3. #include <QByteArray>
  4. #include <pqQtlib/utils/pqutilsfunc.h>
  5. #include <pqQtlib/utils/pqendian.h>
  6. #include "cstring"
  7. LogSerialPort::LogSerialPort()
  8. {
  9. buchang = 0;
  10. min = 0;
  11. max = 999;
  12. ret = QByteArray::fromHex("010300000006C5C8");
  13. }
  14. LogSerialPort::~LogSerialPort()
  15. {
  16. }
  17. float LogSerialPort::toFloat(const QByteArray & ray){
  18. char ch[4] = {0};
  19. ch[0] = ray.at(2);
  20. ch[1] = ray.at(3);
  21. ch[2] = ray.at(0);
  22. ch[3] = ray.at(1);
  23. float value = qFromBigEndian<float>(reinterpret_cast<EndianPointType *>(&ch[0]));
  24. // memcpy(&value,&ch[0],4);
  25. return value;
  26. }
  27. bool LogSerialPort::needSend()
  28. {
  29. return true;
  30. }
  31. QByteArray LogSerialPort::sendData()
  32. {
  33. return ret;
  34. }
  35. void LogSerialPort::handle(QByteArray & data)
  36. {
  37. doHandle(data);
  38. }
  39. void LogSerialPort::doHandle(const QByteArray & data)
  40. {
  41. if(data.at(1) != 0x03) return;
  42. int l = data.length() - 2;
  43. QByteArray td = data.right(2);
  44. int crcc = static_cast<uchar>(td.at(1));
  45. crcc = crcc << 8;
  46. crcc = crcc | static_cast<uchar>(td.at(0));
  47. int scrc = PQ::CRC(data, l);
  48. if(scrc != crcc) {
  49. qDebug()<< " RTC CRC erro : should be : " << scrc << " rev is : " << crcc << "\n"
  50. << "data is : " << data.toHex();
  51. return;
  52. }
  53. QByteArray dt;
  54. dt = data.mid(5,4);
  55. if(dt.isEmpty()) return;
  56. needReply = true;
  57. float v = toFloat(dt);
  58. double rv = v + buchang;
  59. if(rv > max){
  60. rv = max;
  61. }
  62. if(rv < min){
  63. rv = min;
  64. }
  65. GlobalInfo::this_()->currentWidght = rv;
  66. }