Our full technical support staff does not monitor this forum. If you need assistance from a member of our staff, please submit your question from the Ask a Question page.


Log in or register to post/reply in the forum.

Hexadecimal Numbers as variables/Interfacing raw data from an XBee


FenixRising Dec 22, 2016 12:27 AM

Hey everyone,

     So my main question is this: Is it possible to get a raw, hexadecimal number as a variable in CRBasic?.....Here comes my lengthy elaboration.

      Ok so a little background. I am feeding serial data into a universal terminal on a CR6. I know it’s reading the data frame exactly as it should, because when I bring up the Sniffer in the Terminal Emulator it reads the exact Hex values I expect. Just for reference, one data frame reads as such: “7E 00 0A 83 00 00 22 00 01 02 00 03 FF 55”. Granted I only need bytes 12 & 13 for my variable, but that’s for another day.

      I guess the issue is, CRBasic forces me to format serial data “as String”. Which automatically forces the Hex values into it’s ASCII character, which is not what I’m looking for. So my destination variable ends up looking like this: “~”. So, 7E (Hex) = 126 (Decimal) = ~ (ASCII). I believe the data stops at “~” because it views the following “00” as a null character.

      Is it possible to see the Hex string (7E 00 0A 83 00 00 22 00 01 02 00 03 FF 55) in the variable? Or some variation of that? Put simply I’m trying to extract a measurement value from a serial data string. I hope this makes at least a little sense.

     I can put some of my code here if it helps. I'm firmly seated in the struggle bus though and I need help!!!! Thank you!!!


JDavis Dec 22, 2016 09:10 PM

 This code shows a way you can use a second string to display an ASCII representation of the hexadecimal values.

'This program shows how FormatLong can be used to display the
'  contents of a binary string in a formatted hexadecimal string.
' It displays the true contents of a string, being useful for troubleshooting.
Public BinString As String
Public HexString As String * 64
Dim LongVal As Long
Dim i As Long

'Main Program
BeginProg
  LongVal = &h0a0b0c0d 'Loading some example data
  'Binstring will appear empty since the first character is still null
  MoveBytes (BinString,5,LongVal,0,4)

  Scan (1,Sec,0,0)
    'Read each character of BinString and build formatted HexString
    HexString = ""
    For i = 1 To 16
      LongVal = ASCII(BinString(1,1,i))
      HexString = HexString & FormatLong (LongVal,"%02X") & " "
    Next
  NextScan
EndProg

 


FenixRising Dec 22, 2016 09:30 PM

  Thank you good sir Davis.

  I ended up doing the following with each corresponding variable in the program scan:

     StartDelimeter = ASCII(HexString(1,1,1))
     Length(1) = ASCII(HexString(1,1,2))
     Length(2) = ASCII(HexString(1,1,3))
     FrameType = ASCII(HexString(1,1,4))
     SixteenBitAddress(1) = ASCII(HexString(1,1,5))
     SixteenBitAddress(2) = ASCII(HexString(1,1,6))
     RSSI = ASCII(HexString(1,1,7))
     Options = ASCII(HexString(1,1,8))
     NumberOfSamples = ASCII(HexString(1,1,9))
     AnalogChannelMask(1) = ASCII(HexString(1,1,10))
     AnalogChannelMask(2) = ASCII(HexString(1,1,11))
     AnalogValue(1) = ASCII(HexString(1,1,12))
     AnalogValue(2) = ASCII(HexString(1,1,13))
     Check = ASCII(HexString(1,1,14))

  It gave me the Decimal values of each Byte which was fine because I can use those numbers. Your example program is much cleaner and more effective. I'll be implementing it into my program as soon as possible.

  Thanks again!

Log in or register to post/reply in the forum.