Announcement

Collapse
No announcement yet.

Manipulating Bytes

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

  • Manipulating Bytes

    So I have this data that's coming in though the serial port 8 bytes at a time. I'm catching them in the variable 'buf', which is 48 bytes long. The problem is, I'm having difficulty manipulating the data within this variable. Is there more documentation on this? For example, since buf[1] is really 2 bytes, how do I access just the first byte? Thanks!

    -Dan

  • #2


    In other words, once a 16 bit value is received through the serial, how can you look at just a portion of it, say just the last 8 bits?

    Comment


    • #3


      I think you may be looking for str_Ptr. Also look at the demos in the 'Picaso - STring Functions' folder

      Comment


      • #4


        I'm not sure how to use this str_Ptr function. The example in the documentation is not too clear. Also I couldn't find the picaso - STring Functioins folder. I have a uOLED Goldelox.

        Here's the basic problem, buff[1] contains 2 bytes of data and I want to switch the first with the second byte and plot that.

        Comment


        • #5


          Also I tried running the example str_Ptr program and found out that str_Ptr is not recognized by the compiler.

          Comment


          • #6


            Ah, yes, Goldelox is a bit different, here's how I do it, the first bit is a subroutine, the second bit is a code snippet
            // simulate part of str_Ptr on goldelox
            func 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


            p := 0 ;
            char := getch(Scr_String,p++) ;
            while(char != 0)
            // process character
            char := getch(Scr_String,p++) ;
            wend

            Comment


            • #7
              I'm not sure I understand this code. Have you tried running it? What does array[idx/2] do? Also why do you call getch twice?

              For Clarification, here's the code I have:


              Code:
              var buf[24]; 
              // create a 48 byte buffer 
              //--------------------------------------------------------------------------// 
              func main() 
              var chr; 
              var binbuf; 
              setbaud(BAUD_115200); 
              gfx_Set(OUTLINE_COLOUR, RED); 
              // set outline colour, 
              gfx_Cls() will draw outline 
              txt_Set(FONT_SIZE, FONT_SMALL); gfx_Cls(); 
              // clear the screen, set cursor position to 0,0 
              txt_MoveCursor(0,1); 
              // reset cursor to line 1, column 2 
              repeat 
                com_Init(buf,48,'U'); 
                repeat txt_MoveCursor(3,1); 
                print("Buffered ", (com_Count())); until(com_Count() == 7); 
                //print what was received 
                print("\nReceived [0]", [HEX] buf[1]); 
              
                if (com_Count()==7) 
                  while ((chr:= serin()) >= 0) to(COM0); 
                    putch(chr); 
                  wend 
                endif 
              forever 
              endfunc 
              //---------------------------------------------------------------------//
              And I want buf[1] to have its first and second bytes reversed.

              Comment


              • #8
                Um, I don't quite follow what you code is doing, except perhaps to demonstrate the difficulty you are having. So rather than try and explain what my code does, I have simply embedded it into your code in a way that should work (Sorry I haven't tried it). Yes, the call to getch could be moved into the while statement, but I'm trying to make it more 'readable'.

                var buf[24];
                // create a 48 byte buffer
                // simulate part of str_Ptr on goldelox
                func 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

                //--------------------------------------------------------------------------//
                func main()
                var chr;
                var binbuf;
                var p,char ;
                setbaud(BAUD_115200);
                gfx_Set(OUTLINE_COLOUR, RED);
                // set outline colour,
                gfx_Cls() will draw outline
                txt_Set(FONT_SIZE, FONT_SMALL); gfx_Cls();
                // clear the screen, set cursor position to 0,0
                txt_MoveCursor(0,1);
                // reset cursor to line 1, column 2
                repeat
                com_Init(buf,48,'U');
                repeat txt_MoveCursor(3,1);
                print("Buffered ", (com_Count())); until(com_Count() == 7);
                p := 0 ;
                char := getch(buf,p++) ;
                while(char != 0)
                print([CHR] char) ;
                char := getch(buf,p++) ;
                wend

                //print what was received
                print("\nReceived [0]", [HEX] buf[1]);
                if (com_Count()==7)
                while ((chr:= serin()) >= 0) to(COM0);
                putch(chr);
                wend
                endif
                forever
                endfunc
                //---------------------------------------------------------------------//
                Mark

                Comment

                Working...
                X