Example #1
0
        public virtual void RaiseCommunicationComplete(CommunicationCompleteEventArgs e)
        {
            EventHandler <CommunicationCompleteEventArgs> handler = communicationCompleteHandler;

            if (handler != null)
            {
                handler(this, e);
            }
        }
Example #2
0
 static void onCommunicationComplete(object sender, CommunicationCompleteEventArgs e)
 {
     #region debug
     debugger.WriteLine(Debugger.DebugLevel.DebugLogic, "ArduinoInterface.onCommunicationComplete: reply = {0}", e.reply);
     #endregion
     if (e.reply.StartsWith("tag:"))
     {
         _tag = e.reply.Substring("tag:".Length);
     }
     else if (e.reply.StartsWith("error:"))
     {
         _error = e.reply.Substring("error:".Length);
     }
 }
Example #3
0
        private void SendCommand(string command,
                                 bool waitForReply           = true,
                                 ArduinoStatus interimStatus = ArduinoStatus.Communicating,
                                 int timeoutMillis           = 0)
        {
            if (_serialPort == null)
            {
                _error  = "_serialPort is null";
                _status = ArduinoStatus.BadPort;
                return;
            }
            else if (!_serialPort.IsOpen)
            {
                _error  = string.Format("port \"{0}\" not open", _serialPortName);
                _status = ArduinoStatus.PortNotOpen;
                return;
            }

            char[] crnls = { '\r', '\n' };

            if (timeoutMillis != 0)
            {
                _timeoutMillis     = timeoutMillis;
                communicationTimer = new Timer(communicationTimedOut);
                communicationTimer.Change(_timeoutMillis, Timeout.Infinite);
            }
            else
            {
                _timeoutMillis = 0;
            }

            _command = command;
            _status  = interimStatus;
            _error   = null;
            #region debug
            debugger.WriteLine(Debugger.DebugLevel.DebugLogic, string.Format("arduino status: {0}", _status));
            #endregion
            try
            {
                communicatorTask = Task.Run(() =>
                {
                    string packet = mkPacket(command);
                    string reply  = null;
                    #region debug
                    debugger.WriteLine(Debugger.DebugLevel.DebugLogic, "Arduino: Communicate: Sending \"{0}\" ...", _command);
                    #endregion
                    lock (_serialLock)
                    {
                        _serialPort.Write(packet);
                        if (waitForReply)
                        {
                            Thread.Sleep(1000);
                            reply = getPacket().TrimEnd(crnls);
                            #region debug
                            debugger.WriteLine(Debugger.DebugLevel.DebugLogic, "Arduino: Communicate(\"{0}\") ==> \"{1}\"", _command, reply);
                            #endregion
                        }
                    }
                    CommunicationCompleteEventArgs e = new CommunicationCompleteEventArgs();
                    e.reply = reply;
                    _status = ArduinoStatus.Idle;
                    #region debug
                    debugger.WriteLine(Debugger.DebugLevel.DebugLogic, string.Format("arduino status: {0}", _status));
                    #endregion
                    RaiseCommunicationComplete(e);
                });
            }
            catch (Exception ex)
            {
                communicatorTask.Dispose();
                _status = ArduinoStatus.Idle;
                #region debug
                debugger.WriteLine(Debugger.DebugLevel.DebugLogic, string.Format("arduino status: {0}, communication exception: {1}", _status, ex.Message));
                #endregion
            }
        }