Modbus slave timeout error

Hi guys, i connect my arduino YUN to a TTL to RS232 adapter and then use a USB to RS232 adapter to PC. The code is using:
https://code.google.com/p/simple-modbus/

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

#include <SimpleModbusSlave.h>

/* This example code will receive the adc ch0 value from the arduino master. 
   It will then use this value to adjust the brightness of the led on pin 9.
   The value received from the master will be stored in address 1 in its own
   address space namely holdingRegs[].
   
   In addition to this the slaves own adc ch0 value will be stored in 
   address 0 in its own address space holdingRegs[] for the master to
   be read. The master will use this value to alter the brightness of its
   own led connected to pin 9.
   
   The modbus_update() method updates the holdingRegs register array and checks
   communication.

   Note:  
   The Arduino serial ring buffer is 64 bytes or 32 registers.
   Most of the time you will connect the arduino to a master via serial
   using a MAX485 or similar.
 
   In a function 3 request the master will attempt to read from your
   slave and since 5 bytes is already used for ID, FUNCTION, NO OF BYTES
   and two BYTES CRC the master can only request 58 bytes or 29 registers.
 
   In a function 16 request the master will attempt to write to your 
   slave and since a 9 bytes is already used for ID, FUNCTION, ADDRESS, 
   NO OF REGISTERS, NO OF BYTES and two BYTES CRC the master can only write
   54 bytes or 27 registers.
 
   Using a USB to Serial converter the maximum bytes you can send is 
   limited to its internal buffer which differs between manufactures. 
*/

#define  LED 13  

// Using the enum instruction allows for an easy method for adding and 
// removing registers. Doing it this way saves you #defining the size 
// of your slaves register array each time you want to add more registers
// and at a glimpse informs you of your slaves register layout.

//////////////// registers of your slave ///////////////////
enum 
{     
  // just add or remove registers and your good to go...
  // The first register starts at address 0
  ADC_VAL,     
  PWM_VAL,        
  HOLDING_REGS_SIZE // leave this one
  // total number of registers for function 3 and 16 share the same register array
  // i.e. the same address space
};

unsigned int holdingRegs[HOLDING_REGS_SIZE]; // function 3 and 16 register array
////////////////////////////////////////////////////////////

void setup()
{
  /* parameters(HardwareSerial* SerialPort,
                long baudrate, 
		unsigned char byteFormat,
                unsigned char ID, 
                unsigned char transmit enable pin, 
                unsigned int holding registers size,
                unsigned int* holding register array)
  */
  
  /* Valid modbus byte formats are:
     SERIAL_8N2: 1 start bit, 8 data bits, 2 stop bits
     SERIAL_8E1: 1 start bit, 8 data bits, 1 Even parity bit, 1 stop bit
     SERIAL_8O1: 1 start bit, 8 data bits, 1 Odd parity bit, 1 stop bit
     
     You can obviously use SERIAL_8N1 but this does not adhere to the
     Modbus specifications. That said, I have tested the SERIAL_8N1 option 
     on various commercial masters and slaves that were suppose to adhere
     to this specification and was always able to communicate... Go figure.
     
     These byte formats are already defined in the Arduino global name space. 
  */
   Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }	
  modbus_configure(&Serial1, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);
 
  // modbus_update_comms(baud, byteFormat, id) is not needed but allows for easy update of the
  // port variables and slave id dynamically in any function.
  //modbus_update_comms(9600, SERIAL_8N2, 1);
  
  pinMode(LED, OUTPUT);
}

void loop()
{
  // modbus_update() is the only method used in loop(). It returns the total error
  // count since the slave started. You don't have to use it but it's useful
  // for fault finding by the modbus master.
  
  modbus_update();
  
  //holdingRegs[ADC_VAL] = analogRead(A0); // update data to be read by the master to adjust the PWM
  
  analogWrite(LED, holdingRegs[PWM_VAL]>>2); // constrain adc value from the arduino master to 255
  
  /* Note:
     The use of the enum instruction is not needed. You could set a maximum allowable
     size for holdinRegs[] by defining HOLDING_REGS_SIZE using a constant and then access 
     holdingRegs[] by "Index" addressing. 
     I.e.
     holdingRegs[0] = analogRead(A0);
     analogWrite(LED, holdingRegs[1]/4);
  */
  
}

After that I use one Modbus Master simulator to check it: got a timeout error at last..Is there anything wrong with this?

Remove these lines, they just slow everything down:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

This part doesn't compile for me:

  modbus_configure(&Serial1, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);

which is correct because the definition is

void modbus_configure(HardwareSerial* SerialPort,
											long baud,
											unsigned char byteFormat,
											unsigned int _timeout, 
											unsigned int _polling, 
											unsigned char _retry_count, 
											unsigned char _TxEnablePin, 
											Packet* _packets, 
											unsigned int _total_no_of_packets)

I guess you're using an old version. Why are you using an old version and which version are you using?

After that I use one Modbus Master simulator to check it: got a timeout error at last..Is there anything wrong with this?

What configuration did you use there?

Hi guys, i connect my arduino YUN to a TTL to RS232 adapter and then use a USB to RS232 adapter to PC.

Post a wiring diagram or sharp photo of your hardware setup.

Hi pylon,

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);

is to test the softwareSerial port, not related with modbus and I already remove it.

modbus_configure(&Serial1, 9600, SERIAL_8N1, 1, 2, HOLDING_REGS_SIZE, holdingRegs);,initial is using &Serial but my arduino is YUN which same with leonard, need to modify the &Serial to &Serial 1 in order to compile;

I'm using "SimpleModbusSlaveV9"

the configuration at PC simulator and the wiring diagram is in attachment.

Thank you very much.

1.png

I'm using "SimpleModbusSlaveV9"

Then your code won't compile. Version 9 has the same definition of modbus_configure() as version 10, so your call is wrong for that version too. If that code compiles for you, you mixed up different versions of the library and the result may be unpredictable.

the configuration at PC simulator and the wiring diagram is in attachment.

On your PC your configuring the serial interface for 8E1, while in the sketch you're specifying 8N1. These values must be consistent.