从双重价值转换GPS位置 [英] convert gps position from double value
问题描述
我使用的uniguard UT01 GPS跟踪器与noran协议一起工作
gps_tracker发送给服务器的数据具有如下结构:
我收到的数据是:
52 0 8 0 1 12 0 0 0 0 0 0 1 67 149 240 109 66 194 95 17 66 78 82 48 57 66 49 55 53 55 50 0 0 49 54 45 48 56 45 49 51 32 48 56 58 51 48 58 52 54 0
现在我想从这个
获得经度和经度,当我转换字节15,16时, 17,18到经度,我这样做:
i将149 240 109 66转换为十进制值:1114501269
然后除以30000:37150.0423
然后除以它再次由60:619
和我困惑,因为这不起作用。
有什么错?
期望值必须是:36.3435,59.4850
使用System.Globalization;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Xml;
使用System.Xml.Linq;
命名空间ConsoleApplication7
{
类程序
{
static void Main(string [] args)
{
byte [ ] input = {52,0,8,0,1,12,0,0,0,0,0,1,67,149,240,
109,66,194,95,17, 66,78,82,48,57,66,49,55,
53,55,50,0,0,49,54,45,48,56,45,49,51,32,48, 56,58,51,48,58,52,54,0
};
GPS gps =新GPS(输入);
}
}
公共类GPS
{
UInt16 packetLen {get;组; }
UInt16 cmd {get;组; }
布尔bEnable {get;组; }
字节bAlarm {get;组; }
uint nSpeed {get;组; }
float nDirection {get;组; }
float fLongitude {get;组; }
float fLatitude {get;组; }
byte [] sUserID {get;组; }
string userID =;
byte [] bDateTime {get;组; }
string sDateTime =;
DateTime dateTime {get;组; }
public GPS(byte [] bytes)
{
packetLen = BitConverter.ToUInt16(bytes,0);
cmd = BitConverter.ToUInt16(bytes,2);
bEnable = BitConverter.ToBoolean(bytes,4);
bAlarm = bytes [5];
nSpeed = BitConverter.ToUInt32(bytes,6);
nDirection = BitConverter.ToSingle(bytes.Skip(10).Take(4).ToArray(),0);
fLongitude = BitConverter.ToSingle(bytes.Skip(14).Take(4).ToArray(),0);
fLatitude = BitConverter.ToSingle(bytes.Skip(18).Take(4).ToArray(),0);
sUserID = bytes.Skip(22).Take(12).ToArray();
userID = Encoding.ASCII.GetString(sUserID).Replace(\0,);
bDateTime = bytes.Skip(34).ToArray();
sDateTime = Encoding.ASCII.GetString(bDateTime).Replace(\0,);
dateTime = DateTime.ParseExact(sDateTime,yy-MM-dd HH:mm:ss,CultureInfo.InvariantCulture);
}
}
}
I'm using uniguard UT01 gps car tracker that works with noran protocol the data that gps_tracker send to server has a structure like this: my recieved data is: 52 0 8 0 1 12 0 0 0 0 0 0 1 67 149 240 109 66 194 95 17 66 78 82 48 57 66 49 55 53 55 50 0 0 49 54 45 48 56 45 49 51 32 48 56 58 51 48 58 52 54 0
now i want to get latitude and longitude from this
when i convert bytes 15,16,17,18 to longitude, i do this:
i convert 149 240 109 66 to decimal value: 1114501269
then divide it by 30000 : 37150.0423
then divide it again by 60 : 619
and i confused because this doesn't work.
what's the wrong?
expected value must be : 36.3435, 59.4850
Try following code
using System;
using System.Globalization;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication7
{
class Program
{
static void Main(string[] args)
{
byte[] input = {52, 0, 8, 0, 1, 12, 0, 0, 0, 0, 0, 0, 1, 67, 149, 240,
109, 66, 194, 95, 17, 66, 78, 82, 48, 57, 66, 49, 55,
53, 55, 50, 0, 0, 49, 54, 45, 48, 56, 45, 49, 51, 32, 48, 56, 58, 51, 48, 58, 52, 54, 0
};
GPS gps = new GPS(input);
}
}
public class GPS
{
UInt16 packetLen { get; set; }
UInt16 cmd { get; set; }
Boolean bEnable { get; set; }
byte bAlarm { get; set; }
uint nSpeed { get; set; }
float nDirection { get; set; }
float fLongitude { get; set; }
float fLatitude { get; set; }
byte[] sUserID { get; set; }
string userID = "";
byte[] bDateTime { get; set; }
string sDateTime = "";
DateTime dateTime { get; set; }
public GPS(byte[] bytes)
{
packetLen = BitConverter.ToUInt16(bytes, 0);
cmd = BitConverter.ToUInt16(bytes, 2);
bEnable = BitConverter.ToBoolean(bytes, 4);
bAlarm = bytes[5];
nSpeed = BitConverter.ToUInt32(bytes, 6);
nDirection = BitConverter.ToSingle(bytes.Skip(10).Take(4).ToArray(),0);
fLongitude = BitConverter.ToSingle(bytes.Skip(14).Take(4).ToArray(),0);
fLatitude = BitConverter.ToSingle(bytes.Skip(18).Take(4).ToArray(),0);
sUserID = bytes.Skip(22).Take(12).ToArray();
userID = Encoding.ASCII.GetString(sUserID).Replace("\0", "");
bDateTime = bytes.Skip(34).ToArray();
sDateTime = Encoding.ASCII.GetString(bDateTime).Replace("\0", "");
dateTime = DateTime.ParseExact(sDateTime, "yy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
}
}
}
这篇关于从双重价值转换GPS位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!