Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2020
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.62 KB | None | 0 0
  1. #include <SimpleModbusSlave.h>
  2.  
  3. /*
  4. SimpleModbusSlaveV10 supports function 3, 6 & 16.
  5.  
  6. This example code will receive the adc ch0 value from the arduino master.
  7. It will then use this value to adjust the brightness of the led on pin 9.
  8. The value received from the master will be stored in address 1 in its own
  9. address space namely holdingRegs[].
  10.  
  11. In addition to this the slaves own adc ch0 value will be stored in
  12. address 0 in its own address space holdingRegs[] for the master to
  13. be read. The master will use this value to alter the brightness of its
  14. own led connected to pin 9.
  15.  
  16. The modbus_update() method updates the holdingRegs register array and checks
  17. communication.
  18.  
  19. Note:
  20. The Arduino serial ring buffer is 64 bytes or 32 registers.
  21. Most of the time you will connect the arduino to a master via serial
  22. using a MAX485 or similar.
  23.  
  24. In a function 3 request the master will attempt to read from your
  25. slave and since 5 bytes is already used for ID, FUNCTION, NO OF BYTES
  26. and two BYTES CRC the master can only request 58 bytes or 29 registers.
  27.  
  28. In a function 16 request the master will attempt to write to your
  29. slave and since a 9 bytes is already used for ID, FUNCTION, ADDRESS,
  30. NO OF REGISTERS, NO OF BYTES and two BYTES CRC the master can only write
  31. 54 bytes or 27 registers.
  32.  
  33. Using a USB to Serial converter the maximum bytes you can send is
  34. limited to its internal buffer which differs between manufactures.
  35. */
  36.  
  37. int A = 10;
  38.  
  39. // Using the enum instruction allows for an easy method for adding and
  40. // removing registers. Doing it this way saves you #defining the size
  41. // of your slaves register array each time you want to add more registers
  42. // and at a glimpse informs you of your slaves register layout.
  43.  
  44. //////////////// registers of your slave ///////////////////
  45. enum
  46. {
  47. // just add or remove registers and your good to go...
  48. // The first register starts at address 0
  49. ESTADO,
  50. HOLDING_REGS_SIZE // leave this one
  51. // total number of registers for function 3 and 16 share the same register array
  52. // i.e. the same address space
  53. };
  54.  
  55. unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
  56. ////////////////////////////////////////////////////////////
  57.  
  58. void setup()
  59. {
  60. /* parameters(HardwareSerial* SerialPort,
  61. long baudrate,
  62. unsigned char byteFormat,
  63. unsigned char ID,
  64. unsigned char transmit enable pin,
  65. unsigned int holding registers size,
  66. unsigned int* holding register array)
  67. */
  68.  
  69. /* Valid modbus byte formats are:
  70. SERIAL_8N2: 1 start bit, 8 data bits, 2 stop bits
  71. SERIAL_8E1: 1 start bit, 8 data bits, 1 Even parity bit, 1 stop bit
  72. SERIAL_8O1: 1 start bit, 8 data bits, 1 Odd parity bit, 1 stop bit
  73.  
  74. You can obviously use SERIAL_8N1 but this does not adhere to the
  75. Modbus specifications. That said, I have tested the SERIAL_8N1 option
  76. on various commercial masters and slaves that were suppose to adhere
  77. to this specification and was always able to communicate... Go figure.
  78.  
  79. These byte formats are already defined in the Arduino global name space.
  80. */
  81.  
  82. modbus_configure(&Serial, 9600, SERIAL_8N1, 5, 6, HOLDING_REGS_SIZE, holdingRegs);
  83.  
  84. // modbus_update_comms(baud, byteFormat, id) is not needed but allows for easy update of the
  85. // port variables and slave id dynamically in any function.
  86. //modbus_update_comms(9600, SERIAL_8N2, 1);
  87.  
  88. pinMode(A, OUTPUT);
  89. }
  90.  
  91. void loop()
  92. {
  93. // modbus_update() is the only method used in loop(). It returns the total error
  94. // count since the slave started. You don't have to use it but it's useful
  95. // for fault finding by the modbus master.
  96.  
  97. modbus_update();
  98.  
  99. digitalWrite(A, HIGH); // turn the LED on (HIGH is the voltage level)
  100. delay(1000); // wait for a second
  101. digitalWrite(A, LOW); // turn the LED off by making the voltage LOW
  102. delay(1000); // wait for a second
  103.  
  104. holdingRegs[ESTADO] = digitalRead(A); // update data to be read by the master to adjust the PWM
  105.  
  106. //analogWrite(LED, holdingRegs[PWM_VAL]>>2); // constrain adc value from the arduino master to 255
  107.  
  108. /* Note:
  109. The use of the enum instruction is not needed. You could set a maximum allowable
  110. size for holdinRegs[] by defining HOLDING_REGS_SIZE using a constant and then access
  111. holdingRegs[] by "Index" addressing.
  112. I.e.
  113. holdingRegs[0] = analogRead(A0);
  114. analogWrite(LED, holdingRegs[1]/4);
  115. */
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement