示例#1
0
 public void Dispose()
 {
     if (!_stream.IsDisposed)
     {
         _stream.Dispose();
     }
 }
        public void DtrDisableBeforeOpen()
        {
            SerialPortStream serial = null;

            try {
                serial = new SerialPortStream(SourcePort)
                {
                    BaudRate     = 115200,
                    DataBits     = 8,
                    Parity       = Parity.None,
                    DtrEnable    = false,
                    StopBits     = StopBits.One,
                    ReadTimeout  = -1,
                    WriteTimeout = -1
                };

                serial.Open();
                Assert.That(serial.DtrEnable, Is.False);
            } finally {
                if (serial != null)
                {
                    serial.Dispose();
                }
            }
        }
示例#3
0
        internal static actionResult DestroyPort(SerialPortStream port)
        {
            var ar = new actionResult();

            if (!port.IsAlive())
            {
                ar.setError("Port already closed.");
                return(ar);
            }

            if (port == Plugin.SelectedPort)
            {
                Plugin.MainCtl.Log($"=========== {port.PortName}: log end ===========", false);
            }
            string portLongName = Plugin.OpenedPorts.First(kvp => kvp.Value == port).Key;

            Plugin.OpenedPorts.Remove(portLongName);
            if (port.IsOpen)
            {
                port.Close();
            }
            port.Dispose();
            // ReSharper disable once RedundantAssignment
            port = null;
            Plugin.MainCtl.UpdatePortsListView();
            ar.setInfo("Port closed.");
            Plugin.HostInstance.log(nameof(Serial) + ": destroyed port: " + portLongName);
            return(ar);
        }
        public void SimpleConstructor()
        {
            SerialPortStream src = new SerialPortStream();

            src.Dispose();
            Assert.That(src.IsDisposed, Is.True);
        }
示例#5
0
 private void Close()
 {
     lock (accessLock)
     {
         // Stop the Reader task
         if (reader != null)
         {
             if (!reader.Join(5000))
             {
                 readerCts.Cancel();
             }
             reader = null;
         }
         if (_serialPort != null)
         {
             _serialPort.ErrorReceived -= HandleErrorReceived;
             if (_serialPort.IsOpen)
             {
                 _serialPort.Close();
                 OnConnectionStatusChanged(new ConnectionStatusChangedEventArgs(false));
             }
             _serialPort.Dispose();
             _serialPort = null;
         }
         gotReadWriteError = true;
     }
 }
 public bool Initialize()
 {
     try
     {
         header = new byte[MAGIC.Length + 3];
         Array.Copy(MAGIC, header, MAGIC.Length);
         byte hi, lo, chk;
         hi  = (byte)((LedCount - 1) >> 8);
         lo  = (byte)((LedCount - 1) & 0xff);
         chk = (byte)(hi ^ lo ^ 0x55);
         header[MAGIC.Length]     = hi;
         header[MAGIC.Length + 1] = lo;
         header[MAGIC.Length + 2] = chk;
         if (port != null)
         {
             lock (port)
             {
                 port.Dispose();
             }
         }
         port          = new SerialPortStream();
         port.PortName = SerialPort;
         port.BaudRate = Baudrate;
         port.Open();
         AssignLeds();
         isInitialized = true;
         crashed       = false;
         return(true);
     }
     catch (Exception ex)
     {
         Crash(ex, "Initialization");
         return(false);
     }
 }
 public void Dispose()
 {
     if (sp != null)
     {
         sp.Dispose();
     }
 }
示例#8
0
 public void Dispose()
 {
     if (_serialPort.IsOpen)
     {
         _serialPort.Close();
     }
     _serialPort.Dispose();
 }
        public void SimpleConstructorWithPort()
        {
            SerialPortStream src = new SerialPortStream(SourcePort);

            Assert.That(src.PortName, Is.EqualTo(SourcePort));
            src.Dispose();
            Assert.That(src.IsDisposed, Is.True);
        }
示例#10
0
        public void Dispose()
        {
            _reader?.Close();
            _reader?.Dispose();

            _writer?.Close();
            _writer?.Dispose();

            _portStream?.Close();
            _portStream?.Dispose();
        }
示例#11
0
 public void Dispose()
 {
     if (port != null)
     {
         if (port.IsOpen)
         {
             port.Close();
         }
         port.Dispose();
     }
 }
示例#12
0
 /// <summary>
 /// デバイスを破棄します
 /// </summary>
 public void Dispose()
 {
     _logger.LogTrace("Dispose");
     if (serialPort.IsOpen)
     {
         serialPort.Close();
     }
     if (!serialPort.IsDisposed)
     {
         serialPort.Dispose();
     }
 }
        public void DisposedWhenBlocked()
        {
            byte[] buffer = new byte[1024];

            SerialPortStream serialSource = new SerialPortStream(SourcePort, 115200, 8, Parity.None, StopBits.One);
            Thread           testThread;

            using (ManualResetEvent disposedEvent = new ManualResetEvent(false))
                using (SerialPortStream serialDest = new SerialPortStream(DestPort, 115200, 8, Parity.None, StopBits.One)) {
                    serialSource.ReadBufferSize  = 8192;
                    serialSource.WriteBufferSize = 8192;
                    serialDest.ReadBufferSize    = 8192;
                    serialDest.WriteBufferSize   = 8192;
                    serialSource.Handshake       = Handshake.Rts;
                    serialSource.Open();
                    serialDest.Open();

                    serialDest.RtsEnable = false;
                    Thread.Sleep(100);

                    testThread = new Thread(
                        () => {
                        Thread.Sleep(2000);
                        Console.WriteLine("Disposing serialSource");

                        // It appears that the MSDN .NET implementation blocks here, never
                        // to return as we're blocked on another thread.
                        disposedEvent.Set();
                        serialSource.Dispose();
                        Console.WriteLine("Disposed serialSource");
                    }
                        );
                    testThread.Start();

                    Assert.That(
                        () => {
                        int bufferCount = 1024 * 1024;
                        while (bufferCount > 0)
                        {
                            serialSource.Write(buffer, 0, buffer.Length);
                            if (disposedEvent.WaitOne(0))
                            {
                                Assert.Fail("Write returned after being disposed.");
                            }
                            bufferCount -= buffer.Length;
                            Console.WriteLine("{0}", bufferCount);
                        }
                    }, Throws.InstanceOf <ObjectDisposedException>());
                }

            testThread.Join(20000);
            Console.WriteLine("Finished");
        }
示例#14
0
        public void Dispose()
        {
            _cancellationToken.Cancel();

            _cancellationToken.Dispose();

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

            _serialPort.Dispose();
        }
示例#15
0
 public void Stop()
 {
     try
     {
         _stream?.Dispose();
         _thread?.Abort();
         _wdaqFileService.CleanUp(_currentFile);
     }
     catch (Exception exception)
     {
         _logService.Log(exception, LogEventLevel.Fatal, "Ha occurrido un error");
     }
 }
示例#16
0
 public void Close(bool auto)
 {
     if (com != null && com.IsOpen)
     {
         ComLogger.Log("com", string.Format("Close {0} [{1}]", com.PortName.ToUpper(), auto ? "CORE" : "USER"));
         Logger.LogMessage("CloseCom", "Close {0} [{1}]", com.PortName.ToUpper(), auto ? "CORE" : "USER");
         try { com.DiscardOutBuffer(); } catch { }
         try { com.DiscardInBuffer(); } catch { }
         try { com.Close(); } catch { }
         try { com.Dispose(); } catch { }
     }
     com = null;
 }
示例#17
0
        public Dictionary <string, int> GetAvailablePortsAndAutoDiscover()
        {
            trackers = new Dictionary <string, int>();
            foreach (string portName in SerialPortStream.GetPortNames())
            {
                Console.WriteLine("Autodiscover port:");
                Console.WriteLine(portName);
                try
                {
                    autoDiscoverPort = new SerialPortStream(portName, 115200, 8, Parity.None, StopBits.One)
                    {
                        StopBits     = StopBits.One,
                        WriteTimeout = 1000,
                        ReadTimeout  = 3000
                    };
                    stopWatch = new Stopwatch();
                    stopWatch.Start();
                    autoDiscoverPort.DataReceived += SerialPortAutoDiscover;
                    autoDiscoverPort.Open();
                    autoDiscoverPort.WriteLine(SerialMessageParser.Encode("DISCOVERY"));
                }
                catch (UnauthorizedAccessException)
                {
                    autoDiscoverPort.Close();
                }
                catch (System.IO.IOException)
                {
                    autoDiscoverPort.Close();
                }
                catch (TimeoutException)
                {
                    autoDiscoverPort.Close();
                }

                while (autoDiscoverPort.IsOpen)
                {
                    if (stopWatch.ElapsedMilliseconds >= 3000)
                    {
                        autoDiscoverPort.Close();
                    }
                }
                autoDiscoverPort.Dispose();
            }
            return(trackers);
        }
示例#18
0
        private void ReadFromPort()
        {
            try
            {
                _stream = new SerialPortStream(_request.PortNumber, _request.BaudRate, 8, Parity.None, StopBits.One);
                _stream.Open();

                try
                {
                    while (true)
                    {
                        var data = _stream.ReadLine();

                        if (!IsValid(data))
                        {
                            _logService.Log(LogEventLevel.Information, "Dato invalido");
                            continue;
                        }


                        _logService.Log(LogEventLevel.Information, "data: {data}", data);

                        var reading = _dataParser.GetReading(data);

                        DataReceived?.Invoke(this, reading);

                        _wdaqFileService.WriteToFile(reading, _currentFile);
                    }
                }
                catch (Exception exception)
                {
                    _logService.Log(exception, LogEventLevel.Fatal, "Ha occurrido un error");
                }
            }
            catch (Exception e)
            {
                _logService.Log(e, LogEventLevel.Fatal, "Ha occurrido un error");
            }
            finally
            {
                _stream?.Dispose();
            }
        }
示例#19
0
 public async Task <Stream> GetStreamAsync()
 {
     return(await Task.Run <Stream>(() =>
     {
         if ((stream == null) || (!stream.IsOpen))
         {
             stream?.Close();
             stream?.Dispose();
             Logger.Info($"Creating SerialPortStream {PortName} {BaudRate}");
             stream = new SerialPortStream(PortName, BaudRate, DataBits, Parity, StopBits);
             //{ ReadTimeout = ReadTimeout, WriteTimeout = WriteTimeout };
         }
         if (!stream.IsOpen)
         {
             stream.Open();
         }
         return stream;
     }));
 }
示例#20
0
 public override void CommClose(bool ignore_err)
 {
     try
     {
         CommRts(false, true);
         CommDtr(false, true);
         serial.Close();
     }
     catch (Exception ex) { if (!ignore_err)
                            {
                                throw new Bsl430NetException(781, ex);
                            }
     }
     finally
     {
         try
         {
             serial.Dispose();
         }
         catch (Exception) { }
     }
 }
示例#21
0
        public FanController(string portPath)
        {
            uint l = 0;

            _port = new SerialPortStream(portPath, baudrate);
            _port.Open();

            //Wait until data arrived, we expect 15 bytes
            l = 0;
            while (_port.BytesToRead == 0)
            {
                Thread.Sleep(10);
                l++;
                if (l == timeout)
                {
                    _port.Close();
                    _port.Dispose();
                    _port = null;
                    throw new OperationCanceledException("Timeout");
                }
            }

            string ident = _port.ReadExisting();

            Console.WriteLine("--- Hello message start --- ");
            Console.WriteLine(ident);
            Console.WriteLine("--- Hello message end --- ");

            if (!ident.Contains(IDMSG))
            {
                _port.Close();
                _port.Dispose();
                _port = null;
                throw new OperationCanceledException("Not a fan controller!");
            }
        }
示例#22
0
        public SerialPortStream OpenPort(string portName)
        {
            SerialPortStream serialPort = null;

            var retries = 3;

            while (retries > 0)
            {
                try
                {
                    serialPort = new SerialPortStream(portName, 9600, 8, Parity.None, StopBits.One);
                    serialPort.DiscardInBuffer();
                    serialPort.DiscardOutBuffer();
                    serialPort.ReadTimeout = 1000;
                    serialPort.Open();
                    break;
                }
                catch (Exception e)
                {
                    if (serialPort != null)
                    {
                        if (serialPort.IsOpen)
                        {
                            serialPort.Close();
                        }
                        serialPort.Dispose();
                        serialPort = null;
                    }

                    Console.WriteLine("Exception in OpenPort " + e.Message);
                    retries--;
                }
            }

            return(serialPort);
        }
示例#23
0
        protected override void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    if(Connected && IsScanning)
                    {
                        // make sure we're in a halfway known state...
                        StopScan();
                    }

                    if(serialPort.IsOpen)
                        serialPort.Flush();

                    if(!serialPort.IsDisposed)
                        serialPort.Dispose();
                }

                disposedValue = true;
            }

            base.Dispose(disposing);
        }
示例#24
0
 public override void Dispose()
 {
     _serialStream.Dispose();
 }
示例#25
0
 public void Dispose()
 {
     _port?.Dispose();
 }
示例#26
0
 public void Dispose()
 {
     port?.Dispose();
     connection?.Dispose();
 }
 void IDisposable.Dispose() => _serialPort?.Dispose();
示例#28
0
 public void Close()
 {
     stream.Close();
     stream.Dispose();
     stream = null;
 }
 public void Dispose()
 {
     _serial?.Dispose();
 }
 public void Dispose()
 {
     serialPort.Dispose();
 }