Advertisement
RuiViana

Master que Funfou

Feb 26th, 2016
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 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 3
  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(4800);
  39. }
  40. //-----------------------------------------------
  41. void loop()
  42. {
  43. static uint32_t i;
  44. uint8_t j, result;
  45. uint16_t data[6];
  46. i++;
  47. if (flag == 0)
  48. {
  49. // Serial.println("Digite o inicio: ");
  50. if(Serial.available()) // Se algo for recebido pela serial do módulo bluetooth
  51. {
  52. Start = Serial.parseInt();
  53. Serial.println(Start);
  54. flag = 1;
  55. }
  56. }
  57. if (flag == 1)
  58. {
  59. Serial.println("Digite qtos Regs: ");
  60. while(flag == 1)
  61. {
  62. if(Serial.available()) // Se algo for recebido pela serial do módulo bluetooth
  63. {
  64. Regs = Serial.parseInt();
  65. Serial.println(Regs);
  66. flag = 0;
  67. }
  68. }
  69. }
  70. digitalWrite(Ena,HIGH);
  71. // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  72. node.setTransmitBuffer(0, lowWord(i));
  73. // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  74. node.setTransmitBuffer(1, highWord(i));
  75. // slave: write TX buffer to (2) 16-bit registers starting at register 0
  76. result = node.writeMultipleRegisters(0, 1);
  77. // slave: read (6) 16-bit registers starting at register 2 to RX buffer
  78. result = node.readHoldingRegisters(Start, Regs);
  79. // digitalWrite(Ena,LOW);
  80. Serial.print("Result ");
  81. Serial.println(result);
  82. // do something with data if read is successful
  83. if (result == node.ku8MBSuccess)
  84. {
  85. for (j = 0; j < Regs; j++)
  86. {
  87. data[j] = node.getResponseBuffer(j);
  88. Serial.println(data[j]);
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement