Exemplo n.º 1
0
        bool ISerialPortToStream.Initialize(int BaudRate, int Parity, int DataBits, int StopBits, int FlowValue)
        {
            // From MSDN:  The best practice for any application is to wait for some amount of time after calling the Close
            // method  before attempting to call the Open method, as the port may not be closed instantly.

            for (int retries = 5; retries != 0; retries--)
            {
                try
                {
                    if (_port == null)
                    {
                        _port = new SerialPort(_physicalPortName, BaudRate, (Parity)Parity, DataBits);
                        switch (FlowValue)
                        {
                        case 0x18:
                            _port.Handshake = Handshake.XOnXOff;
                            break;

                        case 0x06:
                            _port.Handshake = Handshake.RequestToSend;
                            break;

                        default:
                            _port.Handshake = Handshake.None;
                            break;
                        }
                    }

                    _portIndex           = int.Parse(_physicalPortName.Substring(3)) - 1;
                    _port.DataReceived  += new SerialDataReceivedEventHandler(_port_DataReceived);
                    _port.ErrorReceived += new SerialErrorReceivedEventHandler(_port_ErrorReceived);
                    _port.Open();
                    Stream = new PhysicalSerialPortStream(_port);

                    return(true);
                }
                catch (Exception)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }

            return(false);
        }
Exemplo n.º 2
0
        bool ISerialPortToStream.Initialize(int BaudRate, int Parity, int DataBits, int StopBits, int FlowValue)
        {
            // From MSDN:  The best practice for any application is to wait for some amount of time after calling the Close 
            // method  before attempting to call the Open method, as the port may not be closed instantly.

            for(int retries = 5; retries != 0; retries--)
            {
                try
                {
                    if(_port == null)
                    {
                        _port = new SerialPort(_physicalPortName, BaudRate, (Parity)Parity, DataBits );
                        switch(FlowValue)
                        {
                            case 0x18:
                                _port.Handshake = Handshake.XOnXOff;
                                break;
                                
                            case 0x06:
                                _port.Handshake = Handshake.RequestToSend;
                                break;
                                
                            default:
                                _port.Handshake = Handshake.None;
                                break;
                        }
                    }

                    _portIndex = int.Parse(_physicalPortName.Substring(3))-1;
                    _port.DataReceived += new SerialDataReceivedEventHandler(_port_DataReceived);
                    _port.ErrorReceived += new SerialErrorReceivedEventHandler(_port_ErrorReceived);
                    _port.Open();
                    Stream = new PhysicalSerialPortStream(_port);

                    return true;
                }
                catch(Exception)
                {
                    System.Threading.Thread.Sleep(1000);
                }
            }

            return false;
        }