node_data_acquisition.c 2.8 KB

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