Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
10/21/08 09:49
Read: times


 
#159228 - modbus frame error
Responding to: ???'s previous message
hi,
i am getting framing error this is my code.. as a master i am using simulator.. plz help me to correct my code..

thnks
saritha

...replace this with your code...

#include<stdio.h>
#include<intrins.h>
#include<c8051f000.h>
void xba_con(void);
void port_con(void);
void disb_wdt(void);
void xosc_con(void);
void config_uart(void);
void read_modbus();
unsigned int modbusCRC(unsigned char *,unsigned char);
xdata unsigned char s_data[20] _at_ 0x30;
xdata unsigned char rec_buf[10] _at_ 0x00;
/*---------------------------------------------------------------------------------------------
"MAIN_LOOP"
------------------------------------------------------------------------------------------------*/
void main()
{

// unsigned char temp_l,temp_h;
unsigned int len=6,i;
xba_con();
port_con();
disb_wdt();
xosc_con();
config_uart();
for(i=0;i<=8;i++)
{
rec_buf[i]=SBUF;
while(!RI);
RI=0x0;
}
if(rec_buf[0]==1)
{
if(rec_buf[1]==0x4)
read_modbus();
}
// temp_l=rec_buf[6]^crc&0xff;
//temp_h=rec_buf[7]^crc>>8;
//rec_buf[6]=temp_l;
//rec_buf[7]=temp_h;
//if(!temp_l)
//{
// if(rec_buf[1]==0x4)
// read_modbus();
//}

}
/*---------------------------------------------------------------------------------------------
"MODBUS CRC"
-----------------------------------------------------------------------------------------------*/
unsigned int modbusCRC(unsigned char *data1, unsigned char dataLen)
{
unsigned int crc = 0xffff,tempCRC;
unsigned char tempLSB, flag;
int i, j;
for (i = 0; i < dataLen; i++)
{
tempLSB = 0x00FF & crc;
crc = tempLSB ^ data1[i];
for (j = 0; j < 8; j++)
{
tempCRC = crc;
flag = tempCRC & 0x0001;
crc = crc >> 1;
if (flag)
crc = crc ^ 0xA001;
}
}
return crc;
}
/*---------------------------------------------------------------------------------------------------
"READ MODBUS"
---------------------------------------------------------------------------------------------------------------*/
void read_modbus()
{
unsigned char j=0,i,no_of_bytes;
unsigned int crc;
no_of_bytes=rec_buf[5];
rec_buf[2]=2*no_of_bytes;
for(i=3;i<3+no_of_bytes;i++)
rec_buf[i]=s_data[j++];
crc=modbusCRC(rec_buf,i);
rec_buf[++i]=crc&0xff;
rec_buf[++i]=crc>>0x8;
j=i;
for(i=0;i<=j;i++)
{
SBUF=rec_buf[i];
while(!RI);
RI=0x0;
}

}
/*--------------------------------------------------------------------------------------------
"XBAR CONFIGURATION"
-----------------------------------------------------------------------------------------------*/
void xba_con()
{
XBR0=0x07;
XBR1=0x14;
XBR2=0x40;
}
/*---------------------------------------------------------------------------------------------
"PORTS CONFIGURATION"
-----------------------------------------------------------------------------------------------*/
void port_con()
{
PRT3CF=0x00;
PRT2CF=0x3F;
PRT1CF=0x0F8;
PRT0CF=0x76;
}
/*---------------------------------------------------------------------------------------------
"DISABLE WATCHDOG TIMER"
-----------------------------------------------------------------------------------------------*/
void disb_wdt()
{
EA=0;
WDTCN=0x0DE;
WDTCN=0x0AD;
EA=1;
}
/*---------------------------------------------------------------------------------------------
"OSICILLATOR CONFIGURATION"
-----------------------------------------------------------------------------------------------*/
void xosc_con()
{

int i;
OSCXCN=0x67;
for (i=0; i < 256; i++) ;
while (!(OSCXCN & 0x80)) ;
OSCICN = 0x88;
}
/*------------------------------------------------------------------------------------------------
"CONFIG_UART"
----------------------------------------------------------------------------------------------------*/
void config_uart()
{

RCAP2L=0X59;
RCAP2H=0XFF;
TH2=0XFF;
TL2=0X59;
SCON=0x50;
RCAP2L=0XD8;
RCAP2H=0XFF;
TH2=0XFF;
TL2=0XD8;
TCLK=0X1;
RCLK=0X1;
TR2=0x1;
}

List of 15 messages in thread
TopicAuthorDate
modbus for 8051 controller/At89c52            01/01/70 00:00      
   What help, exactly, do you require?            01/01/70 00:00      
      Modbus            01/01/70 00:00      
         Russel had code            01/01/70 00:00      
            modbus communication            01/01/70 00:00      
               Start in this way            01/01/70 00:00      
                  modbus frame error            01/01/70 00:00      
                     Post as code - or edit and correct            01/01/70 00:00      
      modbus RS485            01/01/70 00:00      
         Source code            01/01/70 00:00      
   modbus frame error            01/01/70 00:00      
      Real results contra expected            01/01/70 00:00      
         modbus            01/01/70 00:00      
            Frame error            01/01/70 00:00      
   board mạch            01/01/70 00:00      

Back to Subject List