LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

how to read 16-bit signed integer in binary format with 2 byte sync. preceding??

A flowmeter provides data as 16-bit signed integer in binary format with 2 byte sync. preceding
 
I got i small problem by understanding the synchronasation.
 
I read the data provided by the flowmeter, and then write it to a binaryfile.dat. i need to convert the data to decimal format. is it better to convert the data before or after writing it to the binary file. Coz i need to read the data out again for calculations and save it again in a new file.
I understand that the synchronization  help one to see where to begin reading the usable data. is it right?
 
Need any suggestions
Thank you !
Zamzam
HFZ
0 Kudos
Message 1 of 9
(6,738 Views)


@HFZ wrote:
A flowmeter provides data as 16-bit signed integer in binary format with 2 byte sync. preceding
 
I got i small problem by understanding the synchronasation.
 
I read the data provided by the flowmeter, and then write it to a binaryfile.dat. i need to convert the data to decimal format. is it better to convert the data before or after writing it to the binary file. Coz i need to read the data out again for calculations and save it again in a new file.
I understand that the synchronization  help one to see where to begin reading the usable data. is it right?
 
Need any suggestions
Thank you !
Zamzam


Basically you want to search for two times 0x7F in the datastream. If you find it you check if the next byte is also 0x7F and if so this and the preceeding byte are actually the sync and the first 0x7F was part of a previous data word. Now with the sync found and since your data seems to be in Big Endian which is what LabVIEW prefers, you can simply Typecast the rest of the string into an array of int16 or uInt16 depending on the singedness of the data. If your incoming stream after the sync contains more than 2 bytes you will end up with an array of 16bit integers in which every second value should be a sync word. The words in between are your real measurement values.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 2 of 9
(6,723 Views)

That was a paradise off an answer!

Thank you very much Rolf Kalbermatter

Zamzam

HFZ
0 Kudos
Message 3 of 9
(6,705 Views)

HI Ray

I have developed the program after your answer,

i search the input string for 7F7F and, then i take the next byte and compare it with 7F, if its true then i got a wrong Sync. so there i take the input string again and saerches for the three bytes to remove them and take the last byte coz that must be a useable data. typecast it to to 16 bit signed integer and initialize it into an array  adds a timestamp to the data value and  plot it out in a wavechart.

As you can see in the jpg.image i've made a probe to compare the dataflow, and they seems to be in binary format  like assumed. But i f you look at the panel in the Front Panel, you see som hex data. So i got that problem that i cant compare with the strings "7F7F" ect. 

I'm not shure that my program is right made. so If you can give a look , that would be helpfully!

Zamzam 

 

HFZ
Download All
0 Kudos
Message 4 of 9
(6,691 Views)

One more thing that i'm thinking about is: the order of reading the 4 bytes. If u look at  the datasheet in the first message (with the sync. example) 

Do i have to revert the bytes,  before converting the data for plotting them to a waveform graph.

Zamzam  

HFZ
0 Kudos
Message 5 of 9
(6,690 Views)


@HFZ wrote:

One more thing that i'm thinking about is: the order of reading the 4 bytes. If u look at  the datasheet in the first message (with the sync. example) 

Do i have to revert the bytes,  before converting the data for plotting them to a waveform graph.

Zamzam  



Exactly NOT! As I mentioned in the earlier post since the data seems to be in Big Endian format already this is perfectly right for the LabVIEW Typecast fucntion, since LabVIEW also assumes Big Endian data when unflattening data streams. So no need to swap Bytes.

Attached VI is how I would do it with some bells and whistles but your specific device may have some complications this won't deal with properly such as spurious (or dropped) bytes in the data stream.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
Message 6 of 9
(6,685 Views)
HI Rolf.k.
Thank you for the small program it was helpfull.
 
You got right about that proberly there  will be conflict with some spurios data, I can already detect that when writing the data to a spreadsheet file.
I writes the data in such a way, that in each line there will be a date, a timestamp, a tab and a timestamp at the end. That means two columns.
 
When i set given samplerate up, that controls the rate of the data outflow from the device, (1,56 Hz - 200 Hz),   the data file that i write to , looks unorderet.
 i get more than one timestamp and severel datavalues in every line and so on down the spreadsheet file.
 
Now the question is: Could it be that the function that writes the data to the file,  can't handle the speed of the dataflow in such a way that the time stamp cant follow with the data flowspeed. so i'm trying to set the timestamp to be  with fractions of the seconds by adding the unit (<digit>) in the timestamp icon but its not working. Meaby when i take the fractions off a second within the timestamp i can get every timestamp with its right data value. Am i in deeb water or what do You mean!??
 
AAttached Pics part of program and a logfile over data written to file
 
regards
Zamzam
HFZ
Download All
0 Kudos
Message 7 of 9
(6,650 Views)

sorry my bad about the data file,

There will be a date and timestamp and then a datavalue, in the file. Two coloumns.

example :

26-01-2006.09:38:52:498             -256 

26-01-2006.09:38:52:503             -154

26-01-2006.09:38:52:508             32

26-01-2006.09:38:52:513             214

HFZ
0 Kudos
Message 8 of 9
(6,649 Views)


@HFZ wrote:

sorry my bad about the data file,

There will be a date and timestamp and then a datavalue, in the file. Two coloumns.

example :

26-01-2006.09:38:52:498             -256 

26-01-2006.09:38:52:503             -154

26-01-2006.09:38:52:508             32

26-01-2006.09:38:52:513             214



If you get several valid data in one line this is not SPURIOUS data in the stream. The loop is setup such that it reads whatever is on the serial port and then converts it to an ARRAY of values. If your device is set to generate lots of data it is very likely that this routine receives several values in each iteration. So your values array is in fact an array of several values and you convert it into a spreadsheet string and append it to the line. This will give you a timestamp, a tab and 0,1, or more tab separated values per line.

To get what you want you will need to add more logic. If no  data has been received do NOT write anything to the file. If more than one data value has been received you will have to decide if you want to write them all to the file with each on its own line, or if you only wnat to pick the last value from the array and write that one out.

Spurious data is when an extra byte shows up in the stream or one byte is dropped for whatever reason.

Rolf Kalbermatter
Rolf Kalbermatter  My Blog
DEMO, Electronic and Mechanical Support department, room 36.LB00.390
0 Kudos
Message 9 of 9
(6,636 Views)