Reputation: 4239
We have a Concox TG06 GPS tracker. I am receiving data from it via TCP, but I am struggling to decode the latitude and longitude coordinates (in C#). According to the protocol manual, the message from the tracker (terminal) to the server looks like this:
As you can see, the latitude and longitude coordinates are 4 bytes each. Further down, the manual explains how these coordinates are formatted:
5.2.1.6 Latitude Four bytes are consumes, defining the latitude value of location data. The range of the value is 0 - 162000000, indicating a range of 0°-90°. The conversion method thereof is as follow: Converting the value of latitude and longitude output by GPS module into a decimal based on minute; multiplying the converted decimal by 30000; and converting the multiplied result into hexadecimal. Example: 22º32.7658’=(22X60+32.7658)X30000=40582974, then converted into a hexadecimal number 40582974(Decimal)= 26B3F3E(Hexadecimal) at last the value is 0x02 0x6B 0x3F 0x3E
I am REALLY struggling to work this out backwards - i.e. getting from the four bytes (0x02 0x6B 0x3F 0x3E) to the latitude coordinate (not necessarily in degrees and minutes, as I use decimal degrees in my code). Basically I am trying to create a function whose shell looks like this:
private double DecodeLat(byte[] latBytes)
{
double latitude;
// implementation
return latitude;
{
(And obviously same for longitude). I would greatly appreciate any help at all!
Thank you
Upvotes: 2
Views: 3148
Reputation: 419
Take for instance converting the Hex Latitude 0x027AC7EB
to degrees.
You can do conversion in 4 steps:
Convert the Hex Lat value to Decimal:
0x027AC7EB
=> 41601003
Divide the value you get by 30000
:
41601003 / 30000 => 1,386.7001
Divide by 60
to convert the value to degrees:
1,386.7001 / 60 => 23.11166833333333 degrees
Prefix the minus
or plus
depending on the hemisphere.
How it works?
For Latitude: - North prefix +
- South prefix -
For Longitude: - East prefix +
- West prefix -
You can extract these positioning values from the Course and Status
Bytes
eg. if decoding the course and status byte evaluates to a South
Latitude
, the final Latitude value would be -23.11166833333333 degrees
Same process applies for decoding the Longitude
.
Upvotes: 0
Reputation: 171
For concox protocol the conversion of binary data is pretty straightforward - you just scan 4 binary bytes into number (little endian - e.g. just simple conversion to 32-bit unsigned integer) and then divide this number by 1800000.0. The result is in decimal degrees. Southern and Western hemispheres are defined by the sign, and sign is located in special bit of byte followed by the coordinates.
If you will have troubles with further data parsing from the device take a look at specialized cloud services that provide full protocol parsing and output completed parsed JSON message, like this.
Disclaimer: I'm related to flespi parsing backend and my opinion can be influenced by this product.
Upvotes: 0
Reputation: 3609
The longitude is 4 bytes because I can not see implementation I assume you try to read the double with
BitConverter.ToDouble(Buffer, index);
a double = 8 bytes
if this is not the case Let me know in the comment, please
Upvotes: 1
Reputation: 7880
You can decode it as follows:
public static decimal DecodeLatitude(byte[] bytes)
{
decimal minutes = (bytes[0] << 24) + (bytes[1] << 16) + (bytes[2] << 8) + bytes[3];
minutes /= 30000;
return minutes;
}
See an example here.
Upvotes: 2