Advertisement
RuiViana

Teste Modbus

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