Help with Serial binary download?

DaveW

Active Member
Licensed User
Longtime 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:
B4X:
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.
 

Attachments

  • comm1.png
    comm1.png
    3.7 KB · Views: 217

taximania

Well-Known Member
Licensed User
Longtime 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 :)
 

DaveW

Active Member
Licensed User
Longtime 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:
 

agraham

Expert
Licensed User
Longtime 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.b4x.com/forum/additional-libraries/2565-byteconverter-library.html#post14218.

Something like
B4X:
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
 

agraham

Expert
Licensed User
Longtime User
IAll data are output in binary (signed integer) format, 2 bytes per integer, MSB first."
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 ...

B4X:
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
 

DaveW

Active Member
Licensed User
Longtime 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:

B4X:
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
 

DaveW

Active Member
Licensed User
Longtime User
It may have been 'wrong' but it was good enough to get me started ! I am more than happy with that :)
 
Top