Solved

How to interpret DWORD in vb.net

Posted on 2008-10-22
14
2,648 Views
Last Modified: 2012-06-27
Hi
I have spent hours reading up on this subject and it's done my head in - please help

I am accessing data from a remote sensor via TCP socket - it's all working fine until I get to intertpret the data.

The incoming data is defined by the manufacturer as

typedef struct
{
WORD SyncHeader;
WORD SyncID;
DWORD UnitID;
} SyncStruct;
SyncHeader is always 0xCAC8
SyncID is a message sequence number
UnitID is the device identification number

How do I access the data via VB.NET? Specifically I want the UnitID as an integer

Many thanks for your help
0
Comment
  • 9
  • 5
14 Comments
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782578
If I am not mistaken the DWORD is an unsigned Integer anyway, so UInt32 as datatype.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782593
In other words, will something like this work out for you.

Dim unitID AS UInt32 = SyncStruct.UnitID

You will have to adjust for how the SyncStruct actually shows up in your code.  You indicated you are receiving data fine, just need to interpret so would be dependent on how the data is coming across to you if that makes sense.
0
 

Author Comment

by:RadicalSoftwareSolutions
ID: 22782705
Thanks for reply

I can't use the struct as it is at the moment - it isn't in vb.net format. I can access the first SyncHeader as strMsg.substring(0,1) and strMsg.substring(1,1) which return hex values. What ned to know is how to interprete the 4 byte dword as integer
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782862
Ok, so you have the four HEX values...

If you have the values in order, meaning byte 4 is the last HEX value if value written out like this:
0xCAC81112

You would write this as follows:
Byte1 * 16777216 '256^3
Byte2 * 65536 '256^2
Byte3 * 256 '256^1
Byte4 * 1 '256^0

Then you would sum all those up to end up with an integer value.
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782932
And you will first need to convert the HEX to its decimal value so this is what this looks like to be complete.  Try this in VB.

Dim unitIDParts(3) As String
        unitIDParts(0) = "0A"
        unitIDParts(1) = "11"
        unitIDParts(2) = "12"
        unitIDParts(3) = "13"
 
        Dim maxParts As Integer = unitIDParts.Length - 1
        Dim intValue As Integer = 0
 
        For i As Integer = 0 To maxParts
            intValue += Math.Pow(256, maxParts - i) * System.Convert.ToUInt32(unitIDParts(i), 16)
        Next
 
        Console.WriteLine(intValue)
 
        Console.ReadLine()

Open in new window

0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22782937
If you reverse the storage of hex values so that the last value is in index 0, then you can simplify this to be:

Math.Pow(256, i) * System.Convert.ToUInt32(unitIDParts(i), 16)
0
 

Author Comment

by:RadicalSoftwareSolutions
ID: 22784872
Hi

I just got to try this and it is crashing on the System.Convert.ToUInt32(unitIDParts(i), 16) command with an error System.FormatException - Could not find any recognizable digits
0
 

Author Comment

by:RadicalSoftwareSolutions
ID: 22785161
Just for info the structure value is "C8-CA-00-00-17-03-06-05"

which should vive a unit ID of 084280087
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22785447
The HEX from above that corresponds to unit ID should be 17030605.  The decimal value of this is 386074117.  This is what my code should produce if you put 17 in unitIDParts(0) ... and 05 in unitIDParts(3).

Not sure how to arrive at your answer, but maybe this will help -- shows how to go from String to HEX then you can use the above to go from HEX to decimal.

http://crossedlogic.blogspot.com/2008/09/converting-between-string-ascii-and-hex.html
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22785455
Make sure you are not including the "-" in your string parts as you are grabbing substring as that will not be valid HEX.
0
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 22785485
Ok, I just got your answer.  The HEX values are in reverse, so ensuring that you are putting in the appropriate strings -- try this:
Dim unitIDParts(3) As String
 
        unitIDParts(0) = "17"
        unitIDParts(1) = "03"
        unitIDParts(2) = "06"
        unitIDParts(3) = "05"
 
        Dim maxParts As Integer = unitIDParts.Length - 1
        Dim intValue As Integer = 0
 
        For i As Integer = 0 To maxParts
            intValue += Math.Pow(256, i) * System.Convert.ToUInt32(unitIDParts(i), 16)
        Next
 
        Console.WriteLine(intValue)
 
        Console.ReadLine()

Open in new window

0
 

Author Comment

by:RadicalSoftwareSolutions
ID: 22785500
I was picking my answer up from a serial connector to the unit which transmitted the ID 084280087
0
 

Author Closing Comment

by:RadicalSoftwareSolutions
ID: 31509032
Many thanks for that - I was just begining to wonder if they were reversed - thanks for reply
0
 
LVL 59

Expert Comment

by:Kevin Cross
ID: 22788893
Glad that worked.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
send messages to whatsapp programatically 2 67
Help with converting xml file to excel using VB.NET 3 25
vb.net 1 month apart 11 35
vb.net and creating a class 5 20
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question