当前位置: 首页>>代码示例>>C#>>正文


C# SerialPort.Close方法代码示例

本文整理汇总了C#中System.IO.Ports.SerialPort.Close方法的典型用法代码示例。如果您正苦于以下问题:C# SerialPort.Close方法的具体用法?C# SerialPort.Close怎么用?C# SerialPort.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.Ports.SerialPort的用法示例。


在下文中一共展示了SerialPort.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: IsPVC

		bool IsPVC(SerialPort port)
		{
			string returnMessage = "";
			try {
				if (port.IsOpen)
					port.Close ();

				port.Open ();

				Thread.Sleep (1500); // Fails if this delay is any shorter

				port.Write ("#");
				port.Write (port.NewLine);

				Thread.Sleep (500); // Fails if this delay is any shorter

				int count = port.BytesToRead;
				int intReturnASCII = 0;
				while (count > 0) {
					intReturnASCII = port.ReadByte ();
					returnMessage = returnMessage + Convert.ToChar (intReturnASCII);
					count--;
				}
			} catch {
			} finally {
				port.Close ();
			}
			if (returnMessage.Contains (Identifier)) {
				return true;
			} else {
				return false;
			}
		}
开发者ID:abrankhalid,项目名称:PVCharacterization,代码行数:33,代码来源:DuinoPortDetector.cs

示例2: QueryDevice

 internal static void QueryDevice(string port)
 {
     try
     {
         tP = new SerialPort(port, 115200, Parity.None, 8, StopBits.One);
         tP.DataReceived += new SerialDataReceivedEventHandler(tP_DataReceived);
         tP.ErrorReceived += new SerialErrorReceivedEventHandler(tP_ErrorReceived);
         tP.Handshake = Handshake.RequestToSend;
         tP.DtrEnable = true;
         tP.RtsEnable = true;
         tP.NewLine = Environment.NewLine;
         tP.ReadTimeout = 2000;
         tP.WriteTimeout = 2000;
         tP.Open();
         Thread.Sleep(200);
         tP.Write("AT+GMM" + (char)13);
         Thread.Sleep(500);
         tP.Close();
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
     }
     finally
     {
         if (tP.IsOpen)
             tP.Close();
     }
 }
开发者ID:madhurjain,项目名称:AutoServeJobsManager,代码行数:29,代码来源:Texting.cs

示例3: SearchPort

        public static AsyncSerial SearchPort(string write, char search, int baud=9600)
        {
            foreach (string port in SerialPort.GetPortNames())
            {
                SerialPort temp;
                try
                {
                    temp = new SerialPort(port, baud);
                    try
                    {
                        temp.Open();
                        temp.ReadTimeout = 2500;
                        temp.Write(write);
                        var v = temp.ReadChar();
                        MainForm.Instance.logControl.Add(v.ToString(), LogEntryType.Error);
                        if (v == search)
                        {
                            temp.Close();
                            return new AsyncSerial(port);
                        }
                    }
                    catch (Exception ex)
                    {}
                    finally
                    {
                        temp.Close();
                    }
                }
                catch (Exception ex)
                {}
            }

            throw new Exception("Port not found.");
        }
开发者ID:DavidTyler,项目名称:Door-Lock,代码行数:34,代码来源:AsyncSerial.cs

示例4: HamegOsziAdapter

 public HamegOsziAdapter(string port, int baudrate)
 {
     try{
     log.Info("Try to connecto to measurment device on port " + port + " with baud rate " + baudrate.ToString());
         dataPort = new SerialPort(port, baudrate);
         dataPort.Open();
         if(!dataPort.IsOpen)
         {
             log.Error("Could not open comport on " + port + " with baudrate " + baudrate);
             dataPort.Close();
             return;
         }
         dataPort.WriteLine("*IDN?");
         System.DateTime start = DateTime.Now;
         while(dataPort.BytesToRead == 0)
         {
             if((DateTime.Now - start).TotalMilliseconds > 1000)
             {
                 log.Error("No device response to *IDN? within 1s!");
                 dataPort.Close();
                 return;
             }
         }
         deviceInfo = dataPort.ReadExisting();
         log.Info("Connected to device " + deviceInfo);
     }
     catch(Exception ex)
     {
         log.Error(ex);
     }
 }
开发者ID:cloidnerux,项目名称:HamegOsziAdapter,代码行数:31,代码来源:HamegOsziAdapter.cs

示例5: Identify

        public string Identify(SerialPort port)
        {
            string returnMessage = "";
            try {
                if (port.IsOpen)
                    port.Close ();

                port.Open ();

                Thread.Sleep (1500); // Fails sometimes if this delay is any shorter

                port.Write (IdentifyRequest);
                port.Write (port.NewLine);

                Thread.Sleep (500); // Fails sometimes if this delay is any shorter

                int count = port.BytesToRead;
                int intReturnASCII = 0;
                while (count > 0) {
                    intReturnASCII = port.ReadByte ();
                    returnMessage = returnMessage + Convert.ToChar (intReturnASCII);
                    count--;
                }
            } catch {
            } finally {
                port.Close ();
            }

            return returnMessage.Trim();
        }
开发者ID:CompulsiveCoder,项目名称:duinocom,代码行数:30,代码来源:DuinoIdentifier.cs

示例6: OpenCash

 public void OpenCash()
 {
     SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
     try
     {
         port.Open();
         port.WriteLine("0000000000");
         port.Close();
     }
     catch (Exception)
     {
         port.Close();
     }
 }
开发者ID:itktc,项目名称:projectktc-v2,代码行数:14,代码来源:frmSaleCOCalMoney.cs

示例7: btnCashDrawer_Click

 private void btnCashDrawer_Click(object sender, EventArgs e)
 {
     SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);
     try
     {
         port.Open();
         port.WriteLine("0000000000");
         port.Close();
     }
     catch (Exception)
     {
         port.Close();
     }
 }
开发者ID:itktc,项目名称:projectktc-v2,代码行数:14,代码来源:rptSlipForBrance.cs

示例8: GetTemperature

 public XmlElement GetTemperature(string port, string baudRate)
 {
     try
     {
         _serialPort = new SerialPort(port);
         _serialPort.BaudRate = Convert.ToInt32(baudRate);
         _serialPort.Parity = Parity.None;
         _serialPort.StopBits = StopBits.One;
         _serialPort.DataBits = 8;
         _serialPort.Handshake = Handshake.None;
         _serialPort.RtsEnable = true;
         _serialPort.ReadTimeout = 1000;
         if (_serialPort.IsOpen)
         {
             _serialPort.Close();
             _serialPort.Open();
         }
         else
         {
             _serialPort.Open();
         }
         Thread.Sleep(1000);
         count = _serialPort.BytesToRead;
         if (count < 1)
         {
             throw new Exception("No Data to Read..."+count);
         }
         else
         {
             while (count > 0)
             {
                 int byteData = _serialPort.ReadByte();
                 data = data + Convert.ToChar(byteData);
                 count--;
             }
         }
         _serialPort.DiscardOutBuffer();
         _serialPort.Close();
         _result = GetXML(data.Trim());
     }
     catch (Exception ex)
     {
         if (_serialPort.IsOpen)
             _serialPort.Close();
         _result = GetExceptionXML(ex.ToString());
     }
     return _result;
 }
开发者ID:stellant,项目名称:LaserpointFinal,代码行数:48,代码来源:Temperature.svc.cs

示例9: Main

        static void Main(string[] args)
        {
            // initialize the sensor port, mine was registered as COM8, you may check yours
            // through the hardware devices from control panel
            SerialPort sensor = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
            int bytesToRead = 0;
            string message;
            sensor.Open();
            try
            {
                while (true)
                {
                    // check if there are bytes incoming
                    bytesToRead = sensor.BytesToRead;
                    if (bytesToRead > 0)
                    {
                        byte[] input = new byte[bytesToRead];
                        // read the Xbee's input
                        sensor.Read(input, 0, bytesToRead);
                        // convert the bytes into string
                        message = System.Text.Encoding.UTF8.GetString(input);
                        Console.WriteLine(message);
                    }
                }

            }
            finally
            {
                // again always close the serial ports!
                sensor.Close();
            }
        }
开发者ID:jsnmc,项目名称:XBeeTest,代码行数:32,代码来源:Program.cs

示例10: GetComPorts

        /// <summary>
        /// Gets the available com ports from the system.
        /// </summary>
        /// <returns></returns>
        public List<string> GetComPorts()
        {
            List<string> validPorts = new List<string>();
            // Get a list of serial port names.
            string[] ports = SerialPort.GetPortNames();

            //Console.WriteLine("The following serial ports were found:");

            // Display each port name to the console.
            foreach (string port in ports)
            {
                try
                {
                    SerialPort testPort = new SerialPort(port);
                    testPort.Open();
                    testPort.Close();
                    validPorts.Add(port);
                }
                catch (Exception)
                {

                }
            }

            return validPorts;
        }
开发者ID:jonathan84clark,项目名称:JTool,代码行数:30,代码来源:PortMonitor.cs

示例11: SerialPortConfig

        public SerialPortConfig(SerialPort serialPort, bool allowPortEdit = true, bool allowBaudEdit = true, bool allowParityEdit = true, bool allowDataEdit = true, bool allowStopEdit = true)
        {
            InitializeComponent();

            //lets try and open the serial port if it can't be opened then it
            //must be in use so label it as in use
            foreach (string s in SerialPort.GetPortNames())
            {
                try
                {

                    using (SerialPort checkPort = new SerialPort(s))
                    {
                        checkPort.Open();
                        checkPort.Close();
                    }
                    comboBoxPortName.Items.Add(s);

                }
                //catch the exception in case we want to use it
                //or log it.
                catch (UnauthorizedAccessException uae)
                {
                    comboBoxPortName.Items.Add(s + " (IN USE)");
                    continue;
                }

            }
            comboBoxPortName.Enabled = allowPortEdit;
            comboBoxBaudRate.Enabled = allowBaudEdit;
            comboBoxParity.Enabled = allowParityEdit;
            textBoxDataBits.Enabled = allowDataEdit;
            comboBoxStopBits.Enabled = allowStopEdit;

            comboBoxParity.Items.AddRange(Enum.GetValues(typeof(Parity)).Cast<object>().ToArray());
            comboBoxStopBits.Items.AddRange(Enum.GetValues(typeof(StopBits)).Cast<object>().ToArray());

            //set our text value
            if (serialPort != null)
            {
                configuredPortValueLabel.Text = serialPort.PortName;
            }
            else
            {
                configuredPortValueLabel.Text = "None";
            }

            if (serialPort == null && SerialPort.GetPortNames().Count() > 0)
            {
                serialPort = new SerialPort(SerialPort.GetPortNames().FirstOrDefault(), 57600, Parity.None, 8, StopBits.One);
            }

            SelectedPort = serialPort;

            //set our first item in the combobox if we have one
            if (comboBoxPortName.Items.Count > 0)
            {
                comboBoxPortName.SelectedIndex = 0;
            }
        }
开发者ID:alphageek-gb,项目名称:vixen-modules,代码行数:60,代码来源:SerialPortConfig.cs

示例12: Main

 static void Main(string[] args)
 {
     System.IO.Ports.SerialPort wixel = new System.IO.Ports.SerialPort("COM20", 57600);
     wixel.Open();
     wixel.Write("hello");
     wixel.Close();
 }
开发者ID:brianlu365,项目名称:senior-design-2012,代码行数:7,代码来源:Program.cs

示例13: Main

        static void Main(string[] args)
        {
            int i = 0;
            SerialPort SP = null;
            Console.WriteLine("Specify which comport you want to connect to:");
            
            foreach (string s in SerialPort.GetPortNames())
            {
                Console.WriteLine($"{i++,2} {s}");
            }
            int PortNo = int.Parse(Console.ReadLine());
            try
            {
                SP = new SerialPort(SerialPort.GetPortNames()[PortNo]);
                SP.Open();
                while(true)
                {
                    Console.Write(SP.ReadExisting());
                }
            }
            finally
            {
                if (SP.IsOpen)
                {
                    SP.Close();
                }
            }

            Console.Read();
        }
开发者ID:svutborg,项目名称:Programming-II,代码行数:30,代码来源:Program.cs

示例14: buttonSend_Click

        private void buttonSend_Click(object sender, EventArgs e)
        {
            string number = textBoxNumber.Text;
            string message = textBoxMessage.Text;

            _serialPort = new SerialPort("COM7", 115200);   //Replace "COM7" with corresponding port name

            Thread.Sleep(1000);

            _serialPort.Open();

            Thread.Sleep(1000);

            _serialPort.Write("AT+CMGF=1\r");

            Thread.Sleep(1000);

            _serialPort.Write("AT+CMGS=\"" + number + "\"\r\n");

            Thread.Sleep(1000);

            _serialPort.Write(message + "\x1A");

            Thread.Sleep(1000);

            labelStatus.Text = "Status: Message sent";

            _serialPort.Close();
        }
开发者ID:CircuitFreakCoder,项目名称:CSharp_SMS,代码行数:29,代码来源:Form1.cs

示例15: Main

        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: SerialReceiver [port] [baudrate]");
                Environment.Exit(0);
            }

            // Initialization
            serial = new SerialPort(args[0], Convert.ToInt32(args[1]));
            Thread readThread = new Thread(Read);

            serial.ReadTimeout = 500;
            serial.WriteTimeout = 500;

            serial.Open();
            running = true;
            readThread.Start();

            Console.WriteLine("Type QUIT to exit");

            // Allow users to quit the application nicely
            while (running) {
                String message = Console.ReadLine();
                if (stringComparer.Equals("quit", message))
                    running = false;
            }

            // Clean up
            readThread.Join();
            serial.Close();
        }
开发者ID:rohankapoorcom,项目名称:Red-Alert-Button,代码行数:32,代码来源:Program.cs


注:本文中的System.IO.Ports.SerialPort.Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。