pqrtuhelper.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include "pqrtuhelper.h"
  2. namespace PQ {
  3. RTUHelper::RTUHelper():
  4. readType(SubAddress),toReadlen(0),_checkSubAddress(false)
  5. {
  6. }
  7. RTUHelper::~RTUHelper(){}
  8. void RTUHelper::readed(const QByteArray &data)
  9. {
  10. int index = 0;
  11. while (index < data.length())
  12. {
  13. const char ch = data.at(index);
  14. switch (readType)
  15. {
  16. case SubAddress:
  17. {
  18. ++ index;
  19. if (!_checkSubAddress || _subAddress.contains(ch))
  20. {
  21. if(!buffer.isEmpty()) buffer.clear();
  22. buffer.append(ch);
  23. readType = GetType;
  24. }
  25. continue;
  26. }
  27. case GetType:
  28. {
  29. buffer.append(ch);
  30. ++ index;
  31. readType = NumLength;
  32. continue;
  33. }
  34. case NumLength: //需要读数据
  35. {
  36. ++ index;
  37. toReadlen = static_cast<uchar>(ch);
  38. buffer.append(ch);
  39. readType = ReadData;
  40. continue;
  41. }
  42. case ReadData: //需要读数据
  43. {
  44. const int canread = data.length() - index;
  45. if (canread >= toReadlen)
  46. {
  47. buffer.append(data.mid(index, toReadlen));
  48. index += toReadlen;
  49. toReadlen = 0;
  50. readType = CRCUL;
  51. }
  52. else
  53. {
  54. buffer.append(data.mid(index, canread));
  55. index += canread;
  56. toReadlen -= canread;
  57. }
  58. continue;
  59. }
  60. case CRCUL: //需要读数据
  61. {
  62. buffer.append(ch);
  63. index ++;
  64. readType = CRCUH;
  65. continue;
  66. }
  67. case CRCUH: //需要读数据
  68. {
  69. buffer.append(ch);
  70. index ++;
  71. readType = SubAddress;
  72. doHandle(this->buffer);
  73. buffer.clear();
  74. }
  75. break;
  76. default:
  77. {
  78. readType = SubAddress;
  79. continue;
  80. }
  81. }
  82. }
  83. }
  84. }