예제 #1
0
 /// <summary>
 /// Write Data Packet
 /// </summary>
 /// <param name="data"></param>
 /// <param name="timestamp"></param>
 /// <returns></returns>
 public virtual PortSet <DefaultSubmitResponseType, Fault> WriteData(byte[] data, System.DateTime timestamp)
 {
     stream.StreamData body = new stream.StreamData(data, timestamp);
     stream.WriteData  op   = new stream.WriteData(body);
     this.PostUnknownType(op);
     return(op.ResponsePort);
 }
        public virtual IEnumerator<ITask> WriteDataHandler(stream.WriteData submit)
        {
            if (submit == null || submit.Body == null || submit.Body.Data == null)
            {
                submit.ResponsePort.Post(Fault.FromException(new InvalidOperationException("Invalid data sent to the iRobot.")));
                yield break;
            }

            if (_serialPort == null || !_serialPort.IsOpen)
            {
                submit.ResponsePort.Post(Fault.FromException(new InvalidOperationException("Attempting to send a command before the iRobot Connection has been established.")));
                yield break;
            }

            if (_iRobotConnection.ConnectionType == roomba.iRobotConnectionType.RooTooth
                && !_serialPort.CtsHolding
                && !_serialPort.DsrHolding)
            {
                string errorMessage = "The Bluetooth serial port is paired to a device,\r\n"
                + "    but the Rootooth may not be connected.\r\n";
                LogWarning(LogGroups.Console, errorMessage);
                if (!_state.Initialized)
                {
                    submit.ResponsePort.Post(Fault.FromException(new System.IO.IOException(errorMessage)));
                    yield break;
                }
            }

            try
            {
                _serialPort.Write(submit.Body.Data, 0, submit.Body.Data.Length);
            }
            catch (Exception ex)
            {
                _state.Initialized = false;
                LogError(LogGroups.Console, ex);
                submit.ResponsePort.Post(Fault.FromException(ex));
                yield break;
            }

            submit.ResponsePort.Post(DefaultSubmitResponseType.Instance);
            yield break;
        }