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


C# SerialPort.Close方法代码示例

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


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

示例1: Chronopic

    //-- Constructor
    public Chronopic(SerialPort sp)
    {
        //-- Comprobar si puerto serie ya estaba abierto
        if (sp != null)
            if (sp.IsOpen)
                sp.Close();

        //-- Abrir puerto serie
        sp.Open();

        //-- Configurar timeout por defecto
        //de momento no en windows (hasta que no encontremos por qué falla)
        //OperatingSystem os = Environment.OSVersion;
        //not used, now there's no .NET this was .NET related
        //on mono timeouts work on windows and linux
        //if( ! os.Platform.ToString().ToUpper().StartsWith("WIN"))
            sp.ReadTimeout = DefaultTimeout;

        //-- Guardar el puerto serie
        this.sp = sp;

        //		//-- Vaciar buffer
        //		//done in a separate method
        //		this.flush();
    }
开发者ID:GNOME,项目名称:chronojump,代码行数:26,代码来源:chronopic.cs

示例2: checkConnection

    // The state object is necessary for a TimerCallback.
    public void checkConnection(object stateObject)
    {
        Process p = new Process();
        Ping pingSender = new Ping ();
        p.StartInfo.FileName = "arp";
        p.StartInfo.Arguments = "-a";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;

        string data = "a";
         byte[] buffer = Encoding.ASCII.GetBytes (data);

        for(int i = 0; i < 25 ; i++){
            pingSender.Send ("10.0.0."+i.ToString(),10,buffer);
        }

        p.Start();

        string output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();

        string MAC = "xx-xx-xx-xx-xx-xx";
        if(output.Contains(MAC)){
            SerialPort port = new SerialPort("COM5", 9600);
            port.Open();
            port.Write("u");
            port.Close();
        }
        else{
            SerialPort port = new SerialPort("COM5", 9600);
            port.Open();
            port.Write("l");
            port.Close();
        }
    }
开发者ID:kurtvonehr,项目名称:ARP-Windows-Service,代码行数:36,代码来源:wifi_doorlock_service.cs

示例3: Start

    // Use this for initialization
    void Start()
    {
        Debug.Log(SerialPort.GetPortNames().ToString());
        mySerialPort = new SerialPort("\\\\.\\COM18");
        mySerialPort.BaudRate = 9600;
        //mySerialPort.Parity = Parity.None;
        //mySerialPort.StopBits = StopBits.One;
        //mySerialPort.DataBits = 8;
        //mySerialPort.Handshake = Handshake.None;
        //mySerialPort.RtsEnable = true;

        //mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);

        if ( mySerialPort != null )
        {
            if ( mySerialPort.IsOpen ) // close if already open
            {
                mySerialPort.Close();
                Debug.Log ("Closed stream");
            }
            mySerialPort.Open();
            Debug.Log ("Opened stream");
        }
        else
        {
            Debug.Log ("ERROR: Uninitialized stream");
        }
          myThread = new Thread(new ThreadStart(GetArduino));
          	  myThread.Start();
    }
开发者ID:am-t,项目名称:mount-amanita,代码行数:31,代码来源:ArduinoSerialInterface.cs

示例4: Start

	// Use this for initialization
	void Start () {
		sp = new SerialPort(serialPortName, 9600);
        if (sp.IsOpen)
        {
            sp.Close();
        }
        else {
            sp.Open();
            sp.ReadTimeout = 1;
        }
	}
开发者ID:se210,项目名称:AbulaHoosh,代码行数:12,代码来源:Arduino.cs

示例5: Main

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        
        _serialPort = new SerialPort();


        _serialPort.PortName = "COM2";
        _serialPort.BaudRate = 9600;
        _serialPort.Parity = Parity.None;
        _serialPort.DataBits = 8;
        _serialPort.StopBits = StopBits.One;
        _serialPort.Handshake = Handshake.XOnXOff;

        // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

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

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(" " + message + " ");
                    //String.Format("{0}:{1}", name, message));
            }
        }

        readThread.Join();
        _serialPort.Close();
        _serialPort = null;
    }
开发者ID:Jevaan,项目名称:Strandmollen,代码行数:50,代码来源:Program.cs

示例6: MainS

    public static void MainS()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = SetPortName(_serialPort.PortName);
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
        _serialPort.Parity = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

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

        while(_continue)
        {
            message = Console.ReadLine();

            if(stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(
                    String.Format("<{0}>: {1}", name, message));
            }
        }

        readThread.Join();
        _serialPort.Close();
    }
开发者ID:Nimelo,项目名称:IWSK,代码行数:49,代码来源:Program.cs

示例7: Main

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        _serialPort.PortName = "COM3";
        _serialPort.BaudRate = 9600;
        _serialPort.Parity = Parity.None;
        _serialPort.DataBits = 8;
        _serialPort.StopBits = StopBits.One;
        _serialPort.Handshake = Handshake.None;

        // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

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

        byte[] Command = new byte[] { Convert.ToByte(1), Convert.ToByte(4), Convert.ToByte(1), Convert.ToByte(30) };

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            }
            else
            {
                _serialPort.Write(Command, 0, 4);
            }
        }
        
        _serialPort.Close();
    }
开发者ID:tummyacid,项目名称:VS2015,代码行数:49,代码来源:Program.cs

示例8: Main

 public static void Main(string[] args)
 {
     SerialPort sp = new SerialPort("/dev/ttyUSB0", 115200);
     sp.Open();
     try {
         byte[] b = new byte[1];
         while (true) {
             int i = Console.Read();
             if (i == -1) break;
             b[0] = (byte)i;
             sp.Write(b, 0, 1);
         }
     } catch {
         sp.Close();
     }
 }
开发者ID:dnet,项目名称:sidbox-ctrl,代码行数:16,代码来源:usb.cs

示例9: Main

    public static void Main()
    {
        string name;
        string message;
        StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
        Thread readThread = new Thread(Read);

        // Создание нового объекта SerialPort с установками по умолчанию.
        _serialPort = new SerialPort();

        // Позволяем пользователю установить подходящие свойства.
        _serialPort.PortName = SetPortName(_serialPort.PortName);
        _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate);
        _serialPort.Parity = SetPortParity(_serialPort.Parity);
        _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits);
        _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits);
        _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

        // Установка таймаутов чтения/записи (read/write timeouts)
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;
        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

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

        while (_continue)
        {
            message = Console.ReadLine();

            if (stringComparer.Equals("quit", message))
            {
                _continue = false;
            } else
            {
                _serialPort.WriteLine(
                String.Format("< {0} >: {1}", name, message));
            }
        }

        readThread.Join();
        _serialPort.Close();
    }
开发者ID:rukpet,项目名称:COM-Port,代码行数:47,代码来源:Program.cs

示例10: SendInfo

 public static string SendInfo(HttpListenerRequest request)
 {
     string context = "";
     Boolean succeeded = false;
     int count = 0;
     SerialPort serialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
     while (!succeeded)
     {
         try
         {
             serialPort.Open();
             int cm1 = Convert.ToInt32(serialPort.ReadLine());
             int cm2 = Convert.ToInt32(serialPort.ReadLine());
             if (cm1 >= 30)
             {
                 context = "false:" + cm1.ToString();
                 Console.WriteLine(context);
             }
             else
             {
                 context = "true:" + cm1.ToString();
                 Console.WriteLine(context);
             }
             succeeded = true;
             if (cm1 != cm2)
             {
                 succeeded = false;
                 count++;
             }
             if (count>=9){
                 context = "false:outOfRange";
                 Console.WriteLine(context);
                 succeeded = true;
             }
         }
         catch (Exception ex)
         {
             context = "open error " + ex.Message;
         }
         finally
         {
             serialPort.Close();
         }
     }
     return string.Format(context);
 }
开发者ID:g23988,项目名称:holyshit,代码行数:46,代码来源:main.cs

示例11: Main

    public static void Main()
    {
        string name;
        string message;

        var readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
        _serialPort = new SerialPort();

        // Allow the user to set the appropriate properties.
        InitializePort(_serialPort);

        // Set the read/write timeouts
        _serialPort.ReadTimeout = 500;
        _serialPort.WriteTimeout = 500;

        _serialPort.Open();
        _continue = true;
        readThread.Start();

        Console.Write("Name: ");
        name = Console.ReadLine();

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

        while (_continue)
        {
            message = Console.ReadLine();

            if (message != null && "quit" ==  message.ToLowerInvariant())
            {
                _continue = false;
            }
            else
            {
                _serialPort.WriteLine(
                    String.Format("<{0}>: {1}", name, message));
            }
        }

        readThread.Join();
        _serialPort.Close();
    }
开发者ID:vbre,项目名称:CS_2015_Winter,代码行数:44,代码来源:Program.cs

示例12: BUT_resettodefault_Click

        private void BUT_resettodefault_Click(object sender, EventArgs e)
        {
            ArdupilotMega.Comms.ICommsSerial comPort = new SerialPort();

            try
            {
                comPort.PortName = MainV2.comPort.BaseStream.PortName;
                comPort.BaudRate = MainV2.comPort.BaseStream.BaudRate;

                comPort.ReadTimeout = 4000;

                comPort.Open();

            }
            catch { CustomMessageBox.Show("Invalid ComPort or in use"); return; }

            lbl_status.Text = "Connecting";

            if (doConnect(comPort))
            {
                // cleanup
                doCommand(comPort, "AT&T");

                comPort.DiscardInBuffer();

                lbl_status.Text = "Doing Command ATI & AT&F";

                doCommand(comPort, "AT&F");

                doCommand(comPort, "AT&W");

                lbl_status.Text = "Reset";

                doCommand(comPort, "ATZ");

                comPort.Close();

                BUT_getcurrent_Click(sender, e);
            }
            else
            {

                // off hook
                doCommand(comPort, "ATO");

                lbl_status.Text = "Fail";
                CustomMessageBox.Show("Failed to enter command mode");
            }

            if (comPort.IsOpen)
                comPort.Close();
        }
开发者ID:rezatoops,项目名称:MissionPlanner,代码行数:52,代码来源:3DRradio.cs

示例13: OpenConnection

	void OpenConnection(SerialPort _stream) {
		
		if (_stream != null) {
			
			if (_stream.IsOpen) {
				_stream.Close ();
				Debug.LogError ("Failed to open Serial Port, already open!");
			} else {
				_stream.Open ();
				_stream.ReadTimeout = 10;
				_stream.WriteTimeout = 40;
				
				Debug.Log ("Open Serial port1");      
			}
		}
		
	}
开发者ID:ekusy,项目名称:Wall-of-Kabe,代码行数:17,代码来源:ArduinoSerialIOScript.cs

示例14: Main

    public static void Main()
    {
        string message;

        //Configuration c = new Configuration();

        //Configuration c = Configuration.Deserialize("u2wits.xml");

         StringComparer stringComparer = StringComparer.OrdinalIgnoreCase;
         Thread readThread = new Thread(Read);

        // Create a new SerialPort object with default settings.
         _uniproPort = new SerialPort();
         _witsPort = new SerialPort();

        // Allow the user to set the appropriate properties.
         if (c.uniproName == "") c.uniproName = SetPortName(_uniproPort.PortName);
         _uniproPort.PortName = c.uniproName;

         if (c.uniproBaudRate == -1) c.uniproBaudRate = SetPortBaudRate(_uniproPort.BaudRate);
         _uniproPort.BaudRate = c.uniproBaudRate;

         if (c.uniproParity == "") c.uniproParity = SetPortParity(_uniproPort.Parity);
         _uniproPort.Parity = (Parity)Enum.Parse(typeof(Parity), c.uniproParity);

         if (c.uniproDataBits == -1) c.uniproDataBits = SetPortDataBits(_uniproPort.DataBits);
         _uniproPort.DataBits = c.uniproDataBits;

         if (c.uniproStopBits == "") c.uniproStopBits = SetPortStopBits(_uniproPort.StopBits);
         _uniproPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits),c.uniproStopBits);

         if (c.uniproHandshake == "") c.uniproHandshake = SetPortHandshake(_uniproPort.Handshake);
         _uniproPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), c.uniproHandshake);

         if (c.uniproPressure == -1) c.uniproPressure = SetPressure();

         if (c.uniproDensity == -1) c.uniproDensity = SetDensity();

         if (c.uniproRate == -1) c.uniproRate = SetRate();

         if (c.uniproVolume == -1) c.uniproVolume = SetVolume();

         if (c.witsName == "") c.witsName = SetPortName(_witsPort.PortName);
         _witsPort.PortName = c.witsName;

         if (c.witsBaudRate == -1) c.witsBaudRate = SetPortBaudRate(_witsPort.BaudRate);
         _witsPort.BaudRate = c.witsBaudRate;

         if (c.witsParity == "") c.witsParity = SetPortParity(_witsPort.Parity);
         _witsPort.Parity = (Parity)Enum.Parse(typeof(Parity), c.witsParity);

         if (c.witsDataBits == -1) c.witsDataBits = SetPortDataBits(_witsPort.DataBits);
         _witsPort.DataBits = c.witsDataBits;

         if (c.witsStopBits == "") c.witsStopBits = SetPortStopBits(_witsPort.StopBits);
         _witsPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), c.witsStopBits);

         if (c.witsHandshake == "") c.witsHandshake = SetPortHandshake(_witsPort.Handshake);
         _witsPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), c.witsHandshake);

         if (c.witsHeadder == "") c.witsHeadder = SetHeadder();

         if (c.witsFotter == "") c.witsFotter = SetFotter();

         Configuration.Serialize("u2wits.xml", c);

        // Set the read/write timeouts
         _uniproPort.ReadTimeout = 500;
         _uniproPort.WriteTimeout = 500;
         _witsPort.ReadTimeout = 500;
         _witsPort.WriteTimeout = 500;

         _uniproPort.Open();
         _witsPort.Open();
         _continue = true;
         readThread.Start();

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

        while (_continue)
         {
         message = Console.ReadLine();

        if (stringComparer.Equals("quit", message))
         {
         _continue = false;
         }
         }

         readThread.Join();
         _uniproPort.Close();
    }
开发者ID:espen77,项目名称:u2wits,代码行数:92,代码来源:Program.cs

示例15: BUT_getcurrent_Click


//.........这里部分代码省略.........
                string[] items = answer.Split('\n');

                foreach (string item in items)
                {
                    if (item.StartsWith("S"))
                    {
                        string[] values = item.Split(':', '=');

                        if (values.Length == 3)
                        {
                            Control[] controls = this.Controls.Find(values[0].Trim(), true);

                            if (controls.Length > 0)
                            {
                                controls[0].Enabled = true;

                                if (controls[0].GetType() == typeof(CheckBox))
                                {
                                    ((CheckBox)controls[0]).Checked = values[2].Trim() == "1";
                                }
                                else
                                {
                                    controls[0].Text = values[2].Trim();
                                }
                            }
                        }
                    }
                }

                // remote
                foreach (Control ctl in groupBox2.Controls)
                {
                    if (ctl.Name.StartsWith("RS") && ctl.Name != "RSSI")
                        ctl.ResetText();
                }

                comPort.DiscardInBuffer();

                lbl_status.Text = "Doing Command RTI5";

                answer = doCommand(comPort, "RTI5");

                items = answer.Split('\n');

                foreach (string item in items)
                {
                    if (item.StartsWith("S"))
                    {
                        string[] values = item.Split(':', '=');

                        if (values.Length == 3)
                        {
                            Control[] controls = this.Controls.Find("R" + values[0].Trim(), true);

                            if (controls.Length == 0)
                                continue;

                            controls[0].Enabled = true;

                            if (controls[0].GetType() == typeof(CheckBox))
                            {
                                ((CheckBox)controls[0]).Checked = values[2].Trim() == "1";
                            }
                            else if (controls[0].GetType() == typeof(TextBox))
                            {
                                ((TextBox)controls[0]).Text = values[2].Trim();
                            }
                            else if (controls[0].GetType() == typeof(ComboBox))
                            {
                                ((ComboBox)controls[0]).Text = values[2].Trim();
                            }
                        }
                        else
                        {
                            log.Info("Odd config line :" + item);
                        }
                    }
                }

                // off hook
                doCommand(comPort, "ATO");

                lbl_status.Text = "Done";
            }
            else
            {

                // off hook
                doCommand(comPort, "ATO");

                lbl_status.Text = "Fail";
                CustomMessageBox.Show("Failed to enter command mode");
            }

            comPort.Close();

            BUT_Syncoptions.Enabled = true;

            BUT_savesettings.Enabled = true;
        }
开发者ID:rezatoops,项目名称:MissionPlanner,代码行数:101,代码来源:3DRradio.cs


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