Converting an ASCII value to a character

M0SNR
2019-04-25
2019-04-27
  • M0SNR

    M0SNR - 2019-04-25

    Hi,

    I'm struggling. I'm more of an occasional CoDeSys user rather than a professional.

    What I am trying to do is to read serial characters and append them to a string. When I read a character from the input object, it is an ASCII value (g = 103 etc.) So I need to convert that value to the character "g" and then concat it to the string.

    I've spent ages Googling and not found an answer so I thought I'd ask here.

    Any suggestions very welcome.

    Thanks in advance,

    Richard

     
  • JAPIB

    JAPIB - 2019-04-25

    I propose a function :


    FUNCTION F_ASCII_TO_STRING : STRING(1)

    VAR_INPUT
    InputVal: UINT;
    END_VAR

    VAR
    Caracter:STRING(1);
    END_VAR


    CASE InputVal OF
    48:Caracter:='0';
    49:Caracter:='1';
    50:Caracter:='2';
    51:Caracter:='3';
    52:Caracter:='4';
    53:Caracter:='5';
    54:Caracter:='6';
    55:Caracter:='7';
    56:Caracter:='8';
    57:Caracter:='9';
    65:Caracter:='A';
    66:Caracter:='B';
    67:Caracter:='C';
    68:Caracter:='D';
    69:Caracter:='E';
    70:Caracter:='F';
    71:Caracter:='G';
    72:Caracter:='H';
    73:Caracter:='I';
    74:Caracter:='J';
    75:Caracter:='K';
    76:Caracter:='L';
    77:Caracter:='M';
    78:Caracter:='N';
    79:Caracter:='O';
    80:Caracter:='P';
    81:Caracter:='Q';
    82:Caracter:='R';
    83:Caracter:='S';
    84:Caracter:='T';
    85:Caracter:='U';
    86:Caracter:='V';
    87:Caracter:='W';
    88:Caracter:='X';
    89:Caracter:='Y';
    90:Caracter:='Z';
    97:Caracter:='a';
    98:Caracter:='b';
    99:Caracter:='c';
    100:Caracter:='d';
    101:Caracter:='e';
    102:Caracter:='f';
    103:Caracter:='g';
    104:Caracter:='h';
    105:Caracter:='i';
    106:Caracter:='j';
    107:Caracter:='k';
    108:Caracter:='l';
    109:Caracter:='m';
    110:Caracter:='n';
    111:Caracter:='o';
    112:Caracter:='p';
    113:Caracter:='q';
    114:Caracter:='r';
    115:Caracter:='s';
    116:Caracter:='t';
    117:Caracter:='u';
    118:Caracter:='v';
    119:Caracter:='w';
    120:Caracter:='x';
    121:Caracter:='y';
    122:Caracter:='z';
    ELSE
    Caracter:='';
    END_CASE;

    F_ASCII_TO_STRING:=Caracter;

    How to use it ?

    PROGRAM PLC_PRG
    VAR
    ValNum:UINT;
    Result:STRING;
    END_VAR


    Result:=F_ASCII_TO_STRING(ValNum);

    Hoping this will meet your needs

    BR

     
  • Comingback4u

    Comingback4u - 2019-04-25

    May not be the best solution but what you could do is:

    VAR
        sTempString : STRING;
        pConvertString : POINTER TO BYTE;
        bySerialByte : BYTE;
    END_VAR
    //First get the address of the temporary string to store your character
    pConvertString := ADR(sTempString);
    //Second assign the decimal value to the temporary string
    pConvertString^ := bySerialByte;
    //Third "sTempString" now equals the ascii character and you can concat it to something else
        //Add your code to concat to another string example: sOriginalString := CONCAT(sOriginalString, sTempString);
    

    You could put this in a function or OSCAT might already have a function to do something like this.

     
  • dkugler

    dkugler - 2019-04-25

    Originally created by: D. Kugler

    why you are using workarounds, when the util Lib does, what you are searching for
    After conversion do the concat...

    IMG: wordAsString.JPG

     
  • Comingback4u

    Comingback4u - 2019-04-25

    Is that library available with that function in Codesys version 2.3 where this question was posted? I don't know what libraries are available in 2.3 since I don't use it is why I ask. My answer was a way to accomplish it without caring about what library is available in 2.3.

     
  • JAPIB

    JAPIB - 2019-04-26

    For D.Kugler
    This library shown in the post is available with CODESYS V3.5.
    The library Util available with CoDeSys V2 don't offer this function. The only conversion functions are BCD functions (INT_TO_BCD, BCD_TO_INT).

    For Cmingback4u :
    Your solution is simpler and more elegant than mine.

    BR

     
  • dkugler

    dkugler - 2019-04-27

    Originally created by: D. Kugler

    Sorry i'm thinking and working most in V3.5 and only sometimes in V2.3! Forget my post or use it in V3

     

Log in to post a comment.