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("
\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