此示例用于读取 plc 时间并将其转换为字符串。此代码是为带有 SCL(结构化控制语言)的 Siemens PLC 开发的:
(*************************************************************************************)
FUNCTION FC11 : STRING
TITLE = 'Convert date and time to string'
(*******************************************************************************
FUNCTION: Convert date and time to string
*******************************************************************************)
// ------------------------------------------------------------------------------
// parameter
// ------------------------------------------------------------------------------
VAR_TEMP
DTAsString : STRING[14];
DateTime : DATE_AND_TIME;
DateTimeAsByteArray AT DateTime
: ARRAY[0..7] OF BYTE;
ReturnValue : INT;
Year : INT;
Month : INT;
Day : INT;
Hours : INT;
Minutes : INT;
Seconds : INT;
END_VAR
// ------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
//Inizialize
DTAsString := '';
//Read PLC date and time
ReturnValue := READ_CLK(CDT := DateTime // OUT: DATE_AND_TIME
);
//Split data and time value
Year := BCD_TO_INT(DateTimeAsByteArray[0]);
Month := BCD_TO_INT(DateTimeAsByteArray[1]);
Day := BCD_TO_INT(DateTimeAsByteArray[2]);
Hours := BCD_TO_INT(DateTimeAsByteArray[3]);
Minutes := BCD_TO_INT(DateTimeAsByteArray[4]);
Seconds := BCD_TO_INT(DateTimeAsByteArray[5]);
//Build string
//------------
//Year
//----
IF (Year >= 10) THEN
DTAsString := CONCAT (IN1:= DTAsString, IN2:= '20');
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= INT_TO_STRING(Year),L:=2));
ELSE
DTAsString := CONCAT (IN1:= DTAsString, IN2:= '200');
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= INT_TO_STRING(Year),L:=1));
END_IF;
//Month
//-----
IF (Month >= 10) THEN
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= INT_TO_STRING(Month),L:=2));
ELSE
DTAsString := CONCAT (IN1:= DTAsString, IN2:= '0');
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= INT_TO_STRING(Month),L:=1));
END_IF;
//Day
//---
IF (Day >= 10) THEN
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= I_STRNG(Day),L:=2));
ELSE
DTAsString := CONCAT (IN1:= DTAsString, IN2:= '0');
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= I_STRNG(Day),L:=1));
END_IF;
//Hours
//-----
IF (Hours >= 10) THEN
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= I_STRNG(Hours),L:=2));
ELSE
DTAsString := CONCAT (IN1:= DTAsString, IN2:= '0');
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= I_STRNG(Hours),L:=1));
END_IF;
//Minutes
//-------
IF (Minutes >= 10) THEN
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= I_STRNG(Minutes),L:=2));
ELSE
DTAsString := CONCAT (IN1:= DTAsString, IN2:= '0');
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= I_STRNG(Minutes),L:=1));
END_IF;
//Seconds
//-------
IF (Seconds >= 10) THEN
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= I_STRNG(Seconds),L:=2));
ELSE
DTAsString := CONCAT (IN1:= DTAsString, IN2:= '0');
DTAsString := CONCAT (IN1:= DTAsString, IN2:= RIGHT(IN:= I_STRNG(Seconds),L:=1));
END_IF;
//Return Date and Time as String
//------------------------------
FC11 := DTAsString;
END_FUNCTION
//*******************************************************************************
(* Nothing beyond this *)