Beispiel #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello Serial port!");
            var  ports = SerialDevice.GetPortNames();
            bool isTTY = false;

            foreach (var prt in ports)
            {
                Console.WriteLine($"Serial name: {prt}");
                if (prt.Contains("ttyS0"))
                {
                    isTTY = true;
                }
            }
            if (!isTTY)
            {
                Console.WriteLine("No ttyS0 serial port!");
                return;
            }
            Console.WriteLine("Yes, we have the embedded serial port available, opening it");
            SerialDevice mySer = new SerialDevice("/dev/ttyS0", BaudRate.B1152000);

            mySer.DataReceived += MySer_DataReceived;
            mySer.Open();
            while (!Console.KeyAvailable)
            {
                ;
            }
            mySer.Close();
        }
Beispiel #2
0
 public void Release()
 {
     if (SerialDevice != null && SerialDevice.IsOpen == true)
     {
         SerialDevice.Close();
     }
 }
Beispiel #3
0
        public void Close()
        {
            lock (_openCloseLock)
            {
                _isClosing = true;

                if (_receiveTask == null)
                {
                    return;
                }

                _readCancellationTokenSource.Cancel();

#if !NETCORE
                _serialPort.Close();
#endif

                try
                {
                    _receiveTask.Wait();
                }
                catch (AggregateException)
                {
                }

                _readCancellationTokenSource.Dispose();

                _receiveTask = null;

                _isClosing = false;
            }
        }
Beispiel #4
0
 /// <summary>
 /// 关闭串口资源
 /// </summary>
 public void Close()
 {
     try
     {
         SerialDevice.Close();
         SerialDevice.Dispose();
     }
     catch (Exception ex)
     {
         ExceptionProcess(ex);
     }
 }
Beispiel #5
0
 static void Main(string[] args)
 {
     //Classify();
     //Console.ReadLine();
     while (!IsSerialAvailable())
     {
         Console.WriteLine("Serial port not available, waiting 5 seconds");
         Thread.Sleep(5000);
     }
     Console.WriteLine("Serial port available, opening");
     mySer = new SerialDevice("/dev/ttyUSB0", BaudRate.B9600);
     mySer.DataReceived += MySer_DataReceived;
     outputComponents    = new IOutputComponent[] { new EmailOutput(), new SerialOutput(mySer) };
     mySer.Open();
     while (!Console.KeyAvailable)
     {
         ;
     }
     mySer.Close();
 }
Beispiel #6
0
        public void Close()
        {
            lock (_openCloseLock)
            {
                if (_receiveTask == null)
                {
                    return;
                }

                _readCancellationTokenSource.Cancel();

#if !NETCORE
                _serialPort.Close();
#endif

                _receiveTask.Wait();
                _readCancellationTokenSource.Dispose();

                _receiveTask = null;
            }
        }
Beispiel #7
0
 public bool Close()
 {
     //Ensure port is opened before attempting to close:
     if (sp.IsOpen)
     {
         try
         {
             sp.Close();
         }
         catch (Exception err)
         {
             modbusStatus = "Error closing " + sp.PortName + ": " + err.Message;
             return(false);
         }
         modbusStatus = sp.PortName + " closed successfully";
         return(true);
     }
     else
     {
         modbusStatus = sp.PortName + " is not open";
         return(false);
     }
 }
Beispiel #8
0
 public void Dispose()
 {
     _sps?.Close();
     _sps.Dispose();
 }