Modbus TCP Connect with IPC B&R - "Library AsMbTcp"

Hi community, I’m currently using the AsMbTCP library to establish a connection between a PLC and an IPC, but I’m having trouble connecting. I’m wondering if I need to declare and use a modbustcp_any block in the Physical view if I’m using this library? Also, how do I know if the PLC has successfully communicated with the IPC? I used my laptop to ping the command prompt and send Modbus signals via the Modbus Poll application, and I was able to connect and send signals to the IPC, but my PLC wasn’t connected. I also looked at the LibAsMbTCP1_ST library to build my code, but it still can’t connect or send signals. Can anyone help me find the problem?

Hình ảnh đã chọn được hiển thị trong một hộp đèn.

ACTION Act_Communication:

IF gRobotEM.Command.Cntrl_Mode = 1 THEN 
	temp_DINT := 1;
ELSIF gRobotEM.Command.Cntrl_Mode = 3 THEN 
	temp_DINT := 4;
ELSE 
	temp_DINT := 0;
END_IF;

mb_WriteBuffer[0] := DINT_TO_UINT(temp_DINT AND 16#FFFF);
mb_WriteBuffer[1] := DINT_TO_UINT(SHR(temp_DINT, 16) AND 16#FFFF);

CASE gRobotEM.Command.Cntrl_StateCmd OF
	pmlSTATE_RESETTING:   temp_CntrlCmd := 1;
	pmlSTATE_EXECUTE:     temp_CntrlCmd := 2;
	pmlSTATE_STOPPING:    temp_CntrlCmd := 3;
	pmlSTATE_HOLDING:     temp_CntrlCmd := 4;
	pmlSTATE_UNHOLDING:   temp_CntrlCmd := 5;
	pmlSTATE_SUSPENDING:  temp_CntrlCmd := 6;
	pmlSTATE_UNSUSPENDING:temp_CntrlCmd := 7;
	pmlSTATE_ABORTING:    temp_CntrlCmd := 8;
	pmlSTATE_CLEARING:    temp_CntrlCmd := 9;
	ELSE                  temp_CntrlCmd := 0;
END_CASE;

mb_WriteBuffer[2] := DINT_TO_UINT(temp_CntrlCmd AND 16#FFFF);
mb_WriteBuffer[3] := DINT_TO_UINT(SHR(temp_CntrlCmd, 16) AND 16#FFFF);

IF (temp_CntrlCmd <> old_CntrlCmd) AND (temp_CntrlCmd > 0) THEN
	cmd_PulseActive := TRUE;
	ton_CmdPulse.IN := FALSE; 
	old_CntrlCmd    := temp_CntrlCmd;
END_IF;

ton_CmdPulse(IN := cmd_PulseActive, PT := T#300ms);
IF ton_CmdPulse.Q THEN
	cmd_PulseActive := FALSE; 
	ton_CmdPulse.IN := FALSE;
END_IF;

mb_WriteBuffer[16] := 0; 
IF (temp_DINT > 0) AND (temp_DINT <> gExtInterface.PLC_To_Ext_Mode) THEN 
	mb_WriteBuffer[16] := mb_WriteBuffer[16] OR 16#0001; 
END_IF;
IF cmd_PulseActive THEN
	mb_WriteBuffer[16] := mb_WriteBuffer[16] OR 16#0002; 
END_IF;

mb_WriteBuffer[17] := 0;
IF gHmiGlobal.Station.Cmd_ChangeRecipe THEN 
	mb_WriteBuffer[17] := mb_WriteBuffer[17] OR 16#0001; 
END_IF;
IF gHmiGlobal.Robot.Cmd_Reset OR gHmiGlobal.Station.Cmd_Reset THEN 
	mb_WriteBuffer[17] := mb_WriteBuffer[17] OR 16#0002; 
END_IF; 

mb_WriteBuffer[18] := 101; 
mb_WriteBuffer[19] := 0;

temp_UDINT := DINT_TO_UDINT(gCurrentRecipe.RecipeID);
mb_WriteBuffer[20] := UDINT_TO_UINT(temp_UDINT AND 16#FFFF); 
mb_WriteBuffer[21] := UDINT_TO_UINT(SHR(temp_UDINT, 16) AND 16#FFFF); 

CASE (instruction) OF

	1: (* LỆNH ĐỌC *)
		mbReadInputRegisters_0.enable       := 1;
		mbReadInputRegisters_0.pStation     := ADR(ext_station);
		mbReadInputRegisters_0.startAddress := 0;
		mbReadInputRegisters_0.nrRegisters  := 35;
		mbReadInputRegisters_0.pData        := ADR(mb_ReadBuffer);
		mbReadInputRegisters_0.dataSize     := SIZEOF(mb_ReadBuffer);
        
		mbReadInputRegisters_0(); 

		IF (mbReadInputRegisters_0.status <> 65535) THEN 
			fub_status          := mbReadInputRegisters_0.status;
			check_status        := TRUE;	
			instruction_ready   := TRUE;	
			
			IF fub_status = 0 THEN
				gExtInterface.PLC_To_Ext_Mode := UINT_TO_DINT(mb_ReadBuffer[0]) OR SHL(UINT_TO_DINT(mb_ReadBuffer[1]), 16);
				temp_DINT := UINT_TO_DINT(mb_ReadBuffer[2]) OR SHL(UINT_TO_DINT(mb_ReadBuffer[3]), 16);
				gExtInterface.Ext_To_PLC_State := temp_DINT; 
				gExtInterface.Ext_IsRunning := (temp_DINT = 6); 
				gExtInterface.Ext_IsError   := (temp_DINT = 8) OR (temp_DINT = 9); 
				
				gCurrentRecipe.RecipeID := UINT_TO_DINT(mb_ReadBuffer[32]) OR SHL(UINT_TO_DINT(mb_ReadBuffer[33]), 16);
			END_IF;
			A
		END_IF;

	2: (* LỆNH GHI *)
		mbWriteMultipleRegisters_0.enable       := 1;
		mbWriteMultipleRegisters_0.pStation     := ADR(ext_station);
		mbWriteMultipleRegisters_0.startAddress := 0;
		mbWriteMultipleRegisters_0.nrRegisters  := 22; 
		mbWriteMultipleRegisters_0.pData        := ADR(mb_WriteBuffer);
		mbWriteMultipleRegisters_0.dataSize     := SIZEOF(mb_WriteBuffer);
        
		mbWriteMultipleRegisters_0(); 
        
		IF (mbWriteMultipleRegisters_0.status <> 65535) THEN
			fub_status          := mbWriteMultipleRegisters_0.status;
			check_status        := TRUE;	
			instruction_ready   := TRUE;	
			
		END_IF;
		
END_CASE;

IF (check_status) THEN
	IF fub_status = 0 THEN
		gExtInterface.IsConnected := TRUE;
		robotAlarmCode := 0; 
	ELSE
		gExtInterface.IsConnected := FALSE; 
		robotAlarmCode := UINT_TO_DINT(fub_status);
	END_IF;
	check_status := FALSE;
END_IF;

IF (instruction_ready AND fub_status = 0) THEN 
	CASE (instruction) OF
		1: instruction := 2; 
		2: instruction := 1; 
	END_CASE;
	instruction_ready := FALSE;
END_IF;

gHmiGlobal.Robot.ActualSpeed := gExtInterface.Pick_Rate;

END_ACTION

Hello,

Yes, you need to add ModbusTcp_any to an ETH interface, configure it, and enable Modbus communication on the ETH interface. See the AsMbTCP example help page.

To diagnose Modbus TCP slaves, see the help. ModuleOk should tell you if a connection to a slave exists.

Hi Pavel Vaura
Thank a lot for your help; I’ve successfully established the connection. Previously, I thought I only needed the AsMbTcp library, but it turns out I need to declare it in both the library and the Physical View to connect.:laughing: