Advertisement
RuiViana

Teste_Fevereiro_ModBus_Com_Ena

Feb 23rd, 2016
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. /*
  2. Basic_Master - example using ModbusMaster library
  3. This file is part of ModbusMaster.
  4.  
  5. ModbusMaster is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9.  
  10. ModbusMaster is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14.  
  15. You should have received a copy of the GNU General Public License
  16. along with ModbusMaster. If not, see <http://www.gnu.org/licenses/>.
  17.  
  18. Written by Doc Walker (Rx)
  19. Copyright © 2009-2013 Doc Walker <4-20ma at wvfans dot net>
  20. */
  21.  
  22. #include <ModbusMaster.h>
  23.  
  24. // instantiate ModbusMaster object as slave ID 2
  25. // defaults to serial port 0 since no port was specified
  26. ModbusMaster node(1,2);
  27.  
  28. #define Ena 6
  29. byte Start = 0;
  30. byte Regs = 4;
  31. byte flag = 0;
  32. //-------------------------------------------------------
  33. void setup()
  34. {
  35. pinMode(Ena, OUTPUT);
  36. Serial.begin(9600);
  37. // initialize Modbus communication baud rate
  38. node.begin(9600);
  39. }
  40. //------------------------------------------------
  41.  
  42. void loop()
  43. {
  44. static uint32_t i;
  45. uint8_t j, result;
  46. uint16_t data[6];
  47.  
  48. i++;
  49.  
  50. if (flag == 0)
  51. {
  52. // Serial.println("Digite o inicio: ");
  53. if(Serial.available()) // Se algo for recebido pela serial do módulo bluetooth
  54. {
  55. Start = Serial.parseInt();
  56. Serial.println(Start);
  57. flag = 1;
  58. }
  59. }
  60. if (flag == 1)
  61. {
  62. Serial.println("Digite qtos Regs: ");
  63. while(flag == 1)
  64. {
  65. if(Serial.available()) // Se algo for recebido pela serial do módulo bluetooth
  66. {
  67. Regs = Serial.parseInt();
  68. Serial.println(Regs);
  69. flag = 0;
  70. }
  71. }
  72. }
  73. // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  74. node.setTransmitBuffer(0, lowWord(i));
  75.  
  76. // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  77. node.setTransmitBuffer(1, highWord(i));
  78.  
  79. // slave: write TX buffer to (2) 16-bit registers starting at register 0
  80. digitalWrite(Ena,HIGH);
  81. result = node.writeMultipleRegisters(0, 1);
  82. digitalWrite(Ena,LOW);
  83. // slave: read (6) 16-bit registers starting at register 2 to RX buffer
  84. result = node.readHoldingRegisters(Start, Regs);
  85. Serial.print("Result ");
  86. Serial.println(result);
  87. // do something with data if read is successful
  88.  
  89. if (result == node.ku8MBSuccess)
  90. {
  91. for (j = 0; j < Regs; j++)
  92. {
  93. data[j] = node.getResponseBuffer(j);
  94. Serial.println(data[j]);
  95. }
  96. }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement