Reading Slave Status in MODBUS Communication

M

Thread Starter

malampati

My objective is my master (Microcontroller) want to read the read holding register from socomec energy meter. Here i am using the RS232 to Rs485 conversion, but i didn't get any acknowledgement. I send the data from my master to slave like this....

data("0x05");data("0x03");data("0xc5");data("0x52");data("0x02");

IS there any mistake in this? Is I2C is needed for this communication?
 
> Is I2C is needed for this communication?
no its not needed. just use the UARTs (RS232). search the web for modbus lib for your micro.
 
Hai thanks for your reply.Then where iam doing a mistake.
This is the code what i written,please check this code if i did any thing wrong please suggest me i am new with modbus communication.

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/fpu.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "driverlib/rom.h"
#include "grlib/grlib.h"
#include "drivers/cfal96x64x16.h"
#include "driverlib/pin_map.h"

void tx(unsigned char);
void clearbuff(void);
void data(unsigned char *);
void Crc_cal(void);

unsigned char m[150],mess1[10];
unsigned char mess[10];
unsigned int low_byte,high_byte;
int count=0,count10=0,count1=0;
int j;
unsigned short int crc,this_byte,i,shift,lastbit;



void
UARTIntHandler(void)
{

++count10;
SysCtlDelay(10000);
uint32_t ui32Status;

//
// Get the interrrupt status.
//
ui32Status = ROM_UARTIntStatus(UART0_BASE, true);

//
// Clear the asserted interrupts.
//
ROM_UARTIntClear(UART0_BASE, ui32Status);

//
// Loop while there are characters in the receive FIFO.
//
while(ROM_UARTCharsAvail(UART0_BASE))

{
/* while(!ADCIntStatus(ADC0_BASE, 1, false))
{
}*/
m[count]=ROM_UARTCharGetNonBlocking(UART0_BASE);

// ROM_UARTCharPutNonBlocking(UART0_BASE,m[count]);

count++;

if(count>=100)
{
//clearbuff();
// if(count==100)
count=0;
// clearbuff((unsigned char *)m);
}

}


// count10=0;
}


int
main(void)
{
//tRectangle sRect;
//tContext sContext;

//
// Enable lazy stacking for interrupt handlers. This allows floating-point
// instructions to be used within interrupt handlers, but at the expense of
// extra stack usage.
//
ROM_FPULazyStackingEnable();

//
// Set the clocking to run directly from the crystal.
//
ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
SYSCTL_XTAL_16MHZ);

//

ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);

GPIOPinConfigure(GPIO_PA0_U0RX);

GPIOPinConfigure(GPIO_PA1_U0TX);

ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 9600,
(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
UART_CONFIG_PAR_NONE));

UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);

// UARTStdioConfig(0, 115200, 16000000);

UARTFIFOEnable(UART0_BASE);

UARTFIFOLevelSet(UART0_BASE, UART_FIFO_TX1_8, UART_FIFO_RX1_8);

UARTTxIntModeSet(UART0_BASE, UART_TXINT_MODE_FIFO);

// ROM_UARTIntDisable(UART0_BASE, UART_INT_TX);

ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

ROM_IntEnable(INT_UART0);

UARTEnable(UART0_BASE);

while(1)
{
data("0x05");
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);

data("0x03");
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);

data("0xC5");
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);

data("0x52");
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);


data("0x00");
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);

data("0x02");
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);


data("0x58");
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);

data("0x92");
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);
SysCtlDelay(SysCtlClockGet() / 12);

}
}
void data(unsigned char *z)
{
while(*z!='\0')
{
ROM_UARTCharPutNonBlocking(UART0_BASE, *z++);

}
}
 
Top