我是新的Flutter,我正试图转换lat lng到这种格式
格式长度(字节)起始位2包长度1协议号1信息内容N信息序列号2错误校验2停止位2
我写了这个函数,但它不工作。
String convertCoordinatesToFormat(double latitude, double longitude) {
// Start Bit (2 bytes)
String startBit = (0x78.toString()) ;
// Protocol Number (1 byte)
int protocolNumber = 0x22; // Replace with the appropriate protocol number
// Information Content (N bytes)
// Convert latitude and longitude to the desired format, e.g., as strings
String latitudeString = latitude.toStringAsFixed(6); // Replace with the desired precision for latitude
String longitudeString = longitude.toStringAsFixed(6); // Replace with the desired precision for longitude
String informationContent = "$latitudeString$longitudeString";
// Calculate the length in bytes
int length = 5 + informationContent.length ~/ 2; // Divide by 2 since each byte is represented by 2 characters in hexadecimal
// Length (1 byte)
String lengthString = length.toRadixString(16).padLeft(2, "0");
// Packet Length (1 byte)
// int packetLength = lengthString; // Length of the remaining bytes (Protocol Number + Information Content + Information Serial Number + Error Check + Stop Bit)
// Information Serial Number (2 bytes)
int informationSerialNumber = 1; // Replace with the appropriate serial number
// Error Check (2 bytes)
int errorCheck = calculateErrorCheck(informationContent);
String errorCheckString = errorCheck.toRadixString(16).padLeft(4, "0");
// Stop Bit (2 bytes)
String stopBit = (0x0D.toString()) + (0x0A.toString());
// Concatenate all the fields together
String result = "$startBit$lengthString$protocolNumber$informationContent$informationSerialNumber$errorCheckString$stopBit";
List<int> bytes = utf8.encode(result);
String answer = "";
for (int a in bytes){
answer += a.toString();
}
// Convert the formatted coordinates to uppercase
return answer;
}
int calculateErrorCheck(String content) {
int sum = 0;
for (int i = 0; i < content.length; i++) {
sum += content.codeUnitAt(i);
}
return sum % 65536;
}
1条答案
按热度按时间slwdgvem1#
以下是Traccar GPS服务器(Java)源代码的相关部分:
GT06解码器的完整源代码是here。
请注意,它使用的是Netty库,因此代码片段使用的是Netty
ByteBuf
。