Announcement

Collapse
No announcement yet.

picking out data from a string

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

  • picking out data from a string

    hi every 1.

    Basically I have a 4 byte string coming in from my "Serin". I store this string to a "var" e.g "var incoming". Storing the string isn't a problem but I can't seem to split up each byte as an individual byte and assign a pointer to it.

    So var incoming = 3982. And I need to be able to read each byte individually. I.e
    a=3 b=9 c=8 d=2.

    Hope I've been clear. I'm very new to all of this, Thanks!

  • #2


    See this thread, it may help.....http://4d.websitetoolbox.com/post/four-bytes-to-BCD-String-5393719
    Keep in mind that the 4DGL uses 16 bit integers, so to hold 4 bytes would require two of them.....
    _______________
    Best Regards,
    Howard

    Comment


    • #3


      Just read your question a bit more carefully. Are you saying the value coming in is already in it's BCD form? For example the "value" 3982 comes in as four bytes, first byte = 3, second byte =9, third = 8, fourth =2? Or are they coming in as Ascii 51, 57, 56, 50?
      Or, are they coming in as 4 bytes = 0,0,15,142?
      _______________
      Best Regards,
      Howard

      Comment


      • #4

        Sorry, The "3982" is coming in as Ascii "51, 57, 56, 50"I'm then converting that into CHAR using [CHR] so when i print "incoming" It prints as "3982".
        Its the "3982 that I want to break down and assign a "var" to e.g: A=3, B=9 C=8, D=2.

        Here is the code I have so far..
        //=====================================#platform "uLCD-43PT_GFX2"

        func main() var imcoming;
        gfx_Cls(); txt_Set(FONT_SIZE, FONT2); print ("Serial Input Test\n");
        to(COM0); print("serial input test:\n");

        // now just stay in a loop repeat
        imcoming := serin(); if (imcoming != -1) to(COM0); print([CHR]imcoming); endif foreverendfunc//=====================================

        Comment


        • #5


          Well, once you have the 4 bytes (say, in an array called temparray), then

          A:= temparray(0)-'0'; // This is same as 51-48B:= temparray(1)-'0';C:= temparray(2)-'0';D:= temparray(3)-'0';
          _______________
          Best Regards,
          Howard

          Comment


          • #6


            Thanks for the quick reply but now im completely lost...
            So now i need to make an array called temp array.How do i put the four bytes in this array?
            Thanks dude!

            Comment


            • #7
              You don't "need" to put it in an array, it is just a matter of convenience. you could do so as simply as:
              func main() var imcoming; var bytecount; var barray(4); var A,B,C,D;
              gfx_Cls(); txt_Set(FONT_SIZE, FONT2); print ("Serial Input Test\n");
              to(COM0); print("serial input test:\n");
              // now just stay in a loop repeat imcoming := serin(); if (imcoming != -1) barray(bytecount++) := imcoming; to(COM0); print([CHR]imcoming); if(bytecount >=4) bytecount := 0; A:=barray(0)-'0'; B:=barray(1)-'0'; C:=barray(2)-'0'; D:=barray(3)-'0'; endif
              endif foreverendfunc
              In reality, you should be doing some more extensive message validation. For example, are there any other symbols or control codes that precede the four bytes that you are interested in?
              _______________
              Best Regards,
              Howard

              Comment


              • #8


                Oh Oh Oh, I see now, Thanks. No not as such, i don't think ill be using and symbols or other control codes.. But if i were to would i be able to set up an "if statement" to do so?
                For some reason when i try to compile the code you wrote i get the following error... I cant seem to see the problem? Any ideas?

                Error: semicolon expected after array declarations, got ( (line 5 column 9 fileertest.4DG)Fatal error: errors in first pass(1), can't continue
                Thanks again dude!

                Comment


                • #9


                  Yeah, Sorry, it should be:
                  var barray[4];
                  Note the [] instead of ()
                  _______________
                  Best Regards,
                  Howard

                  Comment


                  • #10


                    Yes i did think that and i did change it earlier but then i got this error??
                    Error: left side of expression is not a pointer or variable (missing LVAL) (line 18 fileertest.4DG)Error: expected a semicolon following previous expression (line 18 fileertest.4DG)Error: syntax error in expression ? ':=' (line 18 fileertest.4DG)Error: expected a semicolon following previous expression (line 18 fileertest.4DG)Error: statement has no effect (line 18 fileertest.4DG)

                    Comment


                    • #11


                      Keep in mind that RS-232 is not packet based, therefore there is no inherent checksum or message validation occurring.
                      For example, let's say that you want to send to the display the following numbers:
                      12345678901234567890
                      With no message markers (BOF, EOF, # signs, etc), the byte stream looks like:49,50,51,52,53,54,55,56,57,48, 49,50,51,52,53,54,55,56,57,48
                      Now let's say your sending the data, but a glitch happens during the transmission of the first 48 (the zero in the third number) and a com error occurs. You reset the buffer/ serial port, etc and continue to receive data. But, now your byte stream is corrupted, and you can't be sure that when you begin capturing data that your "word" alignment is correct. You need some method of re-syncing the byte stream data.
                      I always recommend that as a minimum, you employ some kind of packet format. If you are only transmitting Ascii, this simplifies things as you can use control codes ( values less than, say 27 and greater than 127) to define the start (and end, for variable length packets) of your message. Also, if your bandwidth allows, an additional checksum within the message helps to detect multi-bit errors that may not be caught by using parity.
                      For example, a message to transmit 4 bytes could be :02 49 50 51 52 202
                      Where 02 is the start of a message, then '1' '2' '3' '4', then a checksum which is the 4 bytes added together, modulo 256.
                      Then, in your loop, you discard all incoming bytes until you get the 02, buffer the next 4 bytes, then validate the message (4 bytes) to the next byte that comes in, which should be a checksum. If it does not add up, then throw the massage away....
                      _______________
                      Best Regards,
                      Howard

                      Comment


                      • #12
                        Same thing, all the () needs to be []
                        func main() var imcoming; var bytecount; var barray[4]; var A,B,C,D;
                        gfx_Cls(); txt_Set(FONT_SIZE, FONT2); print ("Serial Input Test\n");
                        to(COM0); print("serial input test:\n");
                        // now just stay in a loop repeat imcoming := serin(); if (imcoming != -1) barray[bytecount++] := imcoming; to(COM0); print([CHR]imcoming); if(bytecount >=4) bytecount := 0; A:=barray[0]-'0'; B:=barray[1]-'0'; C:=barray[2]-'0'; D:=barray[3]-'0'; endif endif foreverendfunc
                        _______________
                        Best Regards,
                        Howard

                        Comment


                        • #13


                          Hi Howard,Managed to pick up some great things here.
                          Now I totally see what your talking about when it comes to aligning the code in the case of an error (As a minimum).
                          So, say I was going to use 02 as my control byte,
                          would it be done a little something like this?
                          if(incoming == 02) if (imcoming != -1) barray[bytecount++] := imcoming; if(bytecount >=4) bytecount := 0; A:=barray[0]-'0'; B:=barray[1]-'0'; C:=barray[2]-'0'; D:=barray[3]-'0'; endif endif endif
                          Thanks again

                          Comment


                          • #14
                            I also have another question that im yet to work out.
                            How would i go about getting the 4dgl to split the string from "3982 A3-B9-C8-D2" to "A=39 B=82".
                            I tried to do this but i failed..
                            func main() var imcoming; var bytecount; var barray[4]; var A,B,C,D;
                            gfx_Cls(); txt_Set(FONT_SIZE, FONT2); print ("Serial Input Test\n");
                            to(COM0); print("serial input test:\n");
                            // now just stay in a loop repeat imcoming := serin(); if (imcoming != -1) barray[bytecount++] := imcoming; to(COM0); print([CHR]imcoming); if(bytecount >=4) bytecount := 0; A:=barray[0-1]-'0'; B:=barray[2-3]-'0';
                            endif endif foreverendfunc

                            Comment


                            • #15


                              I guess I need to ask, what are you sending, what does it represent, and how do you want to handle it?
                              For example, if you are sending "3982" via rs-232, what is your end goal? Do you want a variable that is equal to 3982? Do you want two variables, first equals 39, second equals 82? Or four variables of 3,9,8, and 2?
                              I was working on a state machine example on how to parse a message, but before I go any further I would need to know what is coming down the RS-232 line and how it should be parsed out.....
                              _______________
                              Best Regards,
                              Howard

                              Comment

                              Working...
                              X