0

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;
        }
    }
}
4

1 に答える 1

1

数値のアップ/ダウンをComboBox、フォームが開いたとき、または更新ボタンがクリックされたとき (またはタイマーなどの他の自動手段によって) に入力される、またはその他のリスト コントロール/メニューに実際に置き換える必要があります。

ComboBoxorは次のメソッドListを使用して埋めることができますGetPortNames:-

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

説明したことを実行したい場合は、同様のコードを使用して使用を試みる前に、ポートを検索して存在するかどうかを確認できます。

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

GetPortNames()次のようなリストを返すことに注意してください:-

 COM1
 COM10
 COM12
于 2012-09-19T16:44:09.957 に答える