Announcement

Collapse
No announcement yet.

String manipulation

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

  • String manipulation

    Hello
    Im using a uLCD-32PT GFX2 device by the way.

    Im hoping that some one can shed some light on this for me. I have spent countless days trying to get my head around SIMPLE string parsing in this device. I have bumbled my way through by just simply trying different things. I have to say, its the worst device Ive ever tried to manage by streets, but Im not about to give up on it.

    Basically Im sending a 14 byte data stream to it from a processor. Part of the stream contains calender information from a RTC.

    I have successfully managed to extract the pieces of information from the stream ( after at least a week ... something that takes mere minutes using any other language)

    Please look at the code attached.

    The part of the data stream with calander information looks like this 110802094734.
    11 - year
    08 - month
    02 - day
    09 - hour
    47 - second
    34 - minute

    I use 3 global variables to hold the information:

    var curryear[1]
    var currmonth[1]
    var currday[1]

    You'll see that I print out each parameter as I get the information thus ..
    print ( "year ",[STR] curryear); - prints year 2011 ----- which is correct
    print ( "month ",[STR] currmonth); - prints month 08 ----- which is correct
    print ( "day ",[STR] currday); - prints day 02 ------ which is correct

    BUT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! and this is the kicker

    print ([STR]currday, "/" ,[STR]currmonth,"/", [STR]curryear);

    prints 02/0802/200802 ---- only the currday variable has printed correctly

    Go figure .. I am at a loss how a variable can hold one lot of information and then without being altered can produce something entirely different.

    Unfortunately the information available for this language is scant to say the least.
    Basically its a trial and error type of education whichis why it takes so long.

    Any information would be greatly appreciated.

    Thank You




    Attached files

  • #2
    your arrays:-
    var curryear[1]
    var currmonth[1]
    var currday[1]

    are only 1 word, which can contain 2 bytes/characters.

    Make your arrays large enough to hold the string.

    eg for current year, you have "year " (5 characters) the 4 characters for the number, then an ascii terminator (0) will be placed at the end by the print function.

    So, you need 5+4+1 characters, and seeing as the characters are packed 2 per array element, you need:-

    var currentyear[5]; // 10 chacter array for "year xxxx\0"

    Same applies to the others, calculate the maximum you need and change them.

    Regards,
    Dave

    Comment


    • #3


      Thanks Dave

      You may be onto something here, but I may have also provided the wrong information.
      The variable dont have any name entered.

      So the year variable holds "2011
      The month "08"
      The day "02"

      Why does it print them perfectly as I parse them but then when I recall them they are wrong ????

      Anyway of to try your recommendation.

      Thanks Again

      Jeff

      Just tried it ... WORKS a treat ... many thanks once again

      Comment


      • #4


        Oh I see I think, you are just working with numbers
        ,
        print ( "year ",[STR] curryear);
        print ( "month ",[STR] currmonth);
        print ( "day ",[STR] currday);
        the code above will print stored strings

        If your just working with raw numbers the code should be just

        var curryear;
        var currmonth;
        var currday;

        print ( "year ",curryear); - prints year 2011
        print ( "month ",currmonth); - prints month 08
        print ( "day ",currday); - prints day 02

        or

        print (currday, "/" ,currmonth,"/", curryear);


        If the numbers are HEX, try this

        print ( "year ",[HEX4] curryear); - prints year 2011
        print ( "month ",[HEX2]currmonth); - prints month 08
        print ( "day ",[HEX2]currday); - prints day 02
        Regards,
        Dave

        Comment


        • #5


          I see our posts crossed, glad you got it working
          Regards,
          Dave

          Comment


          • #6


            Why does it print them perfectly as I parse them but then when I recall them they are wrong ????
            I think that's because you are corrupting memory... eg.

            consider a linear bytewise memory model where your variables are in memory like this

            address variable length
            0 year 2
            2 month 2
            4 day 2
            6 unused

            If you assigned "2011" to year and printed it, you would get "2011", but you would have corrupted Month and Day as memory would contain "2011n" where n is 0x00

            If then assigned "08" to month and printed it, you would get "08", but you would have corrupted Day and the entire memory would contain "2008n" where n is 0x00

            If then assigned "01" to day and printed it, you would get "01", but you would have corrupted the unused area, the entire memory would contain "200801n" where n is 0x00

            So, if you then printed with

            print ([STR]currday, "/" ,[STR]currmonth,"/", [STR]curryear);

            You would get what you got.

            Have a look at some of the string examples, hopefully it will make sense in a little while
            Mark

            Comment


            • #7


              meldavia wrote: Oh I see I think, you are just working with numbers
              ,
              print ( "year ",[STR] curryear);
              print ( "month ",[STR] currmonth);
              print ( "day ",[STR] currday);
              the code above will print stored strings

              If your just working with raw numbers the code should be just

              var curryear;
              var currmonth;
              var currday;

              print ( "year ",curryear); - prints year 2011
              print ( "month ",currmonth); - prints month 08
              print ( "day ",currday); - prints day 02

              or

              print (currday, "/" ,currmonth,"/", curryear);


              If the numbers are HEX, try this

              print ( "year ",[HEX4] curryear); - prints year 2011
              print ( "month ",[HEX2]currmonth); - prints month 08
              print ( "day ",[HEX2]currday); - prints day 02



              Dave .. while I have your attention.
              The data stream is 14 bytes of raw numbers.
              I am selecting 9 elements from the stream.

              Some I have to turn into printable data, others I have to use as raw numbers.
              Obviously I have manged to do it but if I could bother you for 5 secs, how would you go about parsing the stream .. especially for the raw number information.

              I dont want you to do into this indepth.
              I see you suggest using variables without [x]
              So what code would you use for that .. eg ... strGetWord etc etc.
              Sorry for taking the easy way out here .. but believe me .. I have been at this for a week.
              This is my first integration of this product .. hopefully it gets easier from here

              Thanks for your interest

              Jeff

              Comment


              • #8


                Might be easiest to use buffered comms, and when the count gets to 14 just start pulling them out of the buffer with serin and do whatever you need to do with them.

                Have a look at this example, it should help:-
                http://www.4dsystems.com.au/code/index.php?node=66&id=79
                Regards,
                Dave

                Comment


                • #9


                  meldavia wrote: Might be easiest to use buffered comms, and when the count gets to 14 just start pulling them out of the buffer with serin and do whatever you need to do with them.

                  Have a look at this example, it should help:-
                  http://www.4dsystems.com.au/code/index.php?node=66&id=79

                  Hi again Dave
                  Ok I have looked through that example. Im fine with it but it doesnt provide an example of parsing the stream only printing the entire received stream.

                  A question I have is about the putch command. If used and directed to a variable does it append the variable ???

                  My guess is no, as the PDF says "prints single characters to the currently selected output stream" .

                  The only criteria in parsing a data stream is one of these two:

                  Select a byte and

                  1. write it to a variable.
                  2. append a variable.

                  Is there an easy way to do this ???

                  I have done it but Im positive there would be an easier way.

                  Thank you

                  JEff

                  Comment


                  • #10


                    Was this issue ever resolved?



                    I need to do the same thing and I'm not having much luck storing the buffered serin string as a variable. I can print what I receive but that doesn't help me use it.

                    I need to be able to receive a string that varies between 16 and 19 characters, pluck the important parts out and do some stuff to the GPIO according to what is received.



                    I've spent the day scanning through every page on this sub forum, the serial examples, the programmers reference, and the the internal fuctions. I don't think I'm any farther along than where I started. ~)



                    I'm not asking anyone to do it for me, but good god I could use a shove in the right direction.



                    Thanks in advance.

                    Comment


                    • #11


                      It's not clear what you are after.



                      There are also some string usage examples in the codebase http://www.4dsystems.com.au/code/index.php?node=78
                      Mark

                      Comment


                      • #12


                        Hopefully something here will help you http://4d.websitetoolbox.com/post?id=5681797
                        Mark

                        Comment


                        • #13


                          #platform "uLCD-32PT_GFX2"

                          var buffer[100];
                          var p,n,num,col;

                          func main()
                          to(buffer); print("Station #3 = Yellow");
                          p:=str_Ptr(buffer);
                          n:=0;

                          while (n

                          Comment


                          • #14


                            I've been playing around with uCs off and on since '98 and just spent a couple of days feeling like it was day one. You would think one of you basteds would have quit laughing long enough to tell me I was barking up the wrong tree

                            I'm not sure how I got it in my head that writing to a variable required the to() statement in Picaso but I sure explored that path thoroughly.





                            I'm sure this snippet has errors but I'll post it anyway in case some other poor soul stumbles across this thread.



                            HTML Code:
                            #platform "uLCD-32PT_GFX2"
                            #inherit 
                            "4DGL_16bitColours.fnc"
                            
                            
                            var combuf[20];
                            func main()
                            var 
                            n,p,num,col, ch;
                            
                            
                            repeat
                            
                            gfx_Cls();
                            txt_Set(FONT_ID, FONT2);
                            
                            txt_Set(TEXT_COLOUR, BLUE);
                            
                            txt_MoveCursor(1,0);
                            com_Init(combuf, 14, 'S' 
                            );                         
                            //Set Comm with qualifier
                            
                            
                            
                            repeat
                            if 
                            (com_Sync())                             
                            // Check for 
                            qualifier
                            
                            txt_Set(TEXT_COLOUR, 
                            RED);
                            
                            txt_MoveCursor(3,1);
                            
                            putstr("Station 
                            Reporting");
                            
                            endif
                            
                            
                            
                            txt_MoveCursor(5,1);
                            
                            print("Buffered ", ( com_Count() ));        
                            // Debug 
                            Bufffer
                            
                            pause(100);
                            
                            until(com_Full());                              
                            // Shift characters in loop until full
                            
                            p:=str_Ptr(combuf);                                 
                            //Now that Buf is full create pointer
                            
                            txt_MoveCursor(7,1);
                            pause(100);
                            
                            //putstr(p);                                          
                            //debug p - prints but clips leading two characters ???
                            
                            num:= 
                            (str_GetByte(p+8));                           
                            //Pluck reporting Station number from p
                            col:= 
                            (str_GetByte(p+12));                          
                            //Pluck reporting station color from p.
                            
                            txt_MoveCursor(9,1);
                            
                            
                            
                            n := 
                            15;                                            
                            //loop count to shift characters for print.
                            
                            while(n--)
                            if( (ch := serin()) 
                            < 0) ch := 
                            '';              
                            //print
                            
                            putch(ch);
                            wend
                            
                            
                            
                            
                            com_Reset();                                        
                            //Clear combuf
                            txt_MoveCursor(11,1);
                            
                            print("Station # 
                            ",[CHR]num);                       
                            //Vars should not be cleared with reset. Prints 0
                            
                            txt_MoveCursor(13,1);
                            print("Color= 
                            ",[CHR]col);
                            txt_MoveCursor(15,1);
                            
                            
                            
                            ch := 0;
                            
                            while (ch != 13)
                            if ((ch := 
                            serin()) != -1)
                            
                            endif
                            wend
                            
                            gfx_Cls();
                            txt_Set(FONT_ID, FONT2);
                            
                            txt_Set(TEXT_COLOUR, YELLOW);
                            
                            print("Resetting");
                            pause(1000);
                            forever
                            
                            endfunc

                            Comment


                            • #15


                              Glad you got it going Jeff,
                              was trying to find a bit of time to do you a demo but it looks like you have cracked it.

                              'to(mystring)' in 4DGL is approx the same as sprintf(mystring, "xyz

                              and all strings are word aligned and packed 2 bytes per var location which is confusing at first
                              Regards,
                              Dave

                              Comment

                              Working...
                              X