能源管控程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.0 KiB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # ModbusMaster
  2. [![GitHub release](https://img.shields.io/github/release/4-20ma/ModbusMaster.svg?maxAge=3600)][GitHub release]
  3. [![Travis](https://img.shields.io/travis/4-20ma/ModbusMaster.svg?maxAge=3600)][Travis]
  4. [![license](https://img.shields.io/github/license/4-20ma/ModbusMaster.svg?maxAge=3600)][license]
  5. [![code of conduct](https://img.shields.io/badge/%E2%9D%A4-code%20of%20conduct-blue.svg?maxAge=3600)][code of conduct]
  6. [GitHub release]: https://github.com/4-20ma/ModbusMaster
  7. [Travis]: https://travis-ci.org/4-20ma/ModbusMaster
  8. [license]: LICENSE
  9. [code of conduct]: CODE_OF_CONDUCT.md
  10. ## Overview
  11. This is an Arduino library for communicating with Modbus slaves over RS232/485 (via RTU protocol).
  12. ## Features
  13. The following Modbus functions are available:
  14. Discrete Coils/Flags
  15. - 0x01 - Read Coils
  16. - 0x02 - Read Discrete Inputs
  17. - 0x05 - Write Single Coil
  18. - 0x0F - Write Multiple Coils
  19. Registers
  20. - 0x03 - Read Holding Registers
  21. - 0x04 - Read Input Registers
  22. - 0x06 - Write Single Register
  23. - 0x10 - Write Multiple Registers
  24. - 0x16 - Mask Write Register
  25. - 0x17 - Read Write Multiple Registers
  26. Both full-duplex and half-duplex RS232/485 transceivers are supported. Callback functions are provided to toggle Data Enable (DE) and Receiver Enable (/RE) pins.
  27. ## Installation
  28. #### Library Manager
  29. Install the library into your Arduino IDE using the Library Manager (available from IDE version 1.6.2). Open the IDE and click Sketch > Include Library > Manage Libraries…
  30. Scroll or search for `ModbusMaster`, then select the version of the library you want to install. Quit/re-launch the IDE to refresh the list; new versions are automatically added to the list, once released on GitHub.
  31. Refer to Arduino Tutorials > Libraries [Using the Library Manager](https://www.arduino.cc/en/Guide/Libraries#toc3).
  32. #### Zip Library
  33. Refer to Arduino Tutorials > Libraries [Importing a .zip Library](https://www.arduino.cc/en/Guide/Libraries#toc4).
  34. #### Manual
  35. Refer to Arduino Tutorials > Libraries [Manual Installation](https://www.arduino.cc/en/Guide/Libraries#toc5).
  36. ## Hardware
  37. This library has been tested with an Arduino [Duemilanove](http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove), PHOENIX CONTACT [nanoLine](https://www.phoenixcontact.com/online/portal/us?1dmy&urile=wcm%3apath%3a/usen/web/main/products/subcategory_pages/standard_logic_modules_p-21-03-03/3329dd38-7c6a-46e1-8260-b9208235d6fe/3329dd38-7c6a-46e1-8260-b9208235d6fe) controller, connected via RS485 using a Maxim [MAX488EPA](http://www.maxim-ic.com/quick_view2.cfm/qv_pk/1111) transceiver.
  38. ## Caveats
  39. Conforms to Arduino IDE 1.5 Library Specification v2.1 which requires Arduino IDE >= 1.5.
  40. Arduinos prior to the Mega have one serial port which must be connected to USB (FTDI) for uploading sketches and to the RS232/485 device/network for running sketches. You will need to disconnect pin 0 (RX) while uploading sketches. After a successful upload, you can reconnect pin 0.
  41. ## Support
  42. Please [submit an issue](https://github.com/4-20ma/ModbusMaster/issues) for all questions, bug reports, and feature requests. Email requests will be politely redirected to the issue tracker so others may contribute to the discussion and requestors get a more timely response.
  43. ## Example
  44. The library contains a few sketches that demonstrate use of the `ModbusMaster` library. You can find these in the [examples](https://github.com/4-20ma/ModbusMaster/tree/master/examples) folder.
  45. ``` cpp
  46. /*
  47. Basic.pde - example using ModbusMaster library
  48. Library:: ModbusMaster
  49. Author:: Doc Walker <4-20ma@wvfans.net>
  50. Copyright:: 2009-2016 Doc Walker
  51. Licensed under the Apache License, Version 2.0 (the "License");
  52. you may not use this file except in compliance with the License.
  53. You may obtain a copy of the License at
  54. http://www.apache.org/licenses/LICENSE-2.0
  55. Unless required by applicable law or agreed to in writing, software
  56. distributed under the License is distributed on an "AS IS" BASIS,
  57. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  58. See the License for the specific language governing permissions and
  59. limitations under the License.
  60. */
  61. #include <ModbusMaster.h>
  62. // instantiate ModbusMaster object
  63. ModbusMaster node;
  64. void setup()
  65. {
  66. // use Serial (port 0); initialize Modbus communication baud rate
  67. Serial.begin(19200);
  68. // communicate with Modbus slave ID 2 over Serial (port 0)
  69. node.begin(2, Serial);
  70. }
  71. void loop()
  72. {
  73. static uint32_t i;
  74. uint8_t j, result;
  75. uint16_t data[6];
  76. i++;
  77. // set word 0 of TX buffer to least-significant word of counter (bits 15..0)
  78. node.setTransmitBuffer(0, lowWord(i));
  79. // set word 1 of TX buffer to most-significant word of counter (bits 31..16)
  80. node.setTransmitBuffer(1, highWord(i));
  81. // slave: write TX buffer to (2) 16-bit registers starting at register 0
  82. result = node.writeMultipleRegisters(0, 2);
  83. // slave: read (6) 16-bit registers starting at register 2 to RX buffer
  84. result = node.readHoldingRegisters(2, 6);
  85. // do something with data if read is successful
  86. if (result == node.ku8MBSuccess)
  87. {
  88. for (j = 0; j < 6; j++)
  89. {
  90. data[j] = node.getResponseBuffer(j);
  91. }
  92. }
  93. }
  94. ```
  95. _Project inspired by [Arduino Modbus Master](http://sites.google.com/site/jpmzometa/arduino-mbrt/arduino-modbus-master)._
  96. ## License & Authors
  97. - Author:: Doc Walker ([4-20ma@wvfans.net](mailto:4-20ma@wvfans.net))
  98. - Author:: Ag Primatic ([agprimatic@gmail.com](mailto:agprimatic@gmail.com))
  99. - Author:: Marius Kintel ([marius@kintel.net](mailto:marius@kintel.net))
  100. ```
  101. Copyright:: 2009-2016 Doc Walker
  102. Licensed under the Apache License, Version 2.0 (the "License");
  103. you may not use this file except in compliance with the License.
  104. You may obtain a copy of the License at
  105. http://www.apache.org/licenses/LICENSE-2.0
  106. Unless required by applicable law or agreed to in writing, software
  107. distributed under the License is distributed on an "AS IS" BASIS,
  108. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  109. See the License for the specific language governing permissions and
  110. limitations under the License.
  111. ```