Advertisement
RuiViana

ModBus_Master

Jan 8th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.98 KB | None | 0 0
  1. /**
  2. * Modbus master example 1:
  3. * The purpose of this example is to query an array of data
  4. * from an external Modbus slave device.
  5. * The link media can be USB or RS232.
  6. *
  7. * Recommended Modbus slave:
  8. * diagslave http://www.modbusdriver.com/diagslave.html
  9. *
  10. * In a Linux box, run
  11. * "./diagslave /dev/ttyUSB0 -b 19200 -d 8 -s 1 -p none -m rtu -a 1"
  12. * This is:
  13. * serial port /dev/ttyUSB0 at 19200 baud 8N1
  14. * RTU mode and address @1
  15. */
  16.  
  17. #include <ModbusRtu.h>
  18.  
  19. // data array for modbus network sharing
  20. uint16_t au16data[16];
  21. uint8_t u8state;
  22.  
  23. /**
  24. * Modbus object declaration
  25. * u8id : node id = 0 for master, = 1..247 for slave
  26. * u8serno : serial port (use 0 for Serial)
  27. * u8txenpin : 0 for RS-232 and USB-FTDI
  28. * or any pin number > 1 for RS-485
  29. */
  30. Modbus master(0,0,9); // this is master and RS-232 or USB-FTDI, PIN 9 PARA INDICAR QUE TRASMITO
  31.  
  32. /**
  33. * This is an structe which contains a query to an slave device
  34. */
  35. modbus_t telegram;
  36.  
  37. unsigned long u32wait;
  38.  
  39. void setup() {
  40. master.begin( 9600 ); // baud-rate at 19200
  41. master.setTimeOut( 2000 ); // if there is no answer in 2000 ms, roll over
  42. u32wait = millis() + 1000;
  43. u8state = 0;
  44. }
  45.  
  46. void loop() {
  47. switch( u8state ) {
  48. case 0:
  49. if (millis() > u32wait) u8state++; // wait state
  50. break;
  51. case 1:
  52. telegram.u8id = 1; // slave address
  53. telegram.u8fct = 5; // function code (this one is registers read)
  54. telegram.u16RegAdd = 0; // start address in slave
  55. telegram.u16CoilsNo = 2; // number of elements (coils or registers) to read
  56. //telegram.au16reg = au16data; // pointer to a memory array in the Arduino
  57.  
  58. // PARA EL VARIADOR OMRON MX1: MODBUS RTU 01 05 00 00 FF 00 -> START
  59. // 01 -> SLAVE (VFD OMRON)
  60. // 05 -> WRITE COIL
  61. // 00 00 -> ADDRESS COIL
  62. // FF 00 -> VALUES TO WRITE IN COIL FOR RUN
  63.  
  64. telegram.au16reg[0] = 0;
  65. telegram.au16reg[1] = 0;
  66. telegram.au16reg[2] = 0;
  67. telegram.au16reg[3] = 0;
  68. telegram.au16reg[4] = 0;
  69. telegram.au16reg[5] = 0;
  70. telegram.au16reg[6] = 0;
  71. telegram.au16reg[7] = 0;
  72.  
  73. telegram.au16reg[8] = 1;
  74. telegram.au16reg[9] = 1;
  75. telegram.au16reg[10] = 1;
  76. telegram.au16reg[11] = 1;
  77. telegram.au16reg[12] = 1;
  78. telegram.au16reg[13] = 1;
  79. telegram.au16reg[14] = 1;
  80. telegram.au16reg[15] = 1;
  81.  
  82.  
  83. master.query( telegram ); // send query (only once)
  84. delay(3500);
  85.  
  86. telegram.u8id = 1; // slave address
  87. telegram.u8fct = 6; // function code (this one is registers read)
  88. telegram.u16RegAdd = 1; // start address in slave
  89. telegram.u16CoilsNo = 4; // number of elements (coils or registers) to read
  90. //telegram.au16reg = au16data; // pointer to a memory array in the Arduino
  91. //telegram.au16reg = 0000000111110100;
  92.  
  93. // PARA EL VARIADOR OMRON MX1: MODBUS RTU 01 06 00 01 01 F4 -> CHANGE FQ
  94. // 01 -> SLAVE (VFD OMRON)
  95. // 06 -> WRITE IN REGISTE
  96. // 00 01 -> REGISTER ADDRESS
  97. // 01 F4 -> VALUE TO WRITE IN THE REGISTER TO CHANGE THE MOTOR FREQUENCY
  98.  
  99. telegram.au16reg[0] = 0;
  100. telegram.au16reg[1] = 0;
  101. telegram.au16reg[2] = 0;
  102. telegram.au16reg[3] = 0;
  103. telegram.au16reg[4] = 0;
  104. telegram.au16reg[5] = 0;
  105. telegram.au16reg[6] = 0;
  106. telegram.au16reg[7] = 1;
  107.  
  108. telegram.au16reg[8] = 1;
  109. telegram.au16reg[9] = 1;
  110. telegram.au16reg[10] = 1;
  111. telegram.au16reg[11] = 1;
  112. telegram.au16reg[12] = 0;
  113. telegram.au16reg[13] = 1;
  114. telegram.au16reg[14] = 0;
  115. telegram.au16reg[15] = 0;
  116.  
  117. master.query( telegram ); // send query (only once)
  118. delay(3500);
  119. telegram.u8id = 1; // slave address
  120. telegram.u8fct = 5; // function code (this one is registers read)
  121. telegram.u16RegAdd = 1; // start address in slave
  122. telegram.u16CoilsNo = 2; // number of elements (coils or registers) to read
  123. //telegram.au16reg = au16data; // pointer to a memory array in the Arduino
  124.  
  125. // PARA EL VARIADOR OMRON MX1: MODBUS RTU 01 05 00 00 00 00 -> STOP
  126. // 01 -> ESCLAVO (VARIADOR OMRON)
  127. // 05 -> ESCRIBIR EN UNA BOBINA
  128. // 00 00 -> DIRECCION DE LA BOBINA
  129. // 00 00 -> VALOR A ESCRIBIR EN LA BOBINA PARA STOP
  130.  
  131. telegram.au16reg[0] = 0;
  132. telegram.au16reg[1] = 0;
  133. telegram.au16reg[2] = 0;
  134. telegram.au16reg[3] = 0;
  135. telegram.au16reg[4] = 0;
  136. telegram.au16reg[5] = 0;
  137. telegram.au16reg[6] = 0;
  138. telegram.au16reg[7] = 0;
  139. telegram.au16reg[8] = 0;
  140. telegram.au16reg[9] = 0;
  141. telegram.au16reg[10] = 0;
  142. telegram.au16reg[11] = 0;
  143. telegram.au16reg[12] = 0;
  144. telegram.au16reg[13] = 0;
  145. telegram.au16reg[14] = 0;
  146. telegram.au16reg[15] = 0;
  147. master.query( telegram ); // send query (only once)
  148. delay(3500);
  149. u8state++;
  150. break;
  151. case 2:
  152. master.poll(); // check incoming messages
  153. if (master.getState() == COM_IDLE) {
  154. u8state = 0;
  155. u32wait = millis() + 100;
  156. }
  157. break;
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement