Sign in

username:

password:



Not a member?

Search rabbit-semi



Search tips

Subscribe to rabbit-semi



Ads

Discussion Groups

Discussion Groups | Rabbit-Semi | Reading Modbus Registers

This is a group for folks designing and programming embedded systems using the Rabbit Semiconductor C-programmable microcontroller. Rabbit Semi is a spin-off from Z-World who makes a variety of embedded modules and tools. This group is not affiliated with either Rabbit or Z-World, but is a user forum for sharing ideas, asking questions, flaunting knowledge, and other typical user group stuff. The Rabbit is a powerful uC, supported by a full-featured C-compiler.

Reading Modbus Registers - whmeade2008 - Mar 29 11:31:51 2008

I have been able to write to registers over modbus and set/reset coils
but have not been able to figure out how to read a register or group
of registers. I am using the sample modbus programs
modbus_serial_master.c and the modbus_master.lib. My main function is
below:

--------------------------------------------------------------------

main ()
{
int i, RegsValue[4];
serInit();

// Write 4000 to register W40001 on EATON HMI
MBM_WriteReg ( 1, 0, 4000 ); // THIS WORKS

// Set Bit 0 on EATON HMI
MBM_WriteCoil( 1, 0, 1); // THIS WORKS

// Reset Bit 1 on EATON HMI
MBM_WriteCoil( 1, 1, 0); // THIS WORKS

// Read data from register W40002 on EATON HMI
i = MBM_ReadRegs ( 1, &RegsValue[0], 1, 1 ); // read reg 0

// THE LINE ABOVE DOES NOT WORK, AND THE VALUE IS NOT PRINTED OUT
// ON IN THE STUDIO WINDOW.

// Print result to studio window
printf("\n\r%u", i);

while (1) ;
} // end main

--------------------------------------------------------------------

The following is the function that it calls in the modbus_master.lib

--------------------------------------------------------------------

/* START FUNCTION DESCRIPTION ********************************************
MBM_ReadRegs 0x03

SYNTAX: int MBM_ReadRegs ( int MB_address, int* Result,
int Starting_Reg, int Nbr_of_Regs );

DESCRIPTION: Read the specified registers.

PARAMETER1: MODBUS addresss of the target device

PARAMETER2: Starting address to put the results

PARAMETER3: Starting register number, 1 relative, to read

PARAMETER4: Number of registers to read

RETURN VALUE: MB_SUCCESS
MBM_INVALID_PARAMETER
MBM_PACKET_ERROR
MBM_BAD_ADDRESS

Note: This function is not re-entrant.
END DESCRIPTION
**********************************************************/

/*** BeginHeader MBM_ReadRegs */
int MBM_ReadRegs ( int MB_address, int* Result, int Starting_Reg,
int Nbr_of_Regs );
/*** EndHeader */

MODBUS_DEBUG
int MBM_ReadRegs ( int MB_address, int* Result, int Starting_Reg,
int Nbr_of_Regs )
{
auto int ADUStatus;
auto int RegValue;
auto int Count;

if ( Starting_Reg < 0 )
{
return MBM_INVALID_PARAMETER;
}
if ( Nbr_of_Regs <= 0 || Nbr_of_Regs > 125 )
{
return MBM_INVALID_PARAMETER;
}

_initADU( MB_address, 0x03, 4 );
_insertWord ( Starting_Reg );
_insertWord ( Nbr_of_Regs );

ADUStatus = MBM_Send_ADU ( mbADU, pmbADU - &mbADU[0] );

if ( ADUStatus != MB_SUCCESS )
{
return ADUStatus;
}
if ( mbADU[ADU_OFF_ADDRESS] != MB_address )
{
return MBM_BAD_ADDRESS;
}
if ( mbADU[ADU_OFF_FUNCTION] & 0x80 )
{
return (int)mbADU[ADU_OFF_EXCEPTION];
}

for ( Count=0; Count {
*Result++ = _getADUword(3+(Count*2));
}

return MB_SUCCESS;
} // MBM_ReadRegs

--------------------------------------------------------------------

Can someone please show me where I am going wrong?

------------------------------------



(You need to be a member of rabbit-semi -- send a blank email to rabbit-semi-subscribe@yahoogroups.com )