簡體   English   中英

Arduino的C#串行端口通信

[英]C# serialport communication for Arduino

我用C#為Arduino開發了一個小應用程序。 一切正常,但問題出在我的應用程序中。 用戶可以在numericUpDown中選擇他/她的COM端口。

它可以工作,但是如果用戶選擇了錯誤的端口並嘗試連接,則會崩潰,因此我以為我需要提供消息的IF statmant。 例如,錯誤的端口等,但是我不知道該怎么做。 我該怎么做?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.IO.Ports;

namespace IO_Arduino_experiment_project
{
    public partial class Form1 : Form
    {
        public static System.IO.Ports.SerialPort serialPort1;
        private delegate void LineReceivedEvent(string line);
        public Form1()
        {
            InitializeComponent();
            button5.Enabled = false;
            button2.Enabled = false;
            button3.Enabled = false;
        }

        private void button1_Click(object sender, EventArgs e) // Connect Button
        {
            System.ComponentModel.IContainer components = new System.ComponentModel.Container();
            serialPort1 = new System.IO.Ports.SerialPort(components); // Creating the new object.
            serialPort1.PortName = "COM" + numericUpDown1.Value.ToString(); // Setting what port number.
            serialPort1.BaudRate = 9600;
            serialPort1.DtrEnable = true;
            serialPort1.Open(); // Open the port for use.
            button1.Text = "Connected.";
            button1.Enabled = false;
            numericUpDown1.Enabled = false;
            button5.Enabled = true;
            button2.Enabled = true;
            button3.Enabled = true;
        }

        private void numericUpDown1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (serialPort1.IsOpen) serialPort1.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Write("1");
            textBox1.Text = "LED is on!";
            button2.Enabled = false;
            button3.Enabled = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            serialPort1.Write("0");
            textBox1.Text = "LED is off!";
            button2.Enabled = true;
            button3.Enabled = false;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            button1.Enabled = true;
            numericUpDown1.Enabled = true;
        }
    }
}

您應該真正用ComboBox或在列表打開時或單擊刷新按鈕時(或通過其他自動方式,例如計時器)填充的其他列表控件/菜單替換上/下數字。

可以使用GetPortNames方法填充ComboBoxList

String[] pNames;
pNames = System.IO.Ports.SerialPort.GetPortNames();

如果您要執行所描述的操作,則可以使用類似的代碼查找端口,以在嘗試使用之前查看該端口是否存在:

if (System.IO.Ports.SerialPort.GetPortNames().indexOf("COM" +  thePortNum.ToString())>-1)

請注意, GetPortNames()返回如下列表:

 COM1
 COM10
 COM12

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM