Convert LWORD to STRING

Anonymous
2013-01-04
2013-01-05
  • Anonymous - 2013-01-04

    Originally created by: 99bobster99

    I am a newbie at Codesys and trying to concatenate some time variables into text, so that I can pass this information to a server via an Ethernet socket.

    I am trying to convert this vairable "CurDate" which is an LWORD to a STRING (variable named "Text"). I am then trying to decode each digit into it's ASCII character into an array;

    VAR
       CurDate: LWORD;
       Text : STRING;
       Index : INT;
       Var1 AT %MB100 : STRING;
       Var2 AT %MB100 : BYTE;
       ascii : ARRAY[0..64] OF BYTE;
    END_VAR
    CurDate:= (DTNow.Year * 10000000000) + (DT_Month) + (DT_Day) + (DT_Hour) + (DT_Minute) + (DT_Second);  (* this value equals 20130103185505, at the moment *)
    Text := STRING(CurDate);
    FOR Index := 1 TO LEN(Text) BY 1 DO
       Var1 := MID(Text ,1,Index);
       ascii[Index - 1] := Var2;
    END_FOR;
    

    I get an Error 4268: Expression expected on the line "Text := STRING(CurDate);", I can't seem to find an "LWORD_TO_STRING()" function??

    Any assistance would be greatly appreciated.

     
  • Anonymous - 2013-01-04

    Originally created by: 99bobster99

    After digging through some manuals, I came up with this solution. It is not pretty but it works (please let me know if there is a more elegant way of doing this);

    CurDate:= DT_Year + DT_Month + DT_Day;             (* CurDate := 20130104 *)
    CurTime:= DT_Hour + DT_Minute + DT_Second;        (* CurTime := 92530 *)
    (* Convert the LWORD into 2 STRING variables *)
    Text1 := LREAL_TO_STRING(CurDate);                    (* Text1 := "20130104" *)
    Text2 := LREAL_TO_STRING(CurTime);                    (* Text2 := "92540" *)
    IF LEN(Text2) = 5 THEN
    Text2 := INSERT (Text2,'0',0);                               (* Text2 := "092540" *)
    END_IF
    (* Fill Data Array with ASCII characters representing the STRING characters *)
    Text3 := CONCAT(Text1,Text2);                             (* Text3 := "20130104092540" *)
    FOR Index := 1 TO LEN(Text3) BY 1 DO
       Var1 := MID(Text3,1,Index);
       Array_DT[Index - 1] := Var2;
    END_FOR;
    

    Everything works perfectly, I get the ASCII equivalent data in my "Array_DT" array, for each character in "Text3". Any ideas on how to make this code more compact?

     
  • shooter - 2013-01-05

    you can have a look at http://www.oscat.de nice lib

     

Log in to post a comment.