📄 linkpacket.java
字号:
package net.aetherial.gis.garmin;
import java.io.*;
import java.util.*;
public class LinkPacket
{
/**
* This method takes a byte array and converts it into a LinkPacket object.
*/
public LinkPacket (byte[] contents)
{
packetID = contents[1];
packetSize = contents[2];
if (packetSize > 0)
{
ByteArrayOutputStream packetContents = new ByteArrayOutputStream ();
for (int i = 0; i < (int) packetSize; i++)
{
//System.out.println("" + contents.length + "/" + i + "/" +
// ( (int) packetSize));
packetContents.write(contents[i + 3]);
}
packetData = packetContents.toByteArray ();
}
checksum = contents[contents.length - 3];
}
/**
* This constructs a new LinkPacket with a Packet ID of packetType and Packet Data composed of payload.
*/
public LinkPacket (int packetType, byte[] payload)
{
packetID = (byte) packetType;
if (payload != null)
{
packetSize = (byte) payload.length;
packetData = payload;
}
else
{
packetSize = 0;
packetData = null;
}
checksum = computeChecksum ();
}
public byte computeChecksum ()
{
byte[] contents;
if (packetData == null)
contents = new byte[2];
else
contents = new byte[2 + packetData.length];
contents[0] = packetID;
contents[1] = packetSize;
if (packetData != null)
{
for (int i = 2; i < packetData.length + 2; i++)
contents[i] = packetData[i - 2];
}
byte sum = 0;
for (int i = 0; i < contents.length; i++)
sum = (byte) ((byte) sum + (byte) contents[i]);
return (byte) -sum;
}
public boolean goodChecksum ()
{
return true;
/*
if (checksum == 0 || checksum == -1)
return true;
// System.out.println (computeChecksum()+" == "+checksum+"?");
if (computeChecksum () != checksum)
return false;
else
return true;
*/
}
/**
* This method converts a LinkPacket into a byte array that is suitable for sending through serial ports.
*/
public byte[] toByteArray ()
{
int i;
byte[] packetBytes = new byte[packetSize + 6];
packetBytes[0] = dle;
packetBytes[1] = packetID;
packetBytes[2] = packetSize;
if (packetData != null)
{
for (i = 0; i < packetSize; i++)
packetBytes[i + 3] = packetData[i];
}
packetBytes[packetBytes.length - 3] = checksum;
packetBytes[packetBytes.length - 2] = dle;
packetBytes[packetBytes.length - 1] = eot;
return packetBytes;
}
/**
* This method returns the Packet ID of the LinkPacket.
*/
public int getPacketType ()
{
return (int) packetID;
}
/**
* This method returns the LinkPacket's data payload.
*/
public byte[] getPacketData ()
{
return packetData;
}
public byte getChecksum ()
{
return checksum;
}
public void printPacket ()
{
System.out.println ("Packet Data");
System.out.println ("Packet ID: "+packetID);
System.out.println ("Packet Size: "+packetSize);
System.out.println ("Checksum: "+checksum);
System.out.println ("Raw Packet Bytes:");
byte[] packetBytes = this.toByteArray ();
for (int i = 0; i < packetBytes.length; i++)
{
System.out.print ("["+i+"] "+packetBytes[i]+" ");
}
System.out.println ("");
}
private final byte dle = (byte) 16;
private byte packetID;
private byte packetSize;
private byte[] packetData;
private byte checksum;
private final byte eot = (byte) 3;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -