I am looking to use the Arduino as a MODBUS master to control a couple of MODBUS slaved actuators or two . Currently I am sending serial commands to the actuators via hyperterminal and can get them to move based on the manufacturer's specified codes. This is via RS-232 from the PC and I have a 232 to 485 converter in between the PC and the actuator which I like to eliminate as well.
That's nice to hear and means that you have already tested your connection. All you need is to put it in your Arduino. XD
From what I see in your code, there is a modbus_t structure which will construct the query and format with CRC. How does it deal with number of registers and bytes of data that vary between various commands? Maybe I am missing that part as I looked thru the code.
The modbus_t structure emulates what most industrial PLCs do:
- it identifies the slave that shall be accessed. It was "1" in your query. This is telegram.u8id in my example;
- it points to the first slave register to be accessed. It was 0x9900 in your query. This is telegram.u16RegAdd;
- it specifies how many registers or coils shall take place. It was 0x0007 in your query. This is telegram.u16CoilsNo;
- it declares which kind of access shall be done (read registers, read coils and so on). It was 0x10. This is telegram.u8fct;
- and finally it points to a memory link in order to transfer memory from or to the network. You have written several values in your query. This is telegram.au16reg and all these values would be somewhere of this pointer.
The CRC is automatically appended to the outcoming telegram before any transmission.
All the values you have added in the query message would have been borrowed from the pointer dm in my example. Previously you should have filled them somewhere else in the code.
Whenever com.query( telegram ) is executed, the Modbus object fills all the fields of the Modbus telegram as you manually do, appends memory if needed (because of function 0x10 or 0x06), adds the CRC and transfers it through Arduino UART.
After executing the com.query( telegram ), there must be a wait state in order to get and answer from the slave. Sometimes this answer also includes real-time data from your slave (i.e. actual position or status).
I noticed in your code you can specify three serial ports, but not sure how you define pins to them or change them to 485 since I am under the impression that this requires an interface chip. I like to use RS-485 from serial pins other than pin 0 and 1.
I'm afraid that there is a misunderstanding here. The code refers to Serial, Serial1, Serial2 and Serial3, which are available in Arduino Mega. It also supports Arduino Duemilia or Uno (328P based), Leornado and even other microcontrollers like ATMEGA1284P. The Arduino Duemilia or Uno only has Serial and this means that this library shall use your pins 0 and 1 for the RS-485 port.
how you define pins to them or change them to 485 since I am under the impression that this requires an interface chip
RS-485 needs an interface chip which can be: ST485, MAX483 or SN75LBC184P. This chip needs to tie pins 2 and 3 in order to set it to receive or transmit mode. This mode must be directly controlled from the Arduino with one of your digital outputs and must be defined when creating the Modbus object. In order to allow you to upload code to the Arduino, it is particularly useful to put a pull-up resistor between this line and VCC.