node_data_acquisition.c 3.1 KB

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