简体   繁体   中英

C# Serial Communication for Multi COM Ports

I want to communicate two devices with my computer using RS232 communication. I want to send hex commands (0x06 0x14 0x00 0x04 0x00 0x34 0x11 0x00 0x00 0x5D) from my computer to devices. I have 2 buttons on my interface. 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?

How can I send the same command to different com ports with a single button?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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