Код:
FUNCTION_BLOCK create_csv_file
VAR_INPUT
made: BOOL;
file_data: POINTER TO struct_bin_file_record;
END_VAR
VAR_OUTPUT
done: BOOL;
csv_result: WSTRING(10000);
csv_lenght: DINT;
END_VAR
VAR
t_state: INT;
run: R_TRIG;
i: INT;
t_wstring: WSTRING;
END_VAR
VAR CONSTANT
devider: WSTRING := ",";
new_liner: WSTRING := "$N";
END_VAR
run(CLK:=made);
CASE t_state OF
0: //======================================= wait for execute =====
IF run.Q THEN
t_state := 1;
done := FALSE;
csv_result := OSU.CP1251_TO_UNICODE(' Перемещение,Давление,Усилие$N');
csv_result[0] := 16#FEFF;
csv_lenght := StrLenW(ADR(csv_result));
END_IF
1: //======================================= create file =====
FOR i:=0 TO file_data^.table_point DO
t_wstring := OSU.CP1251_TO_UNICODE( OSU.REAL_TO_STRING_FORMAT(file_data^.table[i].len, 3, OSU.DOT) );
StrConcatW ( ADR(t_wstring) , ADR(csv_result) , SIZEOF(csv_result) );
StrConcatW ( ADR(devider) , ADR(csv_result) , SIZEOF(csv_result) );
csv_lenght := csv_lenght + StrLenW(ADR(t_wstring)) + 1;
t_wstring := OSU.CP1251_TO_UNICODE( OSU.REAL_TO_STRING_FORMAT(file_data^.table[i].pres, 1, OSU.DOT) );
StrConcatW ( ADR(t_wstring) , ADR(csv_result) , SIZEOF(csv_result) );
StrConcatW ( ADR(devider) , ADR(csv_result) , SIZEOF(csv_result) );
csv_lenght := csv_lenght + StrLenW(ADR(t_wstring)) + 1;
t_wstring := OSU.CP1251_TO_UNICODE( OSU.REAL_TO_STRING_FORMAT(file_data^.table[i].force, 0, OSU.DOT) );
StrConcatW ( ADR(t_wstring) , ADR(csv_result) , SIZEOF(csv_result) );
csv_lenght := csv_lenght + StrLenW(ADR(t_wstring));
StrConcatW ( ADR(new_liner) , ADR(csv_result) , SIZEOF(csv_result) );
csv_lenght := csv_lenght + 1;
END_FOR
t_state := 2;
2: //======================================= write to buffer =====
t_wstring := " , , ,$N";
StrConcatW ( ADR(t_wstring) , ADR(csv_result) , SIZEOF(csv_result) );
csv_lenght := csv_lenght + StrLenW(ADR(t_wstring));
t_wstring := OSU.CP1251_TO_UNICODE( OSU.REAL_TO_STRING_FORMAT(file_data^.result_Lenght, 3, OSU.DOT) );
StrConcatW ( ADR(t_wstring) , ADR(csv_result) , SIZEOF(csv_result) );
StrConcatW ( ADR(devider) , ADR(csv_result) , SIZEOF(csv_result) );
csv_lenght := csv_lenght + StrLenW(ADR(t_wstring)) + 1;
t_wstring := OSU.CP1251_TO_UNICODE( OSU.REAL_TO_STRING_FORMAT(file_data^.result_Preasure, 1, OSU.DOT) );
StrConcatW ( ADR(t_wstring) , ADR(csv_result) , SIZEOF(csv_result) );
StrConcatW ( ADR(devider) , ADR(csv_result) , SIZEOF(csv_result) );
csv_lenght := csv_lenght + StrLenW(ADR(t_wstring)) + 1;
t_wstring := OSU.CP1251_TO_UNICODE( OSU.REAL_TO_STRING_FORMAT(file_data^.result_Force, 0, OSU.DOT) );
StrConcatW ( ADR(t_wstring) , ADR(csv_result) , SIZEOF(csv_result) );
csv_lenght := csv_lenght + StrLenW(ADR(t_wstring));
csv_result[csv_lenght] := 16#0000;
csv_lenght := csv_lenght + 1;
done := TRUE;
t_state := 0;
END_CASE