TIA Portal本身支持符号寻址,但是有时出于维护的目的,需要在SCADA/HMI上的信息系统中做出相应的的查询子功能。
本文小探在PLC端实现的两种方法,以供参考。
1、通过直接输入的“地址字符串”查询,例如“I0.0"
2、通过直接输入的“配置地址”查询
源代码分别如下,细节不再赘述,阅读代码即可清楚。
------------------------------------------------------------
FUNCTION_BLOCK "FB_GetSymbolByAddressString"
TITLE = GetSymbolNameByString
{ S7_Optimized_Access := 'TRUE' }
AUTHOR : Razor
FAMILY : SymbolName
VERSION : 0.1
//get symbol name of bool address by string setpoint
VAR_INPUT
I_str_address : String[8]; // string of address
END_VAR
VAR_OUTPUT
O_str_symbol { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : String[32]; // symbol name of address
O_int_index { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : Int; // index of symbol table
O_int_error { ExternalWritable := 'False'} : Int; // error
END_VAR
VAR_IN_OUT
I_arr_inputSymbol : Array[*] of String[8]; // symbol table of input
I_arr_outputSymbol : Array[*] of String[16]; // symbol table of output
END_VAR
VAR
statStringType { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : String[1]; // memory type value = I/Q/M
statStringByte { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : String[4]; // byte string:max value = 1024
statStringBit { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : String[1]; // bit string : max value = 7
statStringLength { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : Int; // address length
statByteLength { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : Int;
statByteVlaue { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : Int;
statBitValue { ExternalAccessible := 'False'; ExternalVisible := 'False'; ExternalWritable := 'False'} : Int;
END_VAR
VAR CONSTANT
ADDRESS_TYPE_LENGTH : Int := 1;
ADDRESS_BIT_LENGTH : Int := 1;
ADDRESS_BYTE_POSITION : Int := 2;
NO_ERROR : Int := 0;
HAS_ERROR : Int := 1;
MIN_BIT : Int := 0;
MAX_BIT : Int := 7;
MIN_BYTE : Int := 0;
MAX_BYTE : Int := 1024;
BYTE_WIDTH : Int := 8;
ZERO : Int := 0;
END_VAR
BEGIN
REGION CalculateIndex
#O_int_index := #ZERO;
#statStringLength := LEN(#I_str_address);
#statByteLength := #statStringLength - 3;
#statStringByte := MID(IN := #I_str_address, L := #statByteLength, P := #ADDRESS_BYTE_POSITION);
STRG_VAL(IN := #statStringByte,
FORMAT := w#16#000,
P := 1,
OUT => #statByteVlaue);
#statStringBit := RIGHT(IN := #I_str_address, L := #ADDRESS_BIT_LENGTH);
STRG_VAL(IN := #statStringBit,
FORMAT := w#16#000,
P := 1,
OUT => #statBitValue);
IF #statBitValue >= #MIN_BIT AND #statBitValue <= #MAX_BIT THEN
#O_int_index := #statByteVlaue * #BYTE_WIDTH + #statBitValue;
#O_int_error := #NO_ERROR;
ELSE
#O_int_error := #HAS_ERROR;
END_IF;
END_REGION
REGION Output
#statStringType := LEFT(IN := #I_str_address, L := #ADDRESS_TYPE_LENGTH);
IF #statStringType = 'I' OR #statStringType = 'i' THEN
#O_str_symbol := #I_arr_inputSymbol[#O_int_index];
END_IF;
IF #statStringType = 'Q' OR #statStringType = 'q' THEN
#O_str_symbol := #I_arr_outputSymbol[#O_int_index];
END_IF;
END_REGION
END_FUNCTION_BLOCK