Exemple #1
0
        /// <summary>
        /// Demodulator statistics thread method
        /// </summary>
        static void DemodLoop()
        {
            string logsrc = "DEMOD";

            Program.Log(logsrc, "START");

            Socket s = new Socket(SocketType.Stream, ProtocolType.Tcp);

            Program.Log(logsrc, "Socket created");

            try
            {
                // Connect socket
                s.Connect(IP, DemodPort);
                Program.Log(logsrc, string.Format("Connected to {0}:{1}", IP, DemodPort.ToString()));

                // Send nanomsg init message
                s.Send(nninit);

                // Check nanomsg response
                byte[] res = new byte[8];
                s.Receive(res);
                if (res.SequenceEqual(nnires))
                {
                    Program.Log(logsrc, "nanomsg OK");
                }
                else
                {
                    string resHex = BitConverter.ToString(res);
                    Program.Log(logsrc, string.Format("nanomsg error: {0} (Expected: {1})", resHex, BitConverter.ToString(nnires)));
                }
            }
            catch (Exception e)
            {
                Program.Log(logsrc, "Failed to connect");

                // Reset UI and alert user
                Program.MainWindow.ResetUI();
                System.Windows.Forms.MessageBox.Show("Unable to connect to goesrecv", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

                // Stop all threads
                Stop();
                return;
            }

            byte[]  dres = new byte[256];
            int     num;
            JObject json;

            // Averaging
            long           timeAvg = DateTimeOffset.Now.ToUnixTimeMilliseconds();
            List <decimal> freqAvg = new List <decimal>();

            // Continually receive data
            while (true)
            {
                // Receive nanomsg header
                num = s.Receive(dres, 8, SocketFlags.None);

                // Kill thread if no data received
                if (num == 0)
                {
                    Program.Log(logsrc, "Connection lost/no data, killing thread");
                    return;
                }

                // Get message length
                int msglen = dres[7];

                // Receive message content
                num = s.Receive(dres, msglen, SocketFlags.None);

                // Log message bytes
                //Program.Log(logsrc, BitConverter.ToString(dres).Replace("-", ""));

                // Kill thread if no data received
                if (num == 0)
                {
                    Program.Log(logsrc, "Connection lost/no data, killing thread");
                    return;
                }

                // Convert message bytes to ASCII
                string data = Encoding.ASCII.GetString(dres);

                // Trim message length and remove trailing new line
                data = data.Substring(0, msglen);
                data = data.TrimEnd('\n');

                // Parse JSON objects
                try
                {
                    json = JObject.Parse(data);
                    Program.Log(logsrc, string.Format("OK: {0}", data));
                }
                catch (Newtonsoft.Json.JsonReaderException e)
                {
                    Program.Log(logsrc, string.Format("Error parsing JSON: {0}", data));
                    Program.Log(logsrc, e.ToString());
                    continue;
                }

                // kHz vs Hz
                decimal freq = Math.Round((decimal)json["frequency"], 2);

                // Write parsed data to log
                Program.Log(logsrc, string.Format("FREQUENCY: {0}", freq));

                // Add values to average lists
                freqAvg.Add(freq);

                // Calculate average every second
                if (DateTimeOffset.Now.ToUnixTimeMilliseconds() - timeAvg > 1000)
                {
                    decimal avg = freqAvg.Average();
                    string  avgStr;

                    if (avg > 999 || avg < -999)
                    {
                        avg    = avg / 1000;
                        avg    = Math.Round(avg, 2);
                        avgStr = avg.ToString() + " kHz";
                    }
                    else
                    {
                        avg    = Math.Round(avg);
                        avgStr = avg + " Hz";
                    }

                    Program.Log(logsrc, string.Format("AVERAGE FREQUENCY: {0}", avgStr));

                    // Update main UI
                    Program.MainWindow.FrequencyOffset = avgStr;

                    freqAvg.Clear();
                    timeAvg = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Demodulator statistics thread method
        /// </summary>
        static void DemodLoop()
        {
            Console.WriteLine("[DEMOD] Started");

            Socket s = new Socket(SocketType.Stream, ProtocolType.Tcp);

            try
            {
                // Connect socket
                s.Connect(IP, DemodPort);
                Console.WriteLine("[DEMOD] Connected to {0}:{1}", IP, DemodPort.ToString());

                // Send nanomsg init message
                s.Send(nninit);

                // Check nanomsg response
                byte[] res = new byte[8];
                s.Receive(res);
                if (res.SequenceEqual(nnires))
                {
                    Console.WriteLine("[DEMOD] Nanomsg OK");
                }
                else
                {
                    string resHex = BitConverter.ToString(res);
                    Console.WriteLine("[DEMOD] Nanomsg error: {0} (Expected: {1})", resHex, BitConverter.ToString(nnires));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("[DEMOD] Failed to connect");

                // Reset UI and alert user
                Program.MainWindow.ResetUI();
                System.Windows.Forms.MessageBox.Show("Unable to connect to goesrecv", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

                // Stop all threads
                Stop();
                return;
            }

            // Continually receive data
            while (true)
            {
                byte[] dres     = new byte[5120];
                int    numbytes = s.Receive(dres);

                // Kill thread if no data received
                if (numbytes == 0)
                {
                    Console.WriteLine("[DEMOD] No data");

                    // Reset UI and alert user
                    Program.MainWindow.ResetUI();
                    System.Windows.Forms.MessageBox.Show("Lost connection to goesrecv", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);

                    // Stop all threads
                    Stop();
                    return;
                }

                decimal freq;
                try
                {
                    // Convert to string and trim
                    string data = Encoding.ASCII.GetString(dres);
                    data = data.Substring(8, data.IndexOf('\n'));
                    data = data.TrimEnd('\0').TrimEnd('\n');

                    // Parse frequency value
                    string freqStr = data.Substring(data.IndexOf("frequency") + 12);
                    freqStr = freqStr.Substring(0, freqStr.IndexOf(","));
                    freq    = Math.Round(Decimal.Parse(freqStr, System.Globalization.NumberStyles.Float));
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Console.WriteLine("[DEMOD] Substring error");
                    continue;
                }

                // kHz vs Hz
                string freqUIStr;
                if (freq > 999 || freq < -999)
                {
                    freq      = freq / 1000;
                    freqUIStr = freq.ToString() + " kHz";
                }
                else
                {
                    freqUIStr = freq + " Hz";
                }

                // Update UI
                Program.MainWindow.FrequencyOffset = freqUIStr;

                Thread.Sleep(500);
            }
        }