Ejemplo n.º 1
0
 public static void InitializeSerialConnections(ProsthesisCore.Utility.Logger logger)
 {
     string[] ports = GetPortNames();
     logger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Found {0} ports", ports.Length));
     foreach (string port in ports)
     {
         logger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Port {0} found", port));
     }
 }
Ejemplo n.º 2
0
        private static void OnTelemetryPublishAlarm(object sender, ElapsedEventArgs arg)
        {
            using (var udpClient = new UdpClient(AddressFamily.InterNetwork))
            {
                ProsthesisCore.Telemetry.ProsthesisTelemetry telem = mContext.MachineState;
                ProsthesisDataPacket packet = ProsthesisCore.Messages.ProsthesisDataPacket.BoxMessage <ProsthesisCore.Telemetry.ProsthesisTelemetry>(telem);

                if (packet != null)
                {
                    IPAddress  address    = IPAddress.Parse(ProsthesisCore.ProsthesisConstants.kMulticastGroupAddress);
                    IPEndPoint ipEndPoint = new IPEndPoint(address, ProsthesisCore.ProsthesisConstants.kTelemetryPort);
                    udpClient.JoinMulticastGroup(address, 50);
                    udpClient.Send(packet.Bytes, packet.Bytes.Length, ipEndPoint);
                    udpClient.Close();
                }
                else
                {
                    mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Telemetry, "Failed to box telemetry message");
                }
            }
        }
Ejemplo n.º 3
0
        public static void Main(string[] args)
        {
            mTelemetryBroadcastTimer           = new Timer(kTelemetryBroadcastPeriod);
            mTelemetryBroadcastTimer.AutoReset = true;
            mTelemetryBroadcastTimer.Elapsed  += OnTelemetryPublishAlarm;

            string fileName = string.Format("Server-{0}.txt", System.DateTime.Now.ToString("dd MM yyyy HH-mm-ss"));

            mLogger = new ProsthesisCore.Utility.Logger(fileName, true);
            mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.General, "ProsthesisOS startup", true);

            mContext = new States.ProsthesisMainContext(ProsthesisCore.ProsthesisConstants.ConnectionPort, mLogger);

            mTelemetryBroadcastTimer.Start();

            //Safely shut down app
            AppDomain.CurrentDomain.ProcessExit += delegate(object sender, EventArgs e)
            {
                if (mContext.IsRunning)
                {
                    mContext.Terminate("Aborted");
                }

                mLogger.ShutDown();
            };

            Console.WriteLine("Press 'x' to exit");

            while (Console.ReadKey().Key != ConsoleKey.X)
            {
            }

            if (mContext.IsRunning)
            {
                mContext.Terminate("User terminated");
            }

            mTelemetryBroadcastTimer.Stop();
            mLogger.ShutDown();
        }
Ejemplo n.º 4
0
        public static void Main(string[] args)
        {
            mTelemetryBroadcastTimer = new Timer(kTelemetryBroadcastPeriod);
            mTelemetryBroadcastTimer.AutoReset = true;
            mTelemetryBroadcastTimer.Elapsed += OnTelemetryPublishAlarm;

            string fileName = string.Format("Server-{0}.txt", System.DateTime.Now.ToString("dd MM yyyy HH-mm-ss"));
            mLogger = new ProsthesisCore.Utility.Logger(fileName, true);
            mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.General, "ProsthesisOS startup", true);

            mContext = new States.ProsthesisMainContext(ProsthesisCore.ProsthesisConstants.ConnectionPort, mLogger);

            mTelemetryBroadcastTimer.Start();

            //Safely shut down app
            AppDomain.CurrentDomain.ProcessExit += delegate(object sender, EventArgs e)
            {
                if (mContext.IsRunning)
                {
                    mContext.Terminate("Aborted");
                }

                mLogger.ShutDown();
            };

            Console.WriteLine("Press 'x' to exit");

            while (Console.ReadKey().Key != ConsoleKey.X) { }

            if (mContext.IsRunning)
            {
                mContext.Terminate("User terminated");
            }

            mTelemetryBroadcastTimer.Stop();
            mLogger.ShutDown();
        }
Ejemplo n.º 5
0
        public bool StartArduinoComms()
        {
            if (mWorkerThread != null)
            {
                mWorkerThread.Abort();
                mWorkerThread = null;
            }

            string[] ports = GetPortNames();
            bool     foundCorrectArduino = false;

            var idPacket = new ArduinoMessageBase();

            idPacket.ID = ArduinoMessageValues.kIdentifyValue;

            string jsonOutput = Newtonsoft.Json.JsonConvert.SerializeObject(idPacket);

            foreach (string port in ports)
            {
                SerialPort serialPort = new SerialPort(port, kArduinoCommsBaudRate);

                if (mLogger != null)
                {
                    mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Now checking Arduino on port {0}", port));
                }
                //Only check unopened ports
                if (!serialPort.IsOpen)
                {
                    try
                    {
                        serialPort.Open();
                    }
                    //Port is already open in another context, or Arduino. In any case, skip this one
                    catch (UnauthorizedAccessException)
                    {
                        if (mLogger != null)
                        {
                            mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Unable to open port {0} because of unauthorized access exception", port));
                        }
                        continue;
                    }

                    //Wait for the Arduino to boot once we've opened the port
                    System.Threading.Thread.Sleep(kArduinoBootloaderDelayMilliseconds);

                    //Disable telemtry just incase
                    var    toggle       = new { ID = ArduinoMessageValues.kTelemetryEnableValue, EN = false };
                    string disableTelem = Newtonsoft.Json.JsonConvert.SerializeObject(toggle);
                    serialPort.Write(disableTelem);

                    var    toggleHB  = new { ID = ArduinoMessageValues.kHeartbeatEnableValue, EN = false };
                    string disableHB = Newtonsoft.Json.JsonConvert.SerializeObject(toggleHB);
                    serialPort.Write(disableHB);

                    //Discard any built up data
                    serialPort.DiscardInBuffer();
                    serialPort.Write(jsonOutput);
                    serialPort.ReadTimeout = kIDTimeoutMilliseconds;

                    string response = string.Empty;
                    for (int i = 0; i < kNumRetries; ++i)
                    {
                        try
                        {
                            response = ReadLine(serialPort);
                            break;
                        }
                        //Catch case where the serial port is unavailable. MOve to next port
                        catch (TimeoutException)
                        {
                            if (mLogger != null)
                            {
                                mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Port {0} timed out. Attempt {1} of {2}", port, i + 1, kNumRetries));
                            }
                            continue;
                        }
                    }

                    if (!string.IsNullOrEmpty(response))
                    {
                        try
                        {
                            ArduinoIDAckMessage msg = Newtonsoft.Json.JsonConvert.DeserializeObject <ArduinoIDAckMessage>(response);
                            if (msg.ID == ArduinoMessageValues.kAcknowledgeID && msg.AID == mArduinoID)
                            {
                                mTelemetryToggled = msg.TS;
                                mDeviceState      = msg.DS;

                                if (mLogger != null)
                                {
                                    mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Got the arduino we're looking for on port {0} with AID {1}. Telemetry is {2} and the device's state is {3}", port, mArduinoID, msg.TS, msg.DS));
                                }
                                mPort = serialPort;

                                //Don't timeout anymore. Our worker thread will yield while it waits for data
                                mPort.ReadTimeout  = -1;
                                mWorkerThread      = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(ReadSerialDataFromPort));
                                mWorkerThread.Name = string.Format("Arduino IO worker for AID {0}", mArduinoID);

                                mPort.Disposed     += new EventHandler(OnPortDisposed);
                                mPortName           = port;
                                foundCorrectArduino = true;

                                //Start our worker
                                mWorkerThread.Start();
                            }
                            else if (mLogger != null)
                            {
                                mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Found a Prosthesis Arduino, but not the arduino we're looking for on port {0} with AID {1}", port, mArduinoID));
                            }
                        }
                        //Catch malformed JSON response, if there is one at all
                        catch (Newtonsoft.Json.JsonSerializationException)
                        {
                            if (mLogger != null)
                            {
                                mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Malformed response. Ignoring port {0}", port));
                            }
                        }
                        catch (Newtonsoft.Json.JsonReaderException)
                        {
                            if (mLogger != null)
                            {
                                mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Malformed response. Ignoring port {0}", port));
                            }
                        }
                    }
                    else if (mLogger != null)
                    {
                        mLogger.LogMessage(ProsthesisCore.Utility.Logger.LoggerChannels.Arduino, string.Format("Serial port {0} doesn't have an arduino", port));
                    }

                    if (!foundCorrectArduino)
                    {
                        serialPort.Close();
                        serialPort.Dispose();
                    }
                    else
                    {
                        //We've found our Arduino, no need to continue checking
                        break;
                    }
                }
            }

            return(foundCorrectArduino);
        }