View previous topic :: View next topic |
Author |
Message |
hayee
Joined: 05 Sep 2007 Posts: 252
|
Modbus timeout problem |
Posted: Thu Apr 26, 2012 1:38 am |
|
|
Hi,
I am using modbus routine (as a master) for receiving data.
I am having a problem with timeout. In initialization I set timeout 10000.
According to the driver file this time is in uS. Therefore it means 10000 is equal to 10mS.
Whenever there is communication error (or modbus cable is not connected), after every 10mS timeout error will be generated.
But in my case it is not happening. Timeout error generates after 60mS instead of 10mS.
Why this is happening so?
I am using pic16f628A with 20MHz crystal.
My compiler version is 4.093
Here is my code
Code: |
#include <16f628A.h>
#fuses HS,NOWDT,NOLVP,NOBROWNOUT,PUT
#use delay(clock=20M)
#define MODBUS_TYPE MODBUS_TYPE_MASTER
#define MODBUS_SERIAL_RX_BUFFER_SIZE 64
#define MODBUS_SERIAL_BAUD 19200
#define MODBUS_SERIAL_TIMEOUT 10000 //timeoout in us
#define MODBUS_SERIAL_INT_SOURCE MODBUS_INT_RDA
#define MODBUS_SERIAL_ENABLE_PIN PIN_B0
#include "modbus.c"
#define MODBUS_SLAVE_ADDRESS 0x08
int m_hold_array[5];
int i;
void main()
{
modbus_init();
delay_ms(10);
while(1)
{
output_toggle(PIN_A1);
if(!(modbus_read_holding_registers(MODBUS_SLAVE_ADDRESS,1029,1)))
{
for(i=0;i<(modbus_rx.len);++i)
{
m_hold_array[i]=modbus_rx.data[i];
}
}
}
}
|
Modbus file is too long, thats why I am not pasting it here. But if it is required then I will paste it here. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Apr 26, 2012 5:47 am |
|
|
I didn't yet use the MODBUS master, but after I brief view to the code, I guess, the problem is caused by the silly timeout implementation.
It's counting 10000 delay_us(1) interlaced with polling kbdhit(). This can never work, not with a fast processor and less than ever with a slow PIC16. Simply adjust the MODBUS_SERIAL_TIMEOUT 10000 accordingly. |
|
|
hayee
Joined: 05 Sep 2007 Posts: 252
|
|
Posted: Thu Apr 26, 2012 5:57 am |
|
|
Thanks for your reply FvM.
It means we have to set the timeout manually by setting the time and measure it with oscilloscope?
I also use 18f26k22 with 20Mhz and compiler version 4.124 but the same result. |
|
|
|