简体   繁体   English

将十六进制CANbus消息转换为GPS long / lat

[英]Converting Hex CANbus message to GPS long/lat

I´m looking for some help converting CAN packets from a PCAN-GPS unit to long/lat GPS coordinates. 我正在寻找将CAN数据包从PCAN-GPS单元转换为长/纬度GPS坐标的帮助。
For example i receive Data=90F98E400A0045 for longitude and the package is in the following format: imgur link 例如,我收到经度的Data = 90F98E400A0045,并且包装采用以下格式: imgur链接

So I get the Degree and Indicator part which match my location from Google Maps but when I try to convert the hex value to a floating point I either get enormous or tiny float values that are not longitude values. 因此,我从Google Maps获得了与我的位置相匹配的度数和指示器部分,但是当我尝试将十六进制值转换为浮点数时,我得到的是不是经度值的巨大或微小的浮动值。

I wrote this code (and some other variations) in Python. 我用Python编写了这段代码(以及其他一些变体)。 My location is somewhere in germany ;) 我的位置在德国的某个地方;)

s = ['E8', '5F', '8F', '40', '0A', '00', '45']
con = []
x = ""



def tohex(hex_string):
    return ''.join('{0:04b}'.format(int(c, 16)) for c in hex_string)


def long(str):
    min = str[:32]
    vorz = min[:1]
    mat = int(min[1:24], 2)
    exp = int(min[24:32], 2)
    l = (2**exp) * float("0."+mat.__str__())
    deg = str[32:48]

    print(int(deg,2))

    return l


for e in s:
    con.append(tohex(e))
    x += tohex(e)

print("%.100f" % long(x))

The more efficient solution is to use the struct.unpack() function. 更为有效的解决方案是使用struct.unpack()函数。

Solution for Python 3.5.x Python 3.5.x的解决方案

1- store the binary data recorded from CANbus in a bytes object. 1-将从CANbus记录的二进制数据存储在字节对象中。

tCanLng1=bytes([0xE8, 0x5F, 0x8F, 0x40, 0x0A, 0x00, 0x45])

2- define the format specifier of the GPS_PositionLongitude structure. 2-定义GPS_PositionLongitude结构的格式说明符。

For GPS_LongitudeMinutes (float) is 'f' (4 bytes). 对于GPS_LongitudeMinutes (浮点数)为'f' (4个字节)。

For GPS_LongitudeDegree (int16) is 'h' (2 bytes). 对于GPS_LongitudeDegree (int16)为'h' (2个字节)。

For GPS_IndicatorEW (char) is 'c' (1 byte). 对于GPS_IndicatorEW (字符)为'c' (1个字节)。

End for the byte alignment (little-endian) add as first specifier '<' 字节对齐的结尾(小尾数)添加为第一个说明符'<'

3- Then perform the unpack of the CANbus binary data as follow: 3-然后按以下步骤执行CANbus二进制数据的解压缩:

>> vCanLng=struct.unpack('<fhc',tCanLng1)
>> vCanLng
(4.480457305908203, 10, b'E')

4- And the result for your position in Germany is: 4-您在德国的职位的结果是:

GPS_LongitudeMinutes = vCanLng[0] = 4.480457305908203
GPS_LongitudeDegree = vCanLng[1] = 10
GPS_IndicatorEW = vCanLng[2] = 'E'

GPS_PositionLongitude = 10° 4.480457305908203 E = (10 + (4.480457305908203/60))° E = 10.0746742884318 ° East . GPS_PositionLongitude = 10°4.480457305908203 E =(10 +(4.480457305908203 / 60))°E = 10.0746742884318°东

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM