私はシリアルポートのプロジェクトをやっています..
コンボ ボックスから使用可能な COM ポートを選択する必要があります。
しかし、私はそれを得ることができません..そして、私はいくつかの助けが必要でした〜
これが私のコードです..そのC#です:
btn_Open はシリアルポートを有効にするボタンです
private void btnOpen_Click(object sender, EventArgs e)
{
string [] myPort;
int COM1 = cbCommPorts.SelectedIndex;
int COM2 = cbCommPorts.SelectedIndex;
int COM3 = cbCommPorts.SelectedIndex;
Object selectedItem = serialPort1.PortName;
myPort = System.IO.Ports.SerialPort.GetPortNames();
cbCommPorts.Items.AddRange(myPort);
serialPort1.PortName = cbCommPorts.SelectedItem.ToString();
serialPort1.BaudRate = 115200;
if (serialPort1.IsOpen) {
serialPort1.PortName = cbCommPorts.SelectedItem.ToString();
serialPort1.Open();
btnTransmit.Enabled = true;
btn2.Enabled = true;
btn3.Enabled = true;
}
cbCommPorts はコンボ ボックスの名前です
private void cbCommPorts_SelectedIndexChanged (オブジェクト送信者、EventArgs e) { int COM1 = cbCommPorts.SelectedIndex; int COM2 = cbCommPorts.SelectedIndex; int COM3 = cbCommPorts.SelectedIndex; オブジェクト selectedItem = serialPort1.PortName;
MessageBox.Show("COM PORT: " + selectedItem.ToString() + " Selected"); }
コードに問題はありますか? ありがとう..