Help with Serial binary download?

Discussion in 'Questions (Windows Mobile)' started by DaveW, Nov 22, 2008.

  1. DaveW

    DaveW Active Member Licensed User

    I have been trying to use the SerialEx DLL to download data from a proprietary datalogger. I have done it in the past to a PC but after a couple of hours of floundering in the dark, I have come to the conclusion that it needs a better man than I to get the thing going on a device.

    The data is sent in binary form. The relevant bits from the datalogger manual are: "After receiving an S from the PC, the instrument will transmit 32 signed integers as 64 bytes and wait for the next character S from the PC. .... All data are output in binary (signed integer) format, 2 bytes per integer, MSB first."

    I know I can get data, I send an S and see 'stuff' on the device - but it is not in a format that I can equate to the description in the manual or past experience with the same data on a PC. I have attached a screenshot of my test program showing what comes in.

    Somewhere at the very start of this datastream must be the integer 32700 followed by 1, 1, 20, 0, 100.

    The lines in the screenshot are produced by:
    Code:
    Buffer()=com.InputArray
    For i = 0 To ArrayLen(Buffer())-1
        ReceivedString=ReceivedString & 
    Chr(Buffer(i))
    Next
    ReceivedString=ReceivedString & 
    " - "
    For i = 0 To ArrayLen(Buffer())-1
        ReceivedString=ReceivedString & 
    Asc(Buffer(i))
    Next
    listbox1.Add(ArrayLen(Buffer()) & 
    "[" & ReceivedString & "]")
    I would be most grateful to anyone who can make any useful suggestions about how I can read this data.
    David.
     

    Attached Files:

  2. taximania

    taximania Well-Known Member Licensed User

    32 signed integers as 64 bytes, is that not 2 bytes per integer ?

    32700 is '7F BC' in Hex, or a 'square' and a '1/4' as two bytes.

    That is among the first 2 characters of your screen shot.

    I think it's reading the data Ok.

    Hope you get my drift :)
     
  3. DaveW

    DaveW Active Member Licensed User

    Thanks! I think I need to study the codes a bit more...

    It's a good job I don't do this for a living - oh wait - I do :sign0148:
     
  4. agraham

    agraham Expert Licensed User

    Like Taximania says, it looks like you are getting the data OK, you just need to turn it into usable numbers. Try the Int16FromBytes method in my BytesConverter library http://www.basic4ppc.com/forum/additional-libraries/2565-byteconverter-library.html#post14218.

    Something like
    Code:
    Dim Bytes(0)
    Dim Data(32)
     ... 

    Bytes() = SerialEx.InputArray() 
    ' this is simplistic, all the data may not arrive in one read

    For i = 0 to 31 
      Data(i) = Conv.Int16FromBytes(Bytes(), i+i)
    Next
     
  5. agraham

    agraham Expert Licensed User

    I just noticed that the data is Big-endian. You will need to shuffle the bytes, this is probably not the most elegant way of tackling it but ...

    Code:
    For i = 0 to 31 
      temp = Bytes(i)
      Bytes(i) = Bytes(i+
    1)
      Bytes(i+
    1) = temp
      Data(i) = Conv.Int16FromBytes(Bytes(), i+i)
    Next
     
  6. DaveW

    DaveW Active Member Licensed User

    Hi Andrew,

    Thank you for helping with this. Using your BytesConverter and the sample code I can now get valid numbers out of the datalogger.

    I would like to point out (for others that might find this posting useful) that the sample code did not work 'out of the box'. I had to change it to:

    Code:
    Bytes() = com.InputArray 
    posn = 
    0 
    For i = 0 To 31
         temp = Bytes(posn)   
    'swap the bytes to make them LSB first
         Bytes(posn = Bytes(posn+1)
         Bytes(posn+
    1) = temp
         Data(i) = Conv.Int16FromBytes(Bytes(), posn)  
    'convert the next 2 bytes to an integer
       posn = posn + 2  'move to the next integer position
    Next
     
  7. agraham

    agraham Expert Licensed User

    :signOops: Sorry! I normally test code fragments, in this instance I didn't with the inevitable result that it was wrong.
     
  8. DaveW

    DaveW Active Member Licensed User

    It may have been 'wrong' but it was good enough to get me started ! I am more than happy with that :)
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice