关于GPS的NMEA0831协议的解析
来源:互联网 发布:2015 网络事件 编辑:程序博客网 时间:2024/05/17 04:08
一、NMEA-0183概述
int startIndex = newstr
.indexOf(ConstVal.HEX_YHZL_GPSQ_INDEX);
int endIndex = startIndex + 14;
// 截取标志字符串“24 47 50 47 47 41”以后的字符串
String strnew = newstr.substring(startIndex + 3);
// 1、得到所有的十六进制
String[] messageArray = strnew.split(" ");
StringBuilder sb = new StringBuilder();
int a;
int flagChar = 0x00;
int j = 0;
String f = "";
String[] b = new String[messageArray.length];
for (int i = 0; i < messageArray.length; i++) {
a = Integer.parseInt(messageArray[i], 16);
sb.append(((char) a));
// 当遇到“2a”的时候,就
if (!"2a".equalsIgnoreCase(messageArray[i])
&& (flagChar == 0x00 || flagChar == 0x01 || flagChar == 0x02)) {
if (flagChar == 0x02) {
f += ((char) a + "");
b[j++] = f;
b = Arrays.copyOf(b, j);
break;
}
if (flagChar == 0x01) {
f += ((char) a + "");
flagChar = 0x02;
}
if (flagChar == 0x00) {
b[j++] = messageArray[i];
}
}
if ("2a".equalsIgnoreCase(messageArray[i])) {
flagChar = 0x01;
}
}
// $GPGGA,100252.000,4000.0137,N,11622.6537,E,1,8,1.50,84.0,M,-6.3,M,,*70
// $GPGGA,111723.000,3959.9903,N,11622.6575,E,2,9,0.98,90.9,M,-6.3,M,0000,0000*7A\r\n
// 开始校验
String c = sb.toString();
// 1、如果没有*,校验失败
int index1 = c.indexOf("*");
if (index1 == -1) {
return;
}
// 2、没有校验和的位数,校验失败
try {
c.substring(0, index1 + 3);
} catch (IndexOutOfBoundsException e1) {
return;
}
// 3、进行校验和校验
if (!BDUtils.checkSum(b)) {
send_OTHER_CMD(ConstVal.HEX_YHZL_GPSQ);
return;
}
// 如果有,就截取之
String g = c.substring(0, index1 + 1);
String[] strArray = g.split(",");
if ("0".equals(strArray[6]) || "3".equals(strArray[6])) {
// 当得到的信息无效的时候,重新发送GPS定位信息
send_OTHER_CMD(ConstVal.HEX_YHZL_GPSQ);
return;
}
if ("N".equals(strArray[3])) {
// 如果是北纬的话,在解析的时候,不用加负号
double temp0 = Double.parseDouble(strArray[2]) / 100;
String[] temp1 = (temp0 + "").split("\\.");
double temp2 = Double.parseDouble("0." + temp1[1]) * 100;
DecimalFormat format = new DecimalFormat();
format.applyPattern("0.0000");
String temp3 = format.format(temp2);
double temp4 = Double.parseDouble(temp3) / 60;
latitude = Double.parseDouble(temp1[0]) + temp4;
} else if ("S".equals(strArray[3])) {
// 如果是南纬的话,在解析纬度的时候,要加上负号
double temp0 = Double.parseDouble(strArray[2]) / 100;
String[] temp1 = (temp0 + "").split("\\.");
double temp2 = Double.parseDouble("0." + temp1[1]) * 100 / 60;
latitude = (-1)
* (Double.parseDouble(temp1[0]) + temp2);
}
if ("E".equals(strArray[5])) {
// 如果是东经的话,就不用加负号
double temp0 = Double.parseDouble(strArray[4]) / 100;
String[] temp1 = (temp0 + "").split("\\.");
double temp2 = Double.parseDouble("0." + temp1[1]) * 100;
DecimalFormat format = new DecimalFormat();
format.applyPattern("0.0000");
String temp3 = format.format(temp2);
double temp4 = Double.parseDouble(temp3) / 60;
longitude = Double.parseDouble(temp1[0]) + temp4;
} else if ("W".equals(strArray[5])) {
// 如果是西经的话,就需要加上负号
double temp0 = Double.parseDouble(strArray[4]) / 100;
String[] temp1 = (temp0 + "").split("\\.");
double temp2 = Double.parseDouble("0." + temp1[1]) * 100 / 60;
longitude = (-1)
* (Double.parseDouble(temp1[0]) + temp2);
// longitude = (-1)
// * (Double.parseDouble(strArray[4]) / 100);
}
public static boolean checkSum(String[] strArray) {
boolean result = false;
String content = "";
String CRC = strArray[(strArray.length - 1)];
// 校验和数据
for (int i = 0; i < strArray.length - 1; i++) {
content += strArray[i];
}
String strSum = doCheckSum(content);
if ((strSum.toUpperCase()).equals(CRC.toUpperCase()))
result = true;
return result;
}
/**
* 计算校验和
*/
public static String doCheckSum(String msg) {
// String msg = "2458545A4A000D0679DC0005";
byte cmdBytes[] = null;
int i;
byte bt;
try {
cmdBytes = hexToByte(msg);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for (bt = cmdBytes[0], i = 1; i < cmdBytes.length; i++) {
bt ^= cmdBytes[i];
}
String hex = Integer.toHexString(bt & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
return hex;
}
public static byte[] hexToByte(String s) throws Exception {
if ("0x".equals(s.substring(0, 2))) {
s = s.substring(2);
}
Log.d("BDUtils==hexToByte==", s);
byte[] baKeyword = new byte[s.length() / 2];
for (int i = 0; i < baKeyword.length; i++) {
try {
baKeyword[i] = (byte) (0xff & Integer.parseInt(
s.substring(i * 2, i * 2 + 2), 16));
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
return baKeyword;
}
- 关于GPS的NMEA0831协议的解析
- GPS协议的解析---C#
- GPS的NMEA0183协议
- GPS卫星定位接收器的NMEA协议解析
- gps模块协议NMEA-0183的解析----android4.2下的gps hal层
- GPS NMEA0183协议解析
- GPS NMEA0183协议解析
- GPS协议解析
- GPS NMEA0183协议解析
- GPS通讯协议解析
- android hal 层GPS 研究总结——关于GPS GGA/GSV/RMC 数据的解析
- GPS的NMEA数据解析
- GPS的NMEA数据解析
- GPS的NMEA数据解析
- gps各个模块的解析
- 转:关于对SSDP协议的解析
- 关于GPS偏移的基础知识
- 关于GPS偏移的基础知识
- Allegro中Thermal relief Pad 和Anti Pad
- Torvalds说他没有任何Linux的长远规划
- PackageInfo、ResolveInfo
- AD10应用心得
- Android 的网络编程(17)-android显示网络图片
- 关于GPS的NMEA0831协议的解析
- javascript中createElement函数的使用
- UVA 457 - Linear Cellular Automata
- 端午三天有感。
- javascript中body的参数
- 未找到类型或命名空间名称
- CString 操作指南
- Android 的网络编程(18)-监控四种网络状态
- mac下virtualbox安装ubuntu12.04出现问题