Announcement

Collapse
No announcement yet.

extract part of a string

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • extract part of a string

    Hello
    I want to extract part of a string of 200 characters received by the serial port of an OLED 96 and 144. How the 4DGL language? Is there such instructions "Left, right, middle"
    Thank you for your answers

  • #2


    4DGL is not Basic, it's more like C

    So str_CopyN() is what you are after for Left and Mid, for right you will need to combine it with str_Length()
    Mark

    Comment


    • #3


      do you have any examples?
      OLED products are 96 and 144 GFX.
      The instructions STR_ .... are not recognized?

      Comment


      • #4

        Sorry, I assumed because of the nature of the question you were using Picaso (note to self, read the forum questions are posted in

        Goldelox does not have string handling builtin, you have to do it by hand.

        Here's an example that does str_mid and can easily be expanded for more string functions.


        #platform "GOLDELOX"

        #inherit "4DGL_16bitColours.fnc"

        var string1[20] ;
        var string2[20] ;

        // extract a character from a packed string
        // the first character# is 0
        func str_getch(var *array, var idx)
        var x ;
        if ((idx % 2) == 0)
        x := array[idx / 2] & 0xff ;
        else
        x := array[idx / 2] >> 8 ;
        endif
        return x ;
        endfunc

        // insert a character into a packed string
        // the first character# is 0
        func str_putch(var *array, var idx, var ch)
        var idxd2 ;
        idxd2 := idx / 2 ;
        if ((idx % 2) == 0)
        array[idxd2] := (array[idxd2] & 0xFF00 ) | ch ;
        else
        array[idxd2] := (array[idxd2] &amp; 0x00FF) | (ch << 8) ;
        endif
        endfunc

        // find a character in a packed string and return its position
        // the first character# is 0,
        // returns position, or -1 if not found
        func str_find(var *array, var ch)
        var i, j ;
        i := 0 ;
        j := ~ch ;
        while ((j != ch) &amp;&amp; (j != 0))
        j := str_getch(array, i++) ;
        wend
        i-- ;
        if (j == ch)
        return i ;
        else
        return -1 ;
        endif
        endfunc

        // create a packed substring from a packed string
        // the first character# is 0,
        // the substring is null terminated
        func str_mid(var *array, var *substr, var strt, var count)
        var i ;
        for (i := 0; i < count; i++)
        str_putch(substr, i, str_getch(array, strt+i)) ;
        next
        str_putch(substr, i, 0) ;
        endfunc

        func main()
        var i ;
        to(string1) ;
        print("Now is the Time") ; // create a string

        print([STR] string1, "\n" ) ; // print it

        i := str_find(string1, ' ') ; // find the first blank
        print("First blank at ", i+1) ;

        i := str_find(string1, 0) ; // finding the null gives returns the length
        print("\nlength=", i) ;

        str_mid(string1, string2, 4, 6) ; // get 6 characters from string1 starting at position 4 and place in string2
        print("\n|", [STR] string2, "|\n" ) ; // print the result

        repeat
        forever

        endfunc
        Mark

        Comment


        • #5
          Why equality NewID == Combuf does not work. See the program segment below
          Thank you for your help


          var combuf[3];

          var newID[3];

          to(newID) ;

          print("IDA") ;

          repeat



          com_Init(combuf, 3, ':' );

          repeat

          txt_MoveCursor(3,1);

          print("Buffer: ", ( com_Count() ));

          until(com_Count() == 3);

          print("\nRecu:", [STR] combuf );

          print("\n", [STR]newID, "\n", [STR]combuf);



          // If we reception is equal to IDA equality does not occur



          if (newID == combuf) // DOESN’T WORK ????

          print("\n New ID") ;

          endif



          endfunc

          Comment


          • #6
            2 reasons:-
            1. You can't compare strings that way, you need to use str_Match
            2. The 4th 'character' in the comms buffer may not be \0, so a compare would not necessarily work
            Mark

            Comment


            • #7


              How to use str_Match with an OLED 96 GFX. Do you have an example?
              thank you

              Comment


              • #8


                Thought it would have been simple to create with what you have now.

                What about

                // compare 2 strings
                // return 1 if identical, else return 0
                func str_match(var *array, var *array2)
                var i, j ;
                i := str_find(array, 0) ;
                if (i != str_find(array2, 0))
                return 0 ;
                endif

                for (j := 0; j < i; j++)
                if (str_getch(array, j) != str_getch(array2, j))
                return 0 ;
                endif
                next
                return 1 ;
                endfunc
                Mark

                Comment


                • #9


                  Hello,
                  If I said the last array Promo [2] the program doesn’t works "display More»
                  How many array can be reported?
                  Below the declared variables
                  cordially

                  // Déclaration Variables reception
                  var chr, e;
                  e := 0;
                  var controleID[3];
                  var idrecu[3];

                  // Création des chaines a controler
                  var newID[2];
                  var oldID[2];
                  to(newID) ;
                  print("IDN") ;
                  to(oldID) ;
                  print("IDO") ;
                  // Déclaration des variables pour controler l'ID dans la carte SD
                  var char;
                  var idmemoire[2];
                  var extraitIDmemoire[2];

                  //**********************************************************************************

                  // Variable Gestion chaine caractere
                  var LG_chaine_recu;

                  var chaine_recu[30] ;
                  var chaine_extraitID[2] ;
                  // var chaine_compare[45] ;
                  var chaine_extraitDesign[10] ;
                  var chaine_extraitHT[3];
                  var chaine_extraitTTC[3];
                  var chaine_extraitTVA[3];
                  var chaine_extraitStock[3];
                  var chaine_extraitEtiq[1];
                  var chaine_extraitPKG[3];

                  var pos_separID,pos_separDES,pos_separHT,pos_separTTC,pos_separTVA,pos_separStock;
                  var pos_separEtiq,pos_separPKG;

                  // Variable etiquette
                  var etiq[2];//,promo[2];//,image[2],video[2],solde[2];
                  var res_etiq ,res_promo ;//,res_image,res_video,res_solde;

                  to(etiq);
                  print("E1");
                  // to(promo);
                  // print("P1");


                  //*****************************************************************************

                  Comment


                  • #10


                    I can't quite follow what you are getting at here.

                    Umm, could it be that you are thinking
                    var Promo [2] ;
                    declares 2 variables Promo[1] and promo[2]?

                    That is not correct, arrays are declared in the C style, so the entries will be Promo[0] and Promo[1].
                    Mark

                    Comment


                    • #11


                      If I enable (promo) print ("P1") program NOT works. If I disable to (promo) print ("P1") the program work?
                      Question: Is there a limit in the declarations of the array?
                      Regards

                      Comment


                      • #12


                        If I enable (promo) print ("P1") program NOT works. If I disable to (promo) print ("P1") the program work?

                        Seems to work fine for me, given what has been posted.

                        But I wonder if you have many more variables, and I wonder if you are expecting to call many routines.

                        You have, what appear to be a lot of local variables, these are allocated on the stack, which by default is only 200 words/integers deep, perhaps you need to specify
                        #STACK 400
                        to increase its size.

                        Question: Is there a limit in the declarations of the array?
                        Not really, just as long as you don't use up all memory
                        Mark

                        Comment


                        • #13


                          We tried the command "#STACK 400" and we have the same problem?
                          Do you have any documentation and examples for programming 4DGL
                          Thank you for your help

                          Comment


                          • #14


                            We tried the command "#STACK 400" and we have the same problem?
                            As I said, even what you pasted worked fine for me, I just wondered if I wasn't seeing all the variables you had declared


                            Do you have any documentation and examples for programming 4DGL
                            In Workshop, click File, Samples, Picaso Designer
                            All the documentation is on the Workshop home page http://www.4dsystems.com.au/product/10/120/Development/4D_Workshop_4_IDE/
                            Appnotes are here http://www.4dsystems.com.au/appnotes/
                            Mark

                            Comment


                            • #15


                              These are all variables declared
                              The aficheur Godelox LCD144 is not a Picasso

                              #STACK 400
                              // Déclaration Variables reception
                              var chr, e;
                              e := 0;
                              var controleID[3];
                              var idrecu[3];
                              // Création des chaines a controler
                              var newID[2];
                              var oldID[2];
                              to(newID) ;
                              print("IDN") ;
                              to(oldID) ;
                              print("IDO") ;
                              // Déclaration des variables pour controler l'ID dans la carte SD
                              var char;
                              var idmemoire[2];
                              var extraitIDmemoire[2];
                              //**********************************************************************************
                              // Variable Gestion chaine caractere
                              // var LG_chaine_recu;
                              var chaine_recu[26] ;
                              var chaine_extraitID[2] ;
                              var chaine_extraitDesign[8] ;
                              var chaine_extraitHT[3];
                              var chaine_extraitTTC[3];
                              var chaine_extraitTVA[3];
                              var chaine_extraitStock[3];
                              var chaine_extraitEtiq[2];
                              var chaine_extraitPKG[3];

                              var pos_separID,pos_separDES,pos_separHT,pos_separTTC,pos_separTVA,pos_separStock;
                              var pos_separEtiq,pos_separPKG;

                              // Variable etiquette
                              var etiq[2],promo[2];//,image[2],solde[2];//,video[2];
                              var res_etiq ,res_promo ;//,res_image,res_solde;//,res_video;

                              to(etiq);
                              print("E1");
                              to(promo);
                              print("P1");
                              // to(image);
                              // print("I1");
                              // to(solde);
                              // print("S1");
                              // to(video);
                              // print("V1");

                              Comment

                              Working...
                              X