Advertisement
RuiViana

Modbus_Master_select

Mar 14th, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.89 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_Mod.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,1,7); // 1o. = Serial 2o. = Address slave 3o. = Pino de Ema no Master
  27. unsigned int Cmd = 0;
  28. unsigned int Start = 0;
  29. unsigned int Regs = 0;
  30. unsigned long Data1 = 0;
  31. unsigned long Data2 = 0;
  32. byte flag = 0;
  33.  
  34. static uint32_t i;
  35. uint8_t j, result;
  36. uint16_t data[32];
  37.  
  38. //-------------------------------------------------------
  39. void setup()
  40. {
  41. Serial.begin(9600);
  42. // initialize Modbus communication baud rate
  43. node.begin(9600); // era 4800
  44. }
  45. //--------------------------------------------------------
  46. void RecebeSerial(String CmdStr, String StartStr, String QtdeStr)
  47. {
  48. Serial.println(CmdStr);
  49. Serial.println(StartStr);
  50. while (flag == 1)
  51. {
  52. while(Serial.available()) // Se algo for recebido pela serial
  53. {
  54. Start = Serial.parseInt();
  55. Serial.println(Start);
  56. flag = 2;
  57. }
  58. }
  59. Serial.println(QtdeStr);
  60. while (flag == 2)
  61. {
  62. while(Serial.available()) // Se algo for recebido pela serial
  63. {
  64. Regs = Serial.parseInt();
  65. Serial.println(Regs);
  66. flag = 0;
  67. }
  68. }
  69. }
  70. //-----------------------------------------------
  71. void LeDados()
  72. {
  73. Serial.print("Result ");
  74. Serial.println(result);
  75. if (result == node.ku8MBSuccess)
  76. {
  77. for (j = 0; j < Regs; j++)
  78. {
  79. data[j] = node.getResponseBuffer(j);
  80. Serial.println(data[j]);
  81. }
  82. }
  83. }
  84. //-----------------------------------------------
  85. void loop()
  86. {
  87. Serial.println("Digite o comando: ");
  88. while(flag != 1)
  89. {
  90. if (flag == 0)
  91. {
  92. while(Serial.available()) // Se algo for recebido pela serial
  93. {
  94. Cmd = Serial.parseInt();
  95. flag = 1;
  96. }
  97. }
  98. }
  99. node.setTransmitBuffer(0, lowWord(Data1));
  100. node.setTransmitBuffer(1, highWord(Data2));
  101.  
  102. switch(Cmd)
  103. {
  104. //--------------------------------------------------------------
  105. case 1:
  106. {
  107. String Str1 = "Read Coils";
  108. String Str2 = "Digite Endereco Inicial do Coil:";
  109. String Str3 = "Digite Quantidade de Coils:";
  110. RecebeSerial(Str1,Str2,Str3);
  111. result = node.readCoils(Start, Regs); // 0x01 - Read Coils
  112. LeDados();
  113. break;
  114. }
  115. //--------------------------------------------------------------
  116. case 2:
  117. {
  118. String Str1 = "Read Discrete Inputs";
  119. String Str2 = "Digite Endereco Inicial da Input:";
  120. String Str3 = "Digite Quantidade de Input:";
  121. RecebeSerial(Str1,Str2,Str3);
  122. result = node.readDiscreteInputs(Start, Regs); // 0x02 - Read Discrete Inputs
  123. LeDados();
  124. break;
  125. }
  126. //-----------------------------------------------------------------------------------
  127. case 5:
  128. {
  129. String Str1 = "Write Single Coil";
  130. String Str2 = "Digite Endereco Inicial do Coil:";
  131. String Str3 = "Digite Enter";
  132. RecebeSerial(Str1,Str2,Str3);
  133. result = node.writeSingleCoil(Start, Regs); // 0x05 - Write Single Coil
  134. LeDados();
  135. break;
  136. }
  137. //-----------------------------------------------------------------------------------
  138. case 15:
  139. {
  140. String Str1 = "Write Multiple Coils";
  141. String Str2 = "Digite Endereco Inicial do Coil:";
  142. String Str3 = "Digite Enter";
  143. RecebeSerial(Str1,Str2,Str3);
  144. result = node.writeMultipleCoils(Start, Regs); // 0x0F - Write Multiple Coils (Dec 15)
  145. LeDados();
  146. break;
  147. }
  148. //-----------------------------------------------------------------------------------
  149. case 3:
  150. {
  151. String Str1 = "Read Holding Registers";
  152. String Str2 = "Digite Endereco Inicial dos Registers:";
  153. String Str3 = "Digite Quantidade de Registers:";
  154. RecebeSerial(Str1,Str2,Str3);
  155. result = node.readHoldingRegisters(Start, Regs); // 0x03 - Read Holding Registers
  156. LeDados();
  157. break;
  158. }
  159. //-----------------------------------------------------------------------------------
  160. case 4:
  161. {
  162. String Str1 = "Read Input Registers";
  163. String Str2 = "Digite Endereco Inicial dos Registers:";
  164. String Str3 = "Digite Quantidade de Registers:";
  165. RecebeSerial(Str1,Str2,Str3);
  166. result = node.readInputRegisters(Start, Regs); // 0x04 - Read Input Registers
  167. LeDados();
  168. break;
  169. }
  170. //-----------------------------------------------------------------------------------
  171. case 6:
  172. {
  173. String Str1 = "Write Single Registers";
  174. String Str2 = "Digite Endereco Inicial dos Registers:";
  175. String Str3 = "Digite Quantidade de Registers:";
  176. RecebeSerial(Str1,Str2,Str3);
  177. result = node.writeSingleRegister(Start, Regs); // 0x06 - Write Single Register
  178. LeDados();
  179. break;
  180. }
  181. //-----------------------------------------------------------------------------------
  182. case 16:
  183. {
  184. String Str1 = "Write Multiple Registers";
  185. String Str2 = "Digite Endereco Inicial dos Registers:";
  186. String Str3 = "Digite Quantidade de Registers:";
  187. RecebeSerial(Str1,Str2,Str3);
  188. result = node.writeMultipleRegisters(Start, Regs); // 0x10 - Write Multiple Registers (Dec 16)
  189. LeDados();
  190. break;
  191. }
  192. //-----------------------------------------------------------------------------------
  193. case 17:
  194. {
  195. String Str1 = "Mask Write Register";
  196. String Str2 = "Digite Endereco Inicial dos Registers:";
  197. String Str3 = "Digite Quantidade de Registers:";
  198. RecebeSerial(Str1,Str2,Str3);
  199. // result = node.maskWriteRegister(); // 0x16 - Mask Write Register (Dec 17)
  200. LeDados();
  201. break;
  202. }
  203. //-----------------------------------------------------------------------------------
  204. case 18:
  205. {
  206. String Str1 = "Read Write Multiple Registers";
  207. String Str2 = "Digite Endereco Inicial dos Registers:";
  208. String Str3 = "Digite Quantidade de Registers:";
  209. RecebeSerial(Str1,Str2,Str3);
  210. result = node.readWriteMultipleRegisters(Start, Regs); // 0x17 - Read Write Multiple Registers (Dec 18)
  211. LeDados();
  212. break;
  213. }
  214. }
  215.  
  216. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement