Read data from slave
Use this instruction to request data from 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 FP_READ_FROM_SLAVE_AREA_OFFS or FP_MODBUS_MASTER.
For data transmissions using the Modbus protocol, the compiler generates Modbus commands based on the Modbus reference numbers.Input
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
Station number of the slave (MEWTOCOL: 1–99, MODBUS: 1–247)
Set to 1, if a SYS_ETHERNET_USER_CONNECTION_xx is applied to input Port
Source address on the slave from which the data is requested.
Output
Word area or register on the master unit to which the requested data is written.
For FP7 only: For a description of all error codes, please refer to the table of Modbus/MEWTOCOL communication error codes.
For other PLCs: set to 0
While sending: Master communication sending flag is TRUE
Sending done: Master communication sending flag is FALSE
Normal completion: FALSE
Abnormal completion: TRUE
if the area specified using the index modifier exceeds the limit.
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.
if the area specified using the index modifier exceeds the limit.
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.
In the global variable list you define variables that can be accessed by all POUs in the project.
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_EXTERNAL
g_Peripherie_SlaveResult_Address: ARRAY[0..9] OF REAL:=[10(0)];
END_VAR
VAR
bReadResult: BOOL:=FALSE;
wReadFromSlaveResult: WORD:=0;
(*result of write to slave instruction*)
iSlaveStationNumber: INT:=0;
(*slave station number*)
arrResults: ARRAY [0..9] OF REAL:=[10(0.0)];
(*Array of results to be read from slave station*)
iSlaveMemoryArea: INT:=5;
(*memory Area in slave station*)
iSlaveMemoryOffset: INT:=100;
iSlaveMemorySize: INT:=1;
END_VAR
BODY
WORKSPACE
NETWORK_LIST_TYPE := NWTYPELD ;
ACTIVE_NETWORK := 0 ;
END_WORKSPACE
NET_WORK
NETWORK_TYPE := NWTYPELD ;
NETWORK_LABEL := ;
NETWORK_TITLE := ;
NETWORK_HEIGHT := 8 ;
NETWORK_BODY
B(B_VARIN,,SYS_COM1_PORT,19,2,21,4,);
B(B_VARIN,,iSlaveStationNumber,19,3,21,5,);
B(B_CONTACT,,bReadResult,8,1,10,3,);
B(B_VAROUT,,wReadFromSlaveResult,38,3,40,5,);
B(B_VAROUT,,arrResults,38,2,40,4,);
B(B_F,FP_READ_FROM_SLAVE!,,21,0,33,6,,?DEN?DnPort?DStationNumber?DSlaveAddress?AENO?CDestination?CResult);
B(B_VARIN,,g_Peripherie_SlaveResult_Address,19,4,21,6,);
L(1,2,8,2);
L(10,2,21,2);
L(33,3,38,3);
L(33,4,38,4);
L(1,0,1,8);
END_NETWORK_BODY
END_NET_WORK
END_BODY
if (bReadResult) then
FP_READ_FROM_SLAVE(Port := SYS_COM1_PORT, StationNumber := iSlaveStationNumber,
SlaveAddress := g_Peripherie_SlaveResult_Address, Destination => arrResults[0],
Result => wReadFromSlaveResult);
end_if;