2019-01-11 01:32 AM
Hello friends.
I try imeplement freemodbus on my project. And I have a problem with connect. My task is here:
/* ------------------------ Defines --------------------------------------- */
#define REG_INPUT_START 1000
#define REG_INPUT_NREGS 4
#define REG_HOLDING_START 2000
#define REG_HOLDING_NREGS 130
/* ----------------------- Static variables ---------------------------------*/
static USHORT usRegInputStart = REG_INPUT_START;
static USHORT usRegInputBuf[REG_INPUT_NREGS];
static USHORT usRegHoldingStart = REG_HOLDING_START;
static USHORT usRegHoldingBuf[REG_HOLDING_NREGS];
void ModbusTask(void *pvParameters)
{
eMBErrorCode xStatus;
InfoPrint("FINISH INIT modbus");
while (1)
{
if (eMBTCPInit( MB_TCP_PORT_USE_DEFAULT) != MB_ENOERR)
{
InfoPrint("can't initialize modbus stack!");
}
else if (eMBEnable() != MB_ENOERR)
{
InfoPrint("can't enable modbus stack!");
}
else
{
do
{
xStatus = eMBPoll();
} while (xStatus == MB_ENOERR);
}
/* An error occured. Maybe we can restart. */
(void) eMBDisable();
(void) eMBClose();
}
}
void init_modbus(void)
{
xTaskCreate(ModbusTask, "ModbusTask", 4096, (void *) NULL, 2, NULL);
}
eMBErrorCode
eMBRegInputCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs )
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
if( ( usAddress >= REG_INPUT_START )
&& ( usAddress + usNRegs <= REG_INPUT_START + REG_INPUT_NREGS ) )
{
iRegIndex = ( int )( usAddress - usRegInputStart );
while( usNRegs > 0 )
{
*pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( unsigned char )( usRegInputBuf[iRegIndex] & 0xFF );
iRegIndex++;
usNRegs--;
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
eMBErrorCode
eMBRegHoldingCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNRegs, eMBRegisterMode eMode )
{
eMBErrorCode eStatus = MB_ENOERR;
int iRegIndex;
if( ( usAddress >= REG_HOLDING_START ) &&
( usAddress + usNRegs <= REG_HOLDING_START + REG_HOLDING_NREGS ) )
{
iRegIndex = ( int )( usAddress - usRegHoldingStart );
switch ( eMode )
{
/* Pass current register values to the protocol stack. */
case MB_REG_READ:
while( usNRegs > 0 )
{
*pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] >> 8 );
*pucRegBuffer++ = ( UCHAR ) ( usRegHoldingBuf[iRegIndex] & 0xFF );
iRegIndex++;
usNRegs--;
}
break;
/* Update current register values with new values from the
* protocol stack. */
case MB_REG_WRITE:
while( usNRegs > 0 )
{
usRegHoldingBuf[iRegIndex] = *pucRegBuffer++ << 8;
usRegHoldingBuf[iRegIndex] |= *pucRegBuffer++;
iRegIndex++;
usNRegs--;
}
}
}
else
{
eStatus = MB_ENOREG;
}
return eStatus;
}
eMBErrorCode
eMBRegCoilsCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNCoils, eMBRegisterMode eMode )
{
return MB_ENOREG;
}
eMBErrorCode
eMBRegDiscreteCB( UCHAR * pucRegBuffer, USHORT usAddress, USHORT usNDiscrete )
{
return MB_ENOREG;
}
And configuration with port is in added files. Can anybody help me with it?
2019-01-11 03:19 AM
i am also facing same problem i'm using stm32f103 and stm32f4 discovery board to communicate with HMI
2019-01-11 04:14 AM
I modificated my task function:
void ModbusTask(void *pvParameters)
{
if (eMBTCPInit( MB_TCP_PORT_USE_DEFAULT) != MB_ENOERR)
{
InfoPrint("FreeMODBUS: fail init");
}
if (eMBEnable() != MB_ENOERR)
{
InfoPrint("FreeMODBUS: fail enable");
}
InfoPrint("FINISH INIT modbus");
while (1)
{
eMBPoll();
}
}
and enable debug output. So I use to connect Hercules SETUP utility by HW-group.com. I set IP address: 192.168.11.111 port: 502 in client mode. And I know, connection is good.
But how to connect client with modbus I don't know....
2019-01-16 09:56 PM
Hello.
I try implement this project: https://github.com/armink/FreeModbus_Slave-Master-RTT-STM32/releases/tag/V1.1
And now client is working. And now I am trying start master. And I have a problem with init master. Does anybody help?
Here is my source codes...
Source in: porttcp.c work with client.
Init function
#if MB_MASTER_TCP_ENABLED > 0
eMBErrorCode eMBMasterTCPInit(USHORT ucTCPPort)
{
eMBErrorCode eStatus = MB_ENOERR;
if ((eStatus = eMBTCPDoInit(ucTCPPort)) != MB_ENOERR)
{
eMBState = STATE_DISABLED;
}
else if (!xMBPortEventInit())
{
/* Port dependent event module initalization failed. */
eStatus = MB_EPORTERR;
}
else
{
pvMBMasterFrameStartCur = eMBTCPStart;
pvMBMasterFrameStopCur = eMBTCPStop;
peMBMasterFrameReceiveCur = eMBTCPReceive;
peMBMasterFrameSendCur = eMBTCPSend;
pvMBMasterFrameCloseCur = MB_PORT_HAS_CLOSE ? vMBTCPPortClose : NULL;
eMBState = STATE_DISABLED;
}
return eStatus;
}
#endif
And I think, I will must create a new source file: porttcp_m.c with functions for master. Is it?