F145_WRITE_DATA

Write data to slave

Use this instruction to write data from a master to a slave via the communication port using the MEWTOCOL-COM or Modbus RTU protocol, as defined in the system registers of the port used. Make sure the same protocol is set for master and slave. Master and slave must have matching memory areas. If the slave data is not available in the user area of the master, use F145_WRITE_DATA_TYPE_OFFS or F145F146_MODBUS_MASTER.

For data transmissions using the Modbus protocol, the compiler generates Modbus commands based on the Modbus reference numbers.

Parameters

Input

Port (WORD, INT, UINT)

Specifies the communication ports depending on the PLC type:

  • COM port e.g. SYS_COM0_PORT

  • Ethernet port e.g. SYS_ETHERNET_USER_CONNECTION_1

  • MCU/SCU e.g. 16#xx01 (xx = slot number) in COM01

SlaveAddress (WORD, INT, UINT)

Station number of the slave (MEWTOCOL: 0–99, MODBUS: 0–255, 0: Broadcasting)

Set to 1, if a SYS_ETHERNET_USER_CONNECTION_xx is applied to input Port

MasterData (BOOL, INT, UINT, WORD, DINT, UDINT, DWORD, REAL, DATE, TOD, DT, STRING)

Source address on the master for the data to be written to the slave.

Output

SlaveData (BOOL, INT, UINT, WORD, DINT, UDINT, DWORD, REAL, DATE, TOD, DT, STRING)
  • Destination address on the slave to which the data is written.

  • MasterData and SlaveData must be of the same data type.

  • To establish external data access from the master to the slave data you must assign fixed user addresses (same addresses as slave data) in the global variable list.

Remarks

  • Instead of using this F instruction, we recommend using the most flexible instruction: FP_WRITE_TO_SLAVE_AREA_OFFS.

  • The F145 or F146 instructions can only be executed if neither instruction is active. Evaluate the "F145/F146 not active" flag in your program to check the state of the instructions.

  • The F145 instruction only requests that data be sent to the slave. The actual processing takes place at the end of the scan.

  • Evaluate the "F145/F146 error" flag to check whether transmission has completed normally or with an error.

  • When broadcasting (SlaveAddress set to 0), make sure transmission is executed only after the maximum scan time has elapsed.

  • The F145 or F146 instructions cannot be executed if the destination address is a special internal flag (from R9000), a special data register (from DT9000/DT90000), or a file register FL.

Error flags

sys_bIsOperationErrorHold (turns to TRUE and remains TRUE)
  • If slave or master data exceed the available address range.
  • If the communication mode is not set to MEWTOCOL-COM Master/Slave or Modbus RTU Master/Slave.
  • If the COM port selected requires a communication cassette that has not been installed.
sys_bIsOperationErrorNonHold (turns to TRUE for one scan)
  • If slave or master data exceed the available address range.
  • If the communication mode is not set to MEWTOCOL-COM Master/Slave or Modbus RTU Master/Slave.
  • If the COM port selected requires a communication cassette that has not been installed.

Example

Global variables

In the global variable list you define variables that can be accessed by all POUs in the project.

POU header

All input and output variables used for programming this function have been declared in the POU header. The same POU header is used for all programming languages.

	VAR
		bBool1: BOOL:=FALSE;
		bRead1: BOOL:=FALSE;
		bWrite1: BOOL:=FALSE;
	END_VAR
	VAR_EXTERNAL
		Slave2_g_bY38: BOOL:=FALSE;
	END_VAR

POU body

The system variable sys_bPulse1s is copied to bBool1. If bWrite1 and sys_bIsComPort1F145F146NotActive are set to TRUE, bBool1 is written to the output Y38 of slave 2.

LD body

BODY
    WORKSPACE
        NETWORK_LIST_TYPE := NWTYPELD ;
        ACTIVE_NETWORK := 0 ;
    END_WORKSPACE
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 3 ;
        NETWORK_BODY
B(B_CONTACT,,sys_bPulse1s,3,1,5,3,);
B(B_COIL,,bBool1,24,1,26,3,);
L(5,2,24,2);
L(1,2,3,2);
L(1,0,1,3);
        END_NETWORK_BODY
    END_NET_WORK
    NET_WORK
        NETWORK_TYPE := NWTYPELD ;
        NETWORK_LABEL :=  ;
        NETWORK_TITLE :=  ;
        NETWORK_HEIGHT := 6 ;
        NETWORK_BODY
B(B_CONTACT,,sys_bIsComPort1F145F146NotActive,8,1,10,3,);
B(B_CONTACT,,bWrite1,18,1,20,3,);
B(B_VARIN,,SYS_COM1_PORT,21,2,23,4,);
B(B_VARIN,,2,21,3,23,5,);
B(B_VARIN,,bBool1,21,4,23,6,);
B(B_F,F145_WRITE_DATA!,Instance,23,0,33,6,,?DEN?DPort?DSlaveAddress?DMasterData?AENO?CSlaveData);
B(B_VAROUT,,Slave2_g_bY38,33,2,35,4,);
B(B_COIL,,bWrite1,36,1,38,3,E);
B(B_COIL,,bRead1,42,1,44,3,S);
L(1,2,8,2);
L(10,2,18,2);
L(20,2,23,2);
L(33,2,36,2);
L(38,2,42,2);
L(1,0,1,6);
        END_NETWORK_BODY
    END_NET_WORK
END_BODY

ST body

bBool1 := sys_bPulse1s;
if (bWrite1 and sys_bIsComPort1F145F146NotActive) then
    F145_WRITE_DATA(Port := SYS_COM1_PORT, 
                    SlaveAddress := 2, 
                    MasterData := bBool1, 
                    SlaveData => Slave2_g_bY38);    
    bRead1 := true;
    bWrite1 := false;
end_if;

Modified on: 2022-01-13Feedback on this pagePanasonic hotline