コード例 #1
0
        //private WUData _LastReadWUData;
        //public WUData LastReadWattData
        //{
        //    get { return _LastReadWUData; }
        //    set { _LastReadWUData = value; }
        //}
        #endregion

        #region Public Methods
        /// <summary> Connect to the WattsUp and start recording. Returns true on success, else false  </summary>
        public bool Start(out string err)
        {
            err = string.Empty;
            // LastReadWattData = new WUData();

            if (AvailablePorts.Count() > 0)
            {
                //Set the default port to the first available open port in the computer
                if (string.IsNullOrEmpty(this.Port))
                {
                    this.Port = AvailablePorts.FirstOrDefault();
                }

                try
                {
                    //Initialize the serial port
                    this.sp = new SerialPort(this.Port, this.BAUD, System.IO.Ports.Parity.None, this.DATABITS);
                    this.sp.DataReceived += new SerialDataReceivedEventHandler(this.sp_DataReceived);

                    //Open the serial port, set a read timeout on the serial port,
                    //ans then command the WattsUp to return data for the specified internal sampling rate.

                    this.sp.Open();
                    if (this.sp.IsOpen)
                    {
                        this.sp.ReadTimeout = this.READTIMEOUT;
                        if (this.Write(WUCommands.ExternalModeStartString + this.InternalSamplingRate.ToString() + WUCommands.ExternalModeEndString) == "")
                        {
                            return(false);
                        }
                        return(true);
                    }
                    return(false);
                }
                catch (Exception ex)
                {
                    err = ex.Message;
                    return(false);
                }
            }
            else
            {
                err = "No open serial port found!!!";
                return(false);
            }
        }