Arduino with modbus

Maybe it is just that I am bad at searching on google but I just can't find the correct information.

I want to control a omron varispeed v7 using my arduino.
The protocol I want to use is modbus. I already ordered max485 ic because I thought that I needed those.

The manuel of the omron can be find here:

On page 151 is all of the information of the modbus.

Hope you guys have an idea.

Search for Arduino Modbus and you will find several Modbus libraries.

Finding the libaries wasn't a problem. I think this libarie will work fine:
http://playground.arduino.cc/Code/ModbusMaster

But I don't know how to send out the registers with the bits.

You can try SimpleModbusMaster library by juanB.
It is working good for me as I'm only using it to read from slave modbus device but this library also has write function to use.
As from drive data sheet modbus address are in hex you have to convert them to decimal first to use in your code.
To make your arduino listen over modbus you need modbus to TTL converter as you have ordered.
But with my experience you need to do litlle tweaking with ur module to work with arduino.
I'm posting image of my rs485 to TTL module which I changed and make it look like shield.

Images

That looks pretty much like the thing that I have ordered:
Ebay

In the omron manuel it says that it has slave adress 01H (page 147). But I don't know how to convert it to decimal value because the online coverter says that it is invailed.

I'm sure the slave address is configurable. 01H is 1. Anything 9 and under is the same in decimal as in hex.

Look on page 144 for setting the address.

Okay, I have set the adress to 1 and now I want to send out a run command. How do I do it?
I have no idea of programming for rs485

Well slave address can be between 1-255 in modbus protocol for example if your arduino act as master modbus device which give commands to slave modbus device(omron drive), there should be a slave address defined for slave modbus because you can add more then one up to 255 similar omron drives with your arduino master and operate them simultaneously but they should have some id with master can detect which slave is on which id location.
In your case it is one as by default factory setting is 1.
With this you also need to know baud rate, parity and no. of stop bits(1 or 2) to match it with your arduino modbus master, only then both devices can communicate.

Okay I get it. This looks similair with the dmx protocol.
Baud rate and parity can all be set in the menu of the omron and the stop bits is 1 bit fixed.

Is the SimpleModBus libary good for writing to the slave or do I need to use a other libary that works better?

HERE IS EXAMPLE FROM SIMPLE_MODBUS_MASTER LIBRARY.

use function 3 to read holding register.(in hex 3 is 03H)
use function 16 to write holding register.(in hex 16 is10H)

this example code is perfect solution for your query.

SimpleModbusMasterArduino-1.ino (7.44 KB)

That code didn't work so I used the example that came with the library which is slightly different.

#include <SimpleModbusMaster.h>

/* To communicate with a slave you need to create a 
   packet that will contain all the information
   required to communicate to that slave.
   
   There are numerous counters for easy diagnostic.
   These are variables already implemented in a 
   packet. You can set and clear these variables
   as needed.
   
   There are general modbus information counters:
   requests - contains the total requests to a slave
   successful_requests - contains the total successful requests
   total_errors - contains the total errors as a sum
   timeout - contains the total time out errors
   incorrect_id_returned - contains the total incorrect id returned errors
   incorrect_function_returned - contains the total incorrect function returned errors
   incorrect_bytes_returned - contains the total incorrect bytes returned errors
   checksum_failed - contains the total checksum failed errors
   buffer_errors - contains the total buffer errors
  
   And there are modbus specific exception counters:
   illegal_function - contains the total illegal function errors
   illegal_data_address - contains the total illegal data_address errors
   illegal_data_value - contains the total illegal data value errors
   misc_exceptions - contains the total miscellaneous returned exceptions 
	
   And finally there is a variable called "connection" that 
   at any given moment contains the current connection 
   status of the packet. If true then the connection is 
   active if false then communication will be stopped
   on this packet untill the programmer sets the "connection"
   variable to true explicitly. The reason for this is 
   because of the time out involved in modbus communication.
   EACH faulty slave that's not communicating will slow down
   communication on the line with the time out value. E.g.
   Using a time out of 1500ms, if you have 10 slaves and 9 of them
   stops communicating the latency burden placed on communication
   will be 1500ms * 9 = 13,5 seconds!!!!
  
   modbus_update() returns the previously scanned false connection.
   You can use this as the index to your packet array to find out 
   if the connection has failed in that packet and then react to it. 
   You can then try to re-enable the connecion by setting the
   packet->connection attribute to true.
   The index will only be available for one loop cycle, after that
   it's cleared and ready to return the next false connection index
   if there is one else it will return the packet array size indicating
   everything is ok.
   
   All the error checking, updating and communication multitasking
   takes place in the background!
  
   In general to communicate with to a slave using modbus
   RTU you will request information using the specific
   slave id, the function request, the starting address
   and lastly the number of registers to request.
   Function 3 and 16 are supported. In addition to
   this broadcasting (id = 0) is supported on function 16.
   Constants are provided for:
   Function 3 -  READ_HOLDING_REGISTERS 
   Function 16 - PRESET_MULTIPLE_REGISTERS 
   
   The example sketch will read a packet consisting 
   of 3 registers from address 0 using function 3 from
   the GM7U LS Industrial PLC (id = 2) and then write 
   another packet containing the same 3 registers and 
   the counter information of both packets using function 16
   to address 3 in the PLC. Using the supplied PLC software
   you can then view in realtime what the values are in
   the PLC's registers.
*/   

// led to indicate that a communication error is present
#define connection_error_led 13

//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 200 // the scan rate

// If the packets internal retry register matches
// the set retry count then communication is stopped
// on that packet. To re-enable the packet you must
// set the "connection" variable to true.
#define retry_count 10 

// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2 

// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
  PACKET1,
  PACKET2,
  // leave this last entry
  TOTAL_NO_OF_PACKETS
};

// Create an array of Packets for modbus_update()
Packet packets[TOTAL_NO_OF_PACKETS];

// Create a packetPointer to access each packet
// individually. This is not required you can access
// the array explicitly. E.g. packets[PACKET1].id = 2;
// This does become tedious though...
packetPointer packet1 = &packets[PACKET1];
packetPointer packet2 = &packets[PACKET2];

// The data from the PLC will be stored
// in the regs array
unsigned int regs[9];

void setup()
{
  // read 3 registers starting at address 0
  packet1->id = 2;
  packet1->function = READ_HOLDING_REGISTERS;
  packet1->address = 0;
  packet1->no_of_registers = 3;
  packet1->register_array = regs;
  
  // write the 9 registers to the PLC starting at address 3
  packet2->id = 2;
  packet2->function = PRESET_MULTIPLE_REGISTERS;
  packet2->address = 3;
  packet2->no_of_registers = 9;
  packet2->register_array = regs;
  
  // P.S. the register_array entries above can be different arrays
  
  // Initialize communication settings etc...
  modbus_configure(baud, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS);
  
  pinMode(connection_error_led, OUTPUT);
}

void loop()
{
  unsigned int connection_status = modbus_update(packets);
  
  if (connection_status != TOTAL_NO_OF_PACKETS)
  {
    digitalWrite(connection_error_led, HIGH);
    // You could re-enable the connection by:
    //packets[connection_status].connection = true;
  }
  else
    digitalWrite(connection_error_led, LOW);
  
  // update the array with the counter data
  regs[3] = packet1->requests;
  regs[4] = packet1->successful_requests;
  regs[5] = packet1->total_errors;
  regs[6] = packet2->requests;
  regs[7] = packet2->successful_requests;
  regs[8] = packet2->total_errors; 
}

But how do I send out the things that I want, because this doen't clearify it for me at all.
Lets say I want to send the run command, see the attachment.

Firsrt of all do your rs485 to TTL is working fine, please make sure as it was the main culprit in my case.
Also I've not used function 5 and 15 that changes the bit status in slave, so let me test it first then I will share full code with TTL to modbus modification diagram.

I've had bad experiences with MAX485 chips. If you start frying them or getting frustrated try these.

http://www.serialcomm.com/serial_rs232_converters/rs232_rs485_to_ttl_converters/rs485_to_5v_ttl_converter/rs485_to_5v_ttl.product_general_info.aspx

the code I'm posting is working fine with arduino master and slave plc.
I'm sending coil status to arduino and after reading that coil status arduino write to the another bit into the plc.
readRegs[0] holds the status of coil with modbus address 19.
If status of coil(19) is HIGH(1) then it writes 1 to the coil with Modbus address 31 through writeRegs[0].

you can change your program by using one of the pin of arduino as input button and when you press that it change status of writeRegs[0].

modbus-to-output.ino (1.93 KB)