public static void Main()
byte[] posArr = new byte[] {
0x29,0x8A,0x71,0x45,0x59,0xFB,0x1D,0xB0,0x7F,0x59
DecodeT24Payload(posArr);
public static void DecodeT24Payload(byte[] payload)
if (payload.Length != 10 && payload.Length != 11 && payload.Length != 13 && payload.Length != 14 && payload.Length != 18)
throw (new ArgumentException("Incorrect Messsage Length"));
byte[] lTemp32 = new byte[4];
Int32 lLonSign = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 8) == 8 ? 1 : -1;
Int32 lLatSign = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 16) == 16 ? 1 : -1;
var Heading = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 7);
Console.WriteLine("Heading" + Heading);
bool Emergency = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 32) == 32 ? true : false;
Console.WriteLine("Emergency" + Emergency);
bool EmergencyAck = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 64) == 64 ? true : false;
Console.WriteLine("EmergencyAck" + EmergencyAck);
var lat = (Int32)BitConverter.ToInt32(lTemp32, 0) & 8388607;
Byte lTimeBit = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 8388608) == 8388608 ? (byte)1 : (byte)0;
double LatitudeInDegrees = Math.Round(((lat / 60d) * .00100d), 5, MidpointRounding.AwayFromZero) * lLatSign;
Console.WriteLine("LatitudeInDegrees" + LatitudeInDegrees);
var lon = BitConverter.ToInt32(lTemp32, 0);
double LongitudeInDegrees = Math.Round(((lon / 60d) * .00100d), 5, MidpointRounding.AwayFromZero) * lLonSign;
Console.WriteLine("LongitudeInDegrees" + LongitudeInDegrees);
float Speed = KnotsToMetersPerSecond(BitConverter.ToInt32(lTemp32, 0));
Console.WriteLine("Speed " + Speed);
if (payload.Length == 10)
bool GpsPollingResponse = false;
Console.WriteLine("GpsPollingResponse" + GpsPollingResponse);
else if (payload.Length >= 11)
lTemp32[0] = payload[10];
bool GpsPollingResponse = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 1) == 1 ? true : false;
bool BatteryCharging = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 64) == 64 ? true : false;
Console.WriteLine("GpsPollingResponse" + GpsPollingResponse);
Console.WriteLine("BatteryCharging" + BatteryCharging);
int lIgnitionIndication = ((Int32)BitConverter.ToInt32(lTemp32, 0) & 6);
Console.WriteLine("lIgnitionIndication" + lIgnitionIndication);
if (payload.Length >= 13)
lTemp32[1] = payload[11];
lTemp32[0] = payload[12];
int Altitude = BitConverter.ToInt32(lTemp32, 0);
Console.WriteLine("Altitude" + Altitude);
Int32 lSeconds = (Int32)BitConverter.ToInt32(lTemp32, 0);
Console.WriteLine("Itemp" + lSeconds);
DateTime lUtcTime = DateTime.Now.ToUniversalTime();
if (lSeconds > (Int32)(lUtcTime.TimeOfDay.TotalSeconds + 60))
lSeconds = (Int32)(lUtcTime.TimeOfDay.TotalSeconds);
DateTime Timestamp = lUtcTime.Date.AddSeconds(lSeconds);
Console.WriteLine("Timestamp" + Timestamp);
int SecondsToReachServer = (int)DateTime.UtcNow.Subtract(Timestamp).TotalSeconds;
public static long FromBytes(byte[] buffer, int startIndex, int bytesToConvert)
for (int i=0; i < bytesToConvert; i++)
ret = unchecked((ret << 8) | buffer[startIndex+i]);
Console.WriteLine("ret" + ret);
public static long CheckedFromBytes(byte[] value, int startIndex, int bytesToConvert)
return FromBytes(value, startIndex, bytesToConvert);
public static int ToInt32 (byte[] value, int startIndex)
return unchecked((int) (CheckedFromBytes(value, startIndex, 4)));
public static float KnotsToMetersPerSecond(float knots)
return knots * 0.514444f;