private RTLMessage ReceiveAVRMsg(StreamReader sr) { RTLMessage msg = new RTLMessage(); // read AVR format input msg.RawMessage = sr.ReadLine(); if (msg.RawMessage.StartsWith("*")) { // standard AVR message // no timestamp in telegram --> set timestamp after reading msg.TimeStamp = DateTime.UtcNow; msg.SignalStrength = 0; } else if (msg.RawMessage.StartsWith("@")) { // extended AVR message wit MLAT // convert into standard message format // extract time string string time = msg.RawMessage.Substring(1, 12); // TODO: interprete the MLAT timestamp! msg.TimeStamp = DateTime.UtcNow; msg.RawMessage = "*" + msg.RawMessage.Remove(0, 13); } return(msg); }
private RTLMessage ReceiveBinaryMsg(Stream stream) { // read Mode-S beast binary input string RTL = null; int signal_strength = 0; long nanosec = 0; long daysec = 0; DateTime timestamp = DateTime.UtcNow; byte[] buffer = new byte[23]; // wait for escape character DateTime start = DateTime.UtcNow; DateTime stop = DateTime.Now; do { stream.Read(buffer, 0, 1); // System.Console.WriteLine(BitConverter.ToString(buffer,0,1)); if (buffer[0] == 0x1A) { // read next character stream.Read(buffer, 1, 1); switch (buffer[1]) { case 0x31: // do not decode RTL = null; break; case 0x32: // 7 byte short frame // read timestamp stream.Read(buffer, 2, 6); nanosec = ((buffer[4] & 0x3f) << 24) | (buffer[5] << 16) | (buffer[6] << 8) | (buffer[7]); daysec = (buffer[2] << 10) | (buffer[3] << 2) | (buffer[4] >> 6); timestamp = DateTime.Today.AddSeconds(daysec); timestamp = timestamp.AddMilliseconds(nanosec / 1000); // plausibility check if (Math.Abs((DateTime.Now - timestamp).Seconds) > 10) { // time difference > 10sec --> discard timestamp timestamp = DateTime.UtcNow; } // read signal strength stream.Read(buffer, 8, 1); // plausibility check if (Math.Abs((DateTime.Now - timestamp).Seconds) > 10) { // time difference > 10sec --> discard timestamp timestamp = DateTime.UtcNow; } signal_strength = buffer[8]; // read frame stream.Read(buffer, 9, 7); // convert to AVR string RTL = BitConverter.ToString((byte[])buffer, 9, 7).Replace("-", String.Empty); RTL = "*" + RTL + ";"; break; case 0x33: // 14 byte long frame // read timestamp stream.Read(buffer, 2, 6); nanosec = ((buffer[4] & 0x3f) << 24) | (buffer[5] << 16) | (buffer[6] << 8) | (buffer[7]); daysec = (buffer[2] << 10) | (buffer[3] << 2) | (buffer[4] >> 6); timestamp = DateTime.Today.AddSeconds(daysec); timestamp = timestamp.AddMilliseconds(nanosec / 1000); // plausibility check if (Math.Abs((DateTime.Now - timestamp).Seconds) > 10) { // time difference > 10sec --> discard timestamp timestamp = DateTime.UtcNow; } // read signal strength stream.Read(buffer, 8, 1); signal_strength = buffer[8]; // read frame stream.Read(buffer, 9, 14); // convert to AVR string RTL = BitConverter.ToString((byte[])buffer, 9, 14).Replace("-", String.Empty); RTL = "*" + RTL + ";"; break; default: // false decode RTL = null; break; } } // check for timeout 10sec stop = DateTime.UtcNow; if (stop - start > new TimeSpan(0, 0, 10)) { throw new TimeoutException(); } } // while ((RTL == null) && !this.CancellationPending); while (RTL == null); if (RTL == null) { return(null); } RTLMessage msg = new RTLMessage(); msg.RawMessage = RTL; msg.TimeStamp = timestamp; msg.SignalStrength = signal_strength; return(msg); }
private void bw_Receiver_DoWork(object sender, DoWorkEventArgs e) { Thread.CurrentThread.Priority = ThreadPriority.Highest; StreamReader sr = null; TcpClient client = null; RTLMessage msg = null; // outer loop do { try { // setup TCP listener client = new TcpClient(); string server = Settings.Server; client.Connect(server, Settings.Port); sr = new StreamReader(client.GetStream()); // inner loop // receive messages in a loop do { if (Settings.Binary) { msg = ReceiveBinaryMsg(sr.BaseStream); } else { msg = ReceiveAVRMsg(sr); } // decode the message if (msg.RawMessage.StartsWith("*") && msg.RawMessage.EndsWith(";")) { // try to decode the message string info = ""; try { Console.Write("[" + this.GetType().Name + "]: " + msg.RawMessage + "-- > "); info = Decoder.DecodeMessage(msg.RawMessage, msg.TimeStamp); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(info); } }while ((msg != null) && !bw_Receciver.CancellationPending); } catch (Exception ex) { // report error Console.WriteLine("Error reading from TCP connection: " + ex.Message); // wait 10 sec int i = 0; while ((i < 10) && !bw_Receciver.CancellationPending) { Thread.Sleep(1000); i++; } } finally { // try to close the stream and TCP client try { if (sr != null) { sr.Close(); } } catch { } try { if (client != null) { client.Close(); } } catch { } } }while (!bw_Receciver.CancellationPending); }