Visual C#でシリアル送信の作成をしています。


通信設定を固定値で出来るようになったので、ComboBoxで選択してそれを設定値として
使用したいと思ったのですが、int、stringはいいのですが、Parity、StopBitsが
それぞれSystem.IO.Port.Parityの列挙体、System.IO.Port.StopBitsの列挙体で、
ComboCoxにどう設定すればいいのか?また関数の引数にどう渡せばいいのか?が
判らずに困っております。

どなたか、ご教示をお願い致します。

回答の条件
  • 1人5回まで
  • 登録:
  • 終了:2013/08/08 19:37:54
※ 有料アンケート・ポイント付き質問機能は2023年2月28日に終了しました。

ベストアンサー

id:freemann No.3

回答回数335ベストアンサー獲得回数55

ポイント70pt

以下のやり方ならある程度自動化できるかと思います。


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.Ports;

namespace _1375953045
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (string a in Enum.GetNames(typeof(Parity)))
            {
                this.comboBox1.Items.Add(a);

            }
            foreach (string a in Enum.GetNames(typeof(StopBits)))
            {
                this.comboBox2.Items.Add(a);

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Parity p = (Parity)Enum.Parse(typeof(Parity), this.comboBox1.SelectedItem.ToString());
            StopBits s = (StopBits)Enum.Parse(typeof(StopBits), this.comboBox2.SelectedItem.ToString());

            this.label1.Text = p.ToString();
            this.label2.Text = s.ToString();
        }
    }
}

id:gontakun_55

ご回答ありがとうございます。
これです、これです!
出来ました!
ありがとうございます。

2013/08/08 18:55:16

その他の回答2件)

id:windofjuly No.1

回答回数2625ベストアンサー獲得回数1149

ポイント20pt

手入力は無いのでListBoxやRadio Buttonsのほうがいいようにも思うけど…。

それはさておき…、
ComboBoxではStart、Stopそれぞれどれか一つを選ばせるだけにして、
送信開始ボタンが押された時の処理で、
選ばれたものにあわせてパリティセットする形にします。

・StartBits は Even、Mark、None、Odd、Space のいずれかを選択させる。
・StopBitsは None、One、Two、OnePointFive のいずれかを選択させる。

MSDNの関連ページも参考に。
http://msdn.microsoft.com/ja-jp/library/system.io.ports.parity%28v=vs.90%29.aspx
http://msdn.microsoft.com/ja-jp/library/system.io.ports.stopbits%28v=vs.90%29.aspx

id:gontakun_55

回答ありがとうございます。
確かに、手入力が無いならListBoxの方がいいですね。
ご指摘ありがとうございます。

2013/08/08 18:53:24
id:a-kuma3 No.2

回答回数4973ベストアンサー獲得回数2154

ポイント10pt

コンボボックスに割り振る値と、列挙体のメンバーを対応付けた switch ~ case で設定するしかないんじゃないでしょうか。

    switch (...) {
        case 1:
            serialPort1.Parity = System.IO.Ports.Parity.None;
            break;
        case 2:
            serialPort1.Parity = System.IO.Ports.Parity.Even;
            break;
        ...
    }

また関数の引数にどう渡せばいいのか?が
判らずに困っております。

前の質問にあった serialPort1 って SerialPort コントロールですよね?
Open メソッドを呼ぶ前に、プロパティにセットしてあげます。
こんなのが落ちてたんで、参考まで。
http://homepage2.nifty.com/nonnon/SoftSample/CS.NET/SampleRs232c.html

    private void Form1_Load(
        object sender, EventArgs e)
    {
        // シリアルポートのオープン
        serialPort1.PortName = "COM11";
        // シリアルポートの通信速度指定
        serialPort1.BaudRate = 9600;
        // シリアルポートのパリティ指定
        serialPort1.Parity = System.IO.Ports.Parity.None;
        // シリアルポートのビット数指定
        serialPort1.DataBits = 8;
        // シリアルポートのストップビット指定
        serialPort1.StopBits = System.IO.Ports.StopBits.One;
        // シリアルポートのオープン
        serialPort1.Open();
    }
他3件のコメントを見る
id:windofjuly

横から失礼。

ご回答の前半にあるswitch構文で入力値にあわせた設定変更を行っていますので固定ではないですよ。

2013/08/08 19:09:22
id:gontakun_55

あ、確かにそうですね。
勘違いしておりました。
ご指摘ありがとうございます。

2013/08/08 19:10:57
id:freemann No.3

回答回数335ベストアンサー獲得回数55ここでベストアンサー

ポイント70pt

以下のやり方ならある程度自動化できるかと思います。


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.Ports;

namespace _1375953045
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (string a in Enum.GetNames(typeof(Parity)))
            {
                this.comboBox1.Items.Add(a);

            }
            foreach (string a in Enum.GetNames(typeof(StopBits)))
            {
                this.comboBox2.Items.Add(a);

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Parity p = (Parity)Enum.Parse(typeof(Parity), this.comboBox1.SelectedItem.ToString());
            StopBits s = (StopBits)Enum.Parse(typeof(StopBits), this.comboBox2.SelectedItem.ToString());

            this.label1.Text = p.ToString();
            this.label2.Text = s.ToString();
        }
    }
}

id:gontakun_55

ご回答ありがとうございます。
これです、これです!
出来ました!
ありがとうございます。

2013/08/08 18:55:16

コメントはまだありません

この質問への反応(ブックマークコメント)

「あの人に答えてほしい」「この質問はあの人が答えられそう」というときに、回答リクエストを送ってみてましょう。

これ以上回答リクエストを送信することはできません。制限について

回答リクエストを送信したユーザーはいません