Skip to content

Converting String to Bits

edited January 2019 in PLCnext Engineer

Hallo,

I´m having a hard time programming with the PCWorx Engineer.
There are a lot of basic FunctionBlocks/Functions meantioned in the help center and/or selectable in the programming interface, which are not implemented for the PLCnext.

Examples:

WORD_TO_BITS
When I type this in ST-Code there is a quick help (showing the function arguments order) for it but it is not working.

PULSE_GEN
German error msg:
"Die ausgewählte Ressource axcf2152:OLC unterstützt die Programmorganisationseinheit ACTF nicht"
"Die ausgewählte Ressource axcf2152:OLC unterstützt die Programmorganisationseinheit PULSE_GEN nicht"

I could add more examples...


Could you help me to find an efficient way to convert a String containing a HEX number to Bits?

The String could look like this "input;00C3" and I need to split this into Bits.

Converting the String into a Number always converts it from DEZ to whatever. I don´t know how to Handle it as HEX Numbers.
My Workarround in ST is not really efficient:

 /*parse string: input:0000KOMISCHESZEICHEN*/

//cut number from String
str_tmp:= str_in;
str_tmp:= DELETE(str_tmp,6,1);

//number String to Buf (takes number as DEZ but is really HEX)
b_cpyRdy:= STRING_COPY(str_tmp,buf_tmp,0,4);
r_tmp:= 0;

//reconvert false DEZ to real DEZ
(* Variable und alle Werte müssen vom Typ INT sein *)
FOR int_i := 0 TO 4 BY 1 DO
    int_x:= int_x+1;
    CASE TO_INT(buf_tmp[int_i])(* Ausdruck (muss einen INT Wert zurückgeben*) OF
        48: ;//0 -> muss nicht berechnet werden!
        49: r_tmp := r_tmp + 1*EXPT(anyreal16,3-int_i);//1
        50: r_tmp := r_tmp + 2*EXPT(anyreal16,3-int_i);//2
        51: r_tmp := r_tmp + 3*EXPT(anyreal16,3-int_i);//3
        52: r_tmp := r_tmp + 4*EXPT(anyreal16,3-int_i);//4
        53: r_tmp := r_tmp + 5*EXPT(anyreal16,3-int_i);//5
        54: r_tmp := r_tmp + 6*EXPT(anyreal16,3-int_i);//6
        55: r_tmp := r_tmp + 7*EXPT(anyreal16,3-int_i);//7
        56: r_tmp := r_tmp + 8*EXPT(anyreal16,3-int_i);//8
        57: r_tmp := r_tmp + 9*EXPT(anyreal16,3-int_i);//9
        65: r_tmp := r_tmp + 10*EXPT(anyreal16,3-int_i);//A
        66: r_tmp := r_tmp + 11*EXPT(anyreal16,3-int_i);//B
        67: r_tmp := r_tmp + 12*EXPT(anyreal16,3-int_i);//C
        68: r_tmp := r_tmp + 13*EXPT(anyreal16,3-int_i);//D
        69: r_tmp := r_tmp + 14*EXPT(anyreal16,3-int_i);//E
        70: r_tmp := r_tmp + 15*EXPT(anyreal16,3-int_i);//F
        //usw A,B,C...F
    END_CASE
END_FOR

int_x:= TO_INT(r_tmp);
w_out:= TO_WORD(int_x);

 

Thanks for your help,

Jimmy Pesto

Comments

  • Hello Jummy,

    If you can tell me the version of PC Worx Engineer you are using, I can look into this further. The next version of software (now called PLCnext Engineer) should be available in the coming weeks, and I hope that you will not have the same issues with the new version.

    For the string conversion, I suggest the following:

    1. Prepend the text "WORD#16#" to the string that represents the hexadecimal number. So the text "00C3" will become "WORD#16#00C3"
    2. Use the TO_WORD function to convert the text to a 16 bit number.

    Note that you can use different prefixes and conversion functions, depending on the number of hexadecimal digits you need to convert at once.

    • 2 digits = BYTE
    • 4 digits = WORD
    • 8 digits = DWORD
    • 16 digits = LWORD

    You can then refer to the individual bits in the BYTE, WORD etc. using dot notation - e.g. MyVar.X0 to refer to bit 0 in the variable MyVar.

    Hope this helps.

    - Martin.

  • Hey Martin,

    thanks for your quick response.

    I´m currently using PC  Worx Engineer 7.2.2.

    Nice, I wasn´t aware of the possibility to specify the number system within the string. It works like acharm.

     

    Is there also a way to set Bits in a Word more efficiently then in my solution?

    w_out:= w_out AND NOT SHL(bitOne,n_0);
    w_out:= w_out OR SHL(b_0,n_0);

    w_out:= w_out AND NOT SHL(bitOne,n_1);
    w_out:= w_out OR SHL(b_1,n_1);

     

    This helped a lot, greetings,

    Jimmy

  • Hi Jimmy,

    Re. software version: You can download a later version (7.2.3) from our website, maybe give this a try and see if the problems remain? If so, perhaps you can send a small project showing the problem(s).

    Re. setting bits in a word: You can do this a few ways:

    1. Use dot notation, so to set the first (least significant) bit in a word:  w_out.X0 := TRUE; With this notation, you can't use a variable to specify the bit number.

     2. Use the "bit manipulation functions" - e.g. GET_BIT(), SET_BIT(), RESET_BIT(), INVERT_BIT(). Using these functions, you can use a variable to specify the bit number.

    Hope this helps.

    - Martin.

Sign In or Register to comment.