public static void ReadMessage(Stream st1) { bool avr = false; bool binary = false; int avrcount = 0; st1.ReadTimeout = 10000; //st.ReadTimeout = 5000; while (run) { int by = st1.ReadByte(); if (by == -1) break; if (by == '*') { avrcount++; if (avrcount >= 4) avr = true; if (avr) { Plane plane = ReadMessage('*' + ReadLine(st1)); if (plane != null) { PointLatLngAltHdg plla = new PointLatLngAltHdg(plane.plla()); plla.Heading = (float)plane.heading; if (plla.Lat == 0 && plla.Lng == 0) continue; if (UpdatePlanePosition != null && plla != null) UpdatePlanePosition(plla, EventArgs.Empty); //Console.WriteLine(plane.pllalocal(plane.llaeven)); Console.WriteLine(plane.ID + " " + plla); } } } else if ((by == 'M' || by == 'S' || by == 'A' || by == 'I' || by == 'C') && !binary) // msg clk sta air id sel { string line = ((char)by) +ReadLine(st1); if (line.StartsWith("MSG")) { string[] strArray = line.Split(new char[] { ',' }); if (strArray[1] == "3") // airborne pos { String session_id = strArray[2];// String. Database session record number. String aircraft_id = strArray[3];// String. Database aircraft record number. String hex_ident = strArray[4];//String. 24-bit ICACO ID, in hex. String flight_id = strArray[5];//String. Database flight record number. String generated_date = strArray[6];// String. Date the message was generated. String generated_time = strArray[7];//String. Time the message was generated. String logged_date = strArray[8];//String. Date the message was logged. String logged_time = strArray[9];//String. Time the message was logged. String callsign = strArray[10];//String. Eight character flight ID or callsign. int altitude = 0; try { altitude = (int)double.Parse(strArray[11], CultureInfo.InvariantCulture);// Integer. Mode C Altitude relative to 1013 mb (29.92" Hg). } catch { } double lat = 0; try { lat = double.Parse(strArray[14], CultureInfo.InvariantCulture);//Float. Latitude. } catch { } double lon = 0; try { lon = double.Parse(strArray[15], CultureInfo.InvariantCulture);//Float. Longitude } catch { } bool is_on_ground = strArray[21] != "0";//Boolean. Flag to indicate ground squat switch is active. if (Planes[hex_ident] == null) Planes[hex_ident] = new Plane(); Plane plane = ((Plane)Planes[hex_ident]); if (lat == 0 && lon == 0) continue; if (UpdatePlanePosition != null && plane != null) UpdatePlanePosition(new PointLatLngAltHdg(lat, lon, altitude / 3.048, (float)plane.heading, hex_ident), EventArgs.Empty); } else if (strArray[1] == "4") { String session_id = strArray[2];// String. Database session record number. String aircraft_id = strArray[3];// String. Database aircraft record number. String hex_ident = strArray[4];//String. 24-bit ICACO ID, in hex. String flight_id = strArray[5];//String. Database flight record number. String generated_date = strArray[6];// String. Date the message was generated. String generated_time = strArray[7];//String. Time the message was generated. String logged_date = strArray[8];//String. Date the message was logged. String logged_time = strArray[9];//String. Time the message was logged. if (Planes[hex_ident] == null) Planes[hex_ident] = new Plane(); try { int ground_speed = (int)double.Parse(strArray[12], CultureInfo.InvariantCulture);// Integer. Speed over ground. } catch { } try { ((Plane)Planes[hex_ident]).heading = (int)double.Parse(strArray[13], CultureInfo.InvariantCulture);//Integer. Ground track angle. } catch { } } else if (strArray[1] == "1") { String session_id = strArray[2];// String. Database session record number. String aircraft_id = strArray[3];// String. Database aircraft record number. String hex_ident = strArray[4];//String. 24-bit ICACO ID, in hex. String flight_id = strArray[5];//String. Database flight record number. String generated_date = strArray[6];// String. Date the message was generated. String generated_time = strArray[7];//String. Time the message was generated. String logged_date = strArray[8];//String. Date the message was logged. String logged_time = strArray[9];//String. Time the message was logged. String callsign = strArray[10];//String. Eight character flight ID or callsign. if (Planes[hex_ident] == null) Planes[hex_ident] = new Plane(); ((Plane)Planes[hex_ident]).CallSign = callsign; } } else { log.Info(line); } } else if (by == 0x1a) { avr = false; byte[] buffer = new byte[24]; buffer[0] = (byte)by; int type = st1.ReadByte(); buffer[1] = (byte)type; st1.Read(buffer, 2, 7); switch (type) { case '1': // mode-ac // 2 bytes st1.Read(buffer, 9, 2); //log.Info("1"); break; case '2': // mode-s short st1.Read(buffer, 9, 7); //log.Info("2"); break; case '3': // mode-s long st1.Read(buffer, 9, 14); //log.Info("3"); Plane plane = ReadMessage(buffer); if (plane != null) { binary = true; PointLatLngAltHdg plla = new PointLatLngAltHdg(plane.plla()); if (plla == null) break; if (plla.Lat == 0 && plla.Lng == 0) continue; plla.Heading = (float)plane.heading; if (UpdatePlanePosition != null && plla != null) UpdatePlanePosition(plla, EventArgs.Empty); //Console.WriteLine(plane.pllalocal(plane.llaeven)); Console.WriteLine(plla); } break; default: break; } } else { log.Info("bad sync 0x" + by.ToString("X2") + " " + (char)by); } } }
public static void ReadMessage(Stream st1) { bool avr = false; bool binary = false; int avrcount = 0; st1.ReadTimeout = 10000; //st.ReadTimeout = 5000; while (true) { int by = st1.ReadByte(); if (by == -1) { break; } if (by == '*') { avrcount++; if (avrcount >= 4) { avr = true; } if (avr) { Plane plane = ReadMessage('*' + ReadLine(st1)); if (plane != null) { PointLatLngAltHdg plla = new PointLatLngAltHdg(plane.plla()); plla.Heading = (float)plane.heading; if (plla.Lat == 0 && plla.Lng == 0) { continue; } if (UpdatePlanePosition != null && plla != null) { UpdatePlanePosition(plla, new EventArgs()); } //Console.WriteLine(plane.pllalocal(plane.llaeven)); Console.WriteLine(plane.ID + " " + plla); } } } else if ((by == 'M' || by == 'S' || by == 'A' || by == 'I' || by == 'C') && !binary) // msg clk sta air id sel { string line = ((char)by) + ReadLine(st1); if (line.StartsWith("MSG")) { string[] strArray = line.Split(new char[] { ',' }); if (strArray[1] == "3") // airborne pos { String session_id = strArray[2]; // String. Database session record number. String aircraft_id = strArray[3]; // String. Database aircraft record number. String hex_ident = strArray[4]; //String. 24-bit ICACO ID, in hex. String flight_id = strArray[5]; //String. Database flight record number. String generated_date = strArray[6]; // String. Date the message was generated. String generated_time = strArray[7]; //String. Time the message was generated. String logged_date = strArray[8]; //String. Date the message was logged. String logged_time = strArray[9]; //String. Time the message was logged. String callsign = strArray[10]; //String. Eight character flight ID or callsign. int altitude = 0; try { altitude = (int)double.Parse(strArray[11], CultureInfo.InvariantCulture);// Integer. Mode C Altitude relative to 1013 mb (29.92" Hg). } catch { } double lat = 0; try { lat = double.Parse(strArray[14], CultureInfo.InvariantCulture);//Float. Latitude. } catch { } double lon = 0; try { lon = double.Parse(strArray[15], CultureInfo.InvariantCulture);//Float. Longitude } catch { } bool is_on_ground = strArray[21] != "0";//Boolean. Flag to indicate ground squat switch is active. if (Planes[hex_ident] == null) { Planes[hex_ident] = new Plane(); } Plane plane = ((Plane)Planes[hex_ident]); if (lat == 0 && lon == 0) { continue; } if (UpdatePlanePosition != null && plane != null) { UpdatePlanePosition(new PointLatLngAltHdg(lat, lon, altitude / 3.048, (float)plane.heading, hex_ident), new EventArgs()); } } else if (strArray[1] == "4") { String session_id = strArray[2]; // String. Database session record number. String aircraft_id = strArray[3]; // String. Database aircraft record number. String hex_ident = strArray[4]; //String. 24-bit ICACO ID, in hex. String flight_id = strArray[5]; //String. Database flight record number. String generated_date = strArray[6]; // String. Date the message was generated. String generated_time = strArray[7]; //String. Time the message was generated. String logged_date = strArray[8]; //String. Date the message was logged. String logged_time = strArray[9]; //String. Time the message was logged. if (Planes[hex_ident] == null) { Planes[hex_ident] = new Plane(); } try { int ground_speed = (int)double.Parse(strArray[12], CultureInfo.InvariantCulture);// Integer. Speed over ground. } catch { } try { ((Plane)Planes[hex_ident]).heading = (int)double.Parse(strArray[13], CultureInfo.InvariantCulture);//Integer. Ground track angle. } catch { } } else if (strArray[1] == "1") { String session_id = strArray[2]; // String. Database session record number. String aircraft_id = strArray[3]; // String. Database aircraft record number. String hex_ident = strArray[4]; //String. 24-bit ICACO ID, in hex. String flight_id = strArray[5]; //String. Database flight record number. String generated_date = strArray[6]; // String. Date the message was generated. String generated_time = strArray[7]; //String. Time the message was generated. String logged_date = strArray[8]; //String. Date the message was logged. String logged_time = strArray[9]; //String. Time the message was logged. String callsign = strArray[10]; //String. Eight character flight ID or callsign. if (Planes[hex_ident] == null) { Planes[hex_ident] = new Plane(); } ((Plane)Planes[hex_ident]).CallSign = callsign; } } else { log.Info(line); } } else if (by == 0x1a) { avr = false; byte[] buffer = new byte[24]; buffer[0] = (byte)by; int type = st1.ReadByte(); buffer[1] = (byte)type; st1.Read(buffer, 2, 7); switch (type) { case '1': // mode-ac // 2 bytes st1.Read(buffer, 9, 2); //log.Info("1"); break; case '2': // mode-s short st1.Read(buffer, 9, 7); //log.Info("2"); break; case '3': // mode-s long st1.Read(buffer, 9, 14); //log.Info("3"); Plane plane = ReadMessage(buffer); if (plane != null) { binary = true; PointLatLngAltHdg plla = new PointLatLngAltHdg(plane.plla()); if (plla == null) { break; } if (plla.Lat == 0 && plla.Lng == 0) { continue; } plla.Heading = (float)plane.heading; if (UpdatePlanePosition != null && plla != null) { UpdatePlanePosition(plla, new EventArgs()); } //Console.WriteLine(plane.pllalocal(plane.llaeven)); Console.WriteLine(plla); } break; default: break; } } else { log.Info("bad sync 0x" + by.ToString("X2") + " " + (char)by); } } }