Advertisement
RuiViana

New_ModBus_Master

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