简体   繁体   English

多 COM 端口的 C# 串行通信

[英]C# Serial Communication for Multi COM Ports

I want to communicate two devices with my computer using RS232 communication.我想使用 RS232 通信将两个设备与我的计算机通信。 I want to send hex commands (0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D) from my computer to devices.我想从我的计算机向设备发送十六进制命令(0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D)。 I have 2 buttons on my interface.我的界面上有 2 个按钮。 When I press the first button, I will send a code to the first device, and when I press the second button, I will send a code to the second device.当我按下第一个按钮时,我会向第一个设备发送一个代码,当我按下第二个按钮时,我会向第二个设备发送一个代码。

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();
        }
    }
}

How can I send the command (same or different) for two different COM (COM1 and COM2) ports?如何为两个不同的 COM(COM1 和 COM2)端口发送命令(相同或不同)?

How can I send the same command to different com ports with a single button?如何使用单个按钮将相同的命令发送到不同的 COM 端口?

If you think simply, you should do the following.如果您简单地考虑,您应该执行以下操作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM