小羊i 2019-04-22 15:19
浏览 453

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)
        {

        }
    }
}

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

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划