No announcement yet.

Data conversion from serial buffer to single variable?

  • Filter
  • Time
  • Show
Clear All
new posts

  • Data conversion from serial buffer to single variable?

    I am having some confusion trying to convert what comes into the serial buffer into what I need in a single variable. My first issue was figuring out that the serial buffer isnt setup as bytes, but as words. The serial data comes in as single bytes, but are stacked in the serial buffer as words. So, to pull the data out of the stack the same way that it comes into the buffer, one has to use LObyte(buff[0]), HIbyte(buff[0]), LObyte(buff[1]), etc. When printed on the screen without modification, the decimal version of the byte is printed. So if I had 2 bytes that I wanted to convert into a real number and put into a single variable, I would use "variable := ((byte1 -48) * 10) + (byte2-48)". This works fine when the data doesnt go above base 10 numbers. When base 16 numbers (A thru F) are involved, it no longer seems to work. What further complicates things it that one byte isnt even one byte of the data, its only a nibble. So, to make a single word in a variable, I have to pull 4 bytes from the serial buffer, because each buffer byte is only one character and only has part of a real data byte. For instance, if I have a value coming into the serial buffer and print it on the screen as hex, I have to pull 4 bytes in from the serial buffer that are in decimal for and print as hex. So decimal values from the buffer 48,67,65,53 when converted into hex and printed to the screen will be 0CA5 which is the actual hex value of my data. I then need to convert that to a base 10 number and put it into a variable so that it can be used elsewhere in my program. Ive tried converting this in a few ways and it never comes out as it should. Any thoughts?



  • #2

    Wow Jason, thats one big heap of linear text !
    Tomorrow I will break it into Q/A and respond

    I'm sure all your q's have a tangible answer


    • #3

      Ok, I have a little better understanding of where im getting stuck now, but still stuck in the end. Here is what ive found...

      variable := 0x0A;
      print([HEX]variable); // The result is '000A' -CORRECT

      variable := 0b1010;
      print([HEX]variable); // The result is '000A' -CORRECT

      variable := 65;
      print([HEX]variable); // The result is '0041' -CORRECT

      variable := 65;
      print([CHR]variable); // The result is 'A' -CORRECT

      I need to calculate with anything but the decimal value, but that doesnt seem possible the way im trying. Calculating with HEX, BIN, and CHR are fine.



      • #4

        Ok, I got it. 0-9 worked by subtracting 48 to get a CHR value of a decimal number but A,B,C,D,E, and F didnt work because A isnt right after 9 in the ascii table. From A and up, you have to subtract 55 and from 9 and down you have to subtract 48. Man.... hours and hours wasted on this. I'd like to think I learned my lesson, but ive run into this in the past.


        • #5

          hi there, I send data down the coms to write files to the SD card from the PC.. in one of my messages I send a start character, 4 ASCII chars to represent the hex filesize, and a stop char this all goes into a buffer. 0x1A3F for example is a size of 6719 bytes..

          Here is the code I use to determine the DEC value of the ascii string.

          to(tempBuffer); print("$1A3F:"); //this is a test string containing an ASCII filesize
          bufptr:=str_Ptr(tempBuffer); //this holds all of the received bytes for the file size.
          str_GetC(&bufptr,&ch); //extract and ignore the first character from the buffer and auto increment bufptr
          str_GetHexW(&bufptr, &count); //Function fixed ignores the ':'
          print([HEX] count," \n"); //should be 1A3F
          print([DEC] count, " + 1 = "); // should be "6719 + 1 = 6720"
          print([DEC] count,"\n");

          I copied a few lines I actually use and added some more to help you test with it.. the code should work..

          MicroLec Industries P/L


          • #6

            Thanks Andy,
            yes that is the best method. This will work as long as you are actually receiving packets of a certain length, then reading off from the start of the buffer,
            otherwise the comms buffer just acts like a circular buffer and there will be no reference to the start position.

            var combuf[10]; // buffer for up to 20 bytes
            func main()
            txt_Set(TEXT_OPACITY, OPAQUE);
            com_Init(combuf, 16, ':' ); // set up a comms buffer, requires ':' lead in character followed by 16 bytes
            txt_MoveCursor(3,1); // reset cursor
            print("Buffered ", ( com_Count() )); // show count of chars in buffer
            until(com_Full() ); // loop until packet is available

            now process the buffer...