/// <summary>
        /// Sync receive data.
        /// </summary>
        /// <param name="serialPortobj">Instance of SerialPort.</param>
        private void SyncReceiveData(object serialPortobj)
        {
            SerialPort serialPort = serialPortobj as SerialPort;

            System.Threading.Thread.Sleep(0);
            serialPort.ReadTimeout = this._readTimeout;

            try
            {
                byte firstByte = Convert.ToByte(serialPort.ReadByte());
                int  bytesRead = serialPort.BytesToRead;
                this._receiveBuffer    = new byte[bytesRead + 1];
                this._receiveBuffer[0] = firstByte;

                for (int i = 1; i <= bytesRead; i++)
                {
                    this._receiveBuffer[i] = Convert.ToByte(serialPort.ReadByte());
                }

                this._currentState = SerialStateEnum.OperateOK;
            }
            catch
            {
                this._currentState = SerialStateEnum.ReadTimeout;
            }
            finally
            {
                this._autoResetEvent.Set();
                this.Close();
            }
        }
        /// <summary>
        /// Opens a new serial port connection.
        /// </summary>
        /// <returns>true if succeeded; otherwise, false.</returns>
        public bool Open()
        {
            this.CheckDisposed();

            if (this._serialPort != null)
            {
                try
                {
                    if (!this._serialPort.IsOpen)
                    {
                        this._serialPort.Open();
                    }

                    return(true);
                }
                catch
                {
                    this._currentState = SerialStateEnum.OpenException;
                    return(false);
                }
            }
            else
            {
                this._currentState = SerialStateEnum.SerialPortNull;
                return(false);
            }
        }
        /// <summary>
        /// Initializes a new instance of the SyncSerialPort class using the specified port name, baud rate, parity bit, data bits, and stop bit.
        /// </summary>
        /// <param name="portName">The port to use (for example, COM1).</param>
        /// <param name="baudRate">The baud rate.</param>
        /// <param name="partity">One of the System.IO.Ports.SerialPort.Parity values.</param>
        /// <param name="dataBits">The data bits value.</param>
        /// <param name="stopBits">One of the System.IO.Ports.SerialPort.StopBits values.</param>
        public SyncSerialPort(string portName, int baudRate, Parity partity, int dataBits, StopBits stopBits)
        {
            if (_portNames == null || _portNames.Length == 0)
            {
                this._currentState = SerialStateEnum.NotExistPort;
            }
            else
            {
                bool isExistPort = false;

                for (int i = 0; i < _portNames.Length; i++)
                {
                    if (_portNames[i].Equals(portName))
                    {
                        isExistPort = true;
                        break;
                    }
                }

                if (isExistPort)
                {
                    this._serialPort   = new SerialPort(portName, baudRate, partity, dataBits, stopBits);
                    this._currentState = SerialStateEnum.OperateOK;
                }
                else
                {
                    this._currentState = SerialStateEnum.NotFoundPort;
                }
            }
        }
        /// <summary>
        /// Closes the port connection.
        /// </summary>
        /// <returns>true if succeeded; otherwise, false.</returns>
        public bool Close()
        {
            this.CheckDisposed();

            if (this._serialPort != null)
            {
                if (this._serialPort.IsOpen)
                {
                    this._serialPort.Close();
                }

                return(true);
            }
            else
            {
                this._currentState = SerialStateEnum.SerialPortNull;
                return(false);
            }
        }
        /// <summary>
        /// Sync send a specified number of bytes to the serial port using data from a buffer.
        /// </summary>
        /// <param name="sendData">The byte array that contains the data to write to the port.</param>
        /// <param name="receivedData">The byte array to write the received data.</param>
        /// <param name="timeout">The number of milliseconds before a time-out occurs when a read operation does not finish.</param>
        /// <returns>true if succeeded; otherwise, false.</returns>
        public bool Send(byte[] sendData, out byte[] receivedData, int timeout)
        {
            this.CheckDisposed();

            lock (this._syncRoot)
            {
                this._autoResetEvent.Reset();

                if (sendData == null || sendData.Length == 0)
                {
                    receivedData       = new byte[0];
                    this._currentState = SerialStateEnum.SendDataEmpty;
                    return(false);
                }

                if (this._serialPort == null)
                {
                    receivedData       = new byte[0];
                    this._currentState = SerialStateEnum.SerialPortNull;
                    return(false);
                }

                this._readTimeout = timeout;

                try
                {
                    if (!this.Open())
                    {
                        receivedData = new byte[0];
                        return(false);
                    }

                    this._serialPort.Write(sendData, 0, sendData.Length);
                    Thread threadReceive = new Thread(new ParameterizedThreadStart(this.SyncReceiveData));
                    threadReceive.IsBackground = true;
                    ////threadReceive.Name = "ReadSerialPortData";
                    threadReceive.Start(this._serialPort);
                    this._autoResetEvent.WaitOne();

                    if (threadReceive.IsAlive)
                    {
                        threadReceive.Abort();
                    }

                    if (this._currentState == SerialStateEnum.OperateOK)
                    {
                        receivedData = this._receiveBuffer;
                        return(true);
                    }
                    else
                    {
                        receivedData = new byte[0];
                        return(false);
                    }
                }
                catch
                {
                    receivedData       = new byte[0];
                    this._currentState = SerialStateEnum.SendException;
                    return(false);
                }
            }
        }
Exemple #6
0
 public SerialStateChangeEventArgs(string message, SerialStateEnum state, string portName)
 {
     State    = state;
     Message  = message;
     PortName = portName;
 }