多 COM 端口的 C# 串行通信

C# Serial Communication for Multi COM Ports

我想使用 RS232 通讯方式将两台设备与我的电脑进行通讯。我想从我的计算机向设备发送十六进制命令 (0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D)。我的界面上有 2 个按钮。当我按下第一个按钮时,我会向第一台设备发送代码,当我按下第二个按钮时,我会向第二台设备发送代码。

System

Code

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

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

        private void button1_Click(object sender, EventArgs e)
        {
            SerialPort sp = new SerialPort(); 
            sp.PortName = "COM1"; 
            sp.BaudRate = 19200;  
            sp.DataBits = 8; 
            sp.Parity = Parity.None; 
            sp.StopBits = StopBits.One;
            //SerialPort sp = new SerialPort("COM1",9600,Parity.None,8,StopBits.One);
            sp.Open();
            sp.Write(new byte[] { 0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5D }, 0, 10); //cmd = 0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D
            sp.Close();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            SerialPort sp = new SerialPort();
            sp.PortName = "COM2";
            sp.BaudRate = 19200;
            sp.DataBits = 8;
            sp.Parity = Parity.None;
            sp.StopBits = StopBits.One;
            //SerialPort sp = new SerialPort("COM2",9600,Parity.None,8,StopBits.One);
            sp.Open();
            sp.Write(new byte[] { 0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5E }, 0, 10); //cmd = 0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5E
            sp.Close();
        }
    }
}

如何为两个不同的 COM(COM1 和 COM2)端口发送命令(相同或不同)?

如何通过一个按钮将相同的命令发送到不同的 com 端口?

如果你想的简单,你应该做下面的事情。

    public partial class Form1 : Form
    {
        SerialPort sp1 = new SerialPort();
        SerialPort sp2 = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }

        private void OpenCOM_Click(object sender, EventArgs e)
        {
            if (!sp1.IsOpen)
            {
                sp1.PortName = "COM1";
                sp1.BaudRate = 19200;
                sp1.DataBits = 8;
                sp1.Parity = Parity.None;
                sp1.StopBits = StopBits.One;
                sp1.Open();
            }
            if (!sp2.IsOpen)
            {
                sp2.PortName = "COM2";
                sp2.BaudRate = 19200;
                sp2.DataBits = 8;
                sp2.Parity = Parity.None;
                sp2.StopBits = StopBits.One;
                sp2.Open();
            }
        }

        private void CloseCOM_Click(object sender, EventArgs e)
        {
            if (sp1.IsOpen)
            {
                sp1.Close();
            }
            if (sp2.IsOpen)
            {
                sp2.Close();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            sp1.Write(new byte[] { 0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5D }, 0, 10); //cmd = 0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D
        }

        private void button2_Click(object sender, EventArgs e)
        {
            sp2.Write(new byte[] { 0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5E }, 0, 10); //cmd = 0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5E
        }

        private void button3_Click(object sender, EventArgs e)
        {
            // same
            sp1.Write(new byte[] { 0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5D }, 0, 10); //cmd = 0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D
            sp2.Write(new byte[] { 0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5D }, 0, 10); //cmd = 0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // different
            sp1.Write(new byte[] { 0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5D }, 0, 10); //cmd = 0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D
            sp2.Write(new byte[] { 0x06, 0x14, 0x00, 0x04, 0x00, 0x34, 0x11, 0x00, 0x00, 0x5E }, 0, 10); //cmd = 0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5E
        }
    }