node_data_acquisition.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "node_data_acquisition.h"
  2. #include "node_message.h"
  3. #include <math.h>
  4. uint8_t protocol_485; //485回调函数会调用那个callback dlt645还是modbus dlt645为2 modbus为1
  5. void data_acquisition(void)
  6. {
  7. NODE_DEVICE_PARAMS *node;
  8. node = get_node_receive_params();
  9. int i = 0;
  10. while (i != 20)
  11. {
  12. if (node->params[i].protcol == MODBUS_READ)
  13. {
  14. protocol_485=1;
  15. uint16_t data[node->params[i].modbus_read->registerLength / 2]; // modbus寄存器长度
  16. mmodbus_set16bitOrder(node->params[i].modbus_read->bigLittleFormat);
  17. if (node->params[i].modbus_read->functionCode == 0x03)
  18. {
  19. bool success = mmodbus_readHoldingRegisters16i(node->params[i].modbus_read->slaveAddress,
  20. node->params[i].modbus_read->registerAddress,
  21. node->params[i].modbus_read->registerLength / 2,
  22. data);
  23. if (success)
  24. {
  25. uint32_t value;
  26. if (node->params[i].modbus_read->registerLength == 4)
  27. {
  28. value = (uint32_t)data[0] | data[1];
  29. node->params[i].modbus_read->rxLen = 4;
  30. }
  31. else if (node->params[i].modbus_read->registerLength == 2)
  32. {
  33. value = data[0];
  34. node->params[i].modbus_read->rxLen = 4;
  35. }
  36. if (node->params[i].modbus_read->precision == 0)
  37. {
  38. node->params[i].modbus_read->callback[0] = value;
  39. node->params[i].modbus_read->callback[1] = value << 8;
  40. node->params[i].modbus_read->callback[2] = value << 16;
  41. node->params[i].modbus_read->callback[3] = value << 24;
  42. }
  43. else
  44. {
  45. float convertedValue = (float)value / pow(10, node->params[i].modbus_read->precision);
  46. memcpy(node->params[i].modbus_read->callback, &convertedValue, 4);
  47. }
  48. }
  49. }
  50. }
  51. else if (node->params[i].protcol == MODBUS_WRITE)
  52. {
  53. protocol_485=1;
  54. }
  55. else if (node->params[i].protcol == DLT645_07 || node->params[i].protcol == DLT645_97)
  56. {
  57. protocol_485=2;
  58. uint8_t read_buf[10];
  59. node->params[i].dlt645_params->rxLen = 0;
  60. memset(read_buf, 0, 10);
  61. memset(node->params[i].dlt645_params->value, 0, 10);
  62. dlt645_set_addr(&dlt645, node->params[i].dlt645_params->deviceType645);
  63. int8_t rs;
  64. if (node->params[i].protcol == DLT645_07)
  65. {
  66. rs = dlt645_read_data(&dlt645, node->params[i].dlt645_params->dataType645, read_buf, DLT645_2007);
  67. }
  68. else if (node->params[i].protcol == DLT645_97)
  69. {
  70. rs = dlt645_read_data(&dlt645, node->params[i].dlt645_params->dataType645, read_buf, DLT645_1997);
  71. }
  72. if (rs != -1)
  73. {
  74. if (rs <= 4)
  75. {
  76. memcpy(node->params[i].dlt645_params->value, read_buf, 4);
  77. node->params[i].dlt645_params->rxLen = 4;
  78. }
  79. else if (rs == 5)
  80. {
  81. memcpy(node->params[i].dlt645_params->value, read_buf, 5);
  82. node->params[i].dlt645_params->rxLen = 5;
  83. }
  84. else if (rs > 5)
  85. {
  86. memcpy(node->params[i].dlt645_params->value, read_buf, 9);
  87. node->params[i].dlt645_params->rxLen = 9;
  88. }
  89. }
  90. else
  91. {
  92. node->params[i].dlt645_params->rxLen =0;
  93. }
  94. }
  95. i++;
  96. }
  97. }