c#写一个上位机程序,用串口助手调试不出结果

图片说明图片说明

我用了论坛里搜到的代码,但是不全

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
        }

        private string[] portNames = null;
        private List<SerialPort> serialPorts;
        private byte[] portBuffer;

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.serialPorts = new List<SerialPort>();
            this.portNames = SerialPort.GetPortNames();
            this.portBuffer = new byte[1000];
            //Assign the value of port names to serial ports display
            if (this.portNames.Length > 1)
            {
                for (int i = 0; i < this.portNames.Length; i++)
                {
                    this.serialPorts.Add(new SerialPort(this.portNames[i]));
                }
            }
            this.comboBox1.Items.AddRange(this.portNames);
            this.comboBox2.Items.AddRange(this.portNames);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.portNames.Length; i++)
            {
                try
                {
                    if (this.portNames.ElementAt(i) == this.comboBox1.Text.ToString())
                    {
                        if (!this.serialPorts.ElementAt(i).IsOpen)
                        {
                            this.serialPorts.ElementAt(i).Open();
                            MessageBox.Show("已打开串口!");
                        }
                        //this.serialPorts.ElementAt(i).
                    }
                }
                catch (IOException eio)
                {
                    MessageBox.Show("打开串口异常:" + eio);
                }

            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.portNames.Length; i++)
            {
                try
                {
                    if (this.portNames.ElementAt(i) == this.comboBox1.Text.ToString())
                    {
                        if (this.serialPorts.ElementAt(i).IsOpen)
                        {
                            this.serialPorts.ElementAt(i).Close();
                            MessageBox.Show("已关闭串口!");
                        }

                    }
                }
                catch (IOException eio)
                {
                    MessageBox.Show("关闭串口异常:" + eio);
                }
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < this.portNames.Length; i++)
            {
                try
                {
                    if (this.portNames.ElementAt(i) == this.comboBox1.Text.ToString())
                    {
                        if (this.serialPorts.ElementAt(i).IsOpen)
                        {
                            string sendContent = this.textBox1.Text.ToString();
                            this.serialPorts.ElementAt(i).Write(sendContent);
                            MessageBox.Show("已发送数据!");
                        }

                    }
                }
                catch (IOException eio)
                {
                    MessageBox.Show("串口发送异常:" + eio);
                }
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.AddEventHandlerForResponse();
        }
        private void AddEventHandlerForResponse()
        {
            for (int i = 0; i < this.portNames.Length; i++)
            {
                try
                {
                    if (this.portNames.ElementAt(i) == this.comboBox2.Text.ToString())
                    {
                        if ((this.serialPorts.ElementAt(i).IsOpen) && (this.serialPorts.ElementAt(i).BytesToRead > 0))
                        {
                            this.serialPorts.ElementAt(i).Read(this.portBuffer, 0, this.serialPorts.ElementAt(i).BytesToRead);
                            this.serialPorts.ElementAt(i).DataReceived += new SerialDataReceivedEventHandler(this.DataReceiveEventHandler);
                        }

                    }
                }
                catch (IOException eio)
                {
                    MessageBox.Show("串口异常:" + eio);
                }
            }

        }

        private void DataReceiveEventHandler(object sender, SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            this.textBox2.Invoke(
                new MethodInvoker(
                    delegate
                    {
                        this.textBox2.AppendText(sp.ReadExisting());
                        this.textBox2.Text += " ";

                    }
                                )
                                    );
        }

        private void button4_Click(object sender, EventArgs e)
        {

        }
    }
}

运行出来的结果,根本接收不到,求解哪里错了啊

查看全部
Mag_Tu
美腻乖巧的晓解结
2019/04/22 15:19
  • c#
  • 点赞
  • 收藏
  • 回答
    私信

0个回复