找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1396|回复: 9
收起左侧

C#上位机程序 索引超出数组界限?

[复制链接]
ID:1023010 发表于 2022-5-9 15:47 | 显示全部楼层 |阅读模式
50黑币
51hei图片20220509155053.png
运行上位机,十秒不到就崩,不知道怎么解决?求助

原码如下:

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

namespace 上位机demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Thread th = new Thread(Serial);
            th.IsBackground = true;
            th.Start();
            Control.CheckForIllegalCrossThreadCalls = false;
            System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
            string s = " ";
            textBox1.Text = s;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                    button1.Text = "打开串口";

                }
                else
                {
                    serialPort1.PortName = comboBox1.Text;
                    serialPort1.BaudRate = 115200;          //波特率
                    serialPort1.DataBits = 8;               //数据位
                    serialPort1.Parity = System.IO.Ports.Parity.None;
                    serialPort1.StopBits = System.IO.Ports.StopBits.One;
                    serialPort1.Open();     //打开串口
                    button1.Text = "关闭串口";

                }
            }
            catch
            {
                serialPort1 = new System.IO.Ports.SerialPort();
                //刷新COM口选项
                comboBox1.Items.Clear();
                comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
                //响铃并显示异常给用户
                System.Media.SystemSounds.Beep.Play();
                button1.Text = "打开串口";
            }
        }
        //串口扫描
        void Serial(object o)
        {
            Control.CheckForIllegalCrossThreadCalls = false;
            if (comboBox1.Text == "")
            {
                comboBox1.Items.AddRange(System.IO.Ports.SerialPort.GetPortNames());
            }
        }
        int listCount = 0;

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                string str;
                string data = string.Empty;

                this.Invoke((EventHandler)(delegate
                {

                    data = serialPort1.ReadExisting();
                    str = data.Substring(0, 1);
                    //  1/10/20/30/40/1/1/1
                    string[] info = data.Split('/');
                    //下位机数据发送格式
                    //将数据显示
                    // textBox1.Text = info[1];
                    //textBox2.Text = info[2];
                    if (str == "2")
                    {
                        ListViewItem item = new ListViewItem();      //先实例化ListViewItem这个类
                        listCount++;
                        string s1 = listCount.ToString();
                        item.Text = s1;                 //序号

                        //item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[1]) + Convert.ToInt32(info[3])) / 2));     //温度1
                        //item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[2]) + Convert.ToInt32(info[4])) / 2));     //温度1
                        item.SubItems.Add(info[5]);     //烟雾浓度
                        item.SubItems.Add(info[6]);     //存在火焰

                        int cmd = Convert.ToInt32(info[1]);
                        if (cmd != 0)
                        {
                            textBox1.Text = info[1] + "℃";//温度1
                            textBox2.Text = info[2] + "%RH";//湿度1
                        }
                        textBox3.Text = info[3] + "lx";//光强              
                    }
                    if (str == "1")
                    {
                        ListViewItem item = new ListViewItem();      //先实例化ListViewItem这个类
                        listCount++;
                        string s1 = listCount.ToString();
                        item.Text = s1;                 //序号

                        //item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[1]) + Convert.ToInt32(info[3])) / 2));     //温度1
                        //item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[2]) + Convert.ToInt32(info[4])) / 2));     //温度1
                        item.SubItems.Add(info[5]);     //烟雾浓度
                        item.SubItems.Add(info[6]);     //存在火焰

                        textBox4.Text = info[4] + "%";//烟雾浓度

                        int cmf = Convert.ToInt32(info[2]);
                        if (cmf < 10000)
                        {
                            textBox5.Text = info[5] + "μg/m3";//PM2.5} }


                        }
                    }
                }
                   )
                );
            }
            catch (Exception ex)
            {
                //响铃并显示异常给用户
                System.Media.SystemSounds.Beep.Play();
                MessageBox.Show(ex.Message);

            }
        }


        int listCount1 = 0;

        private void serialPort2_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                string str;
                string data = string.Empty;

                this.Invoke((EventHandler)(delegate
                {

                    data = serialPort1.ReadExisting();
                    str = data.Substring(0, 1);
                    //  1/10/20/30/40/1/1/1
                    string[] info = data.Split('/');
                    //下位机数据发送格式
                    //将数据显示
                    // textBox1.Text = info[1];
                    //textBox2.Text = info[2];
                    if (str == "2")
                    {
                        ListViewItem item = new ListViewItem();      //先实例化ListViewItem这个类
                        listCount1++;
                        string s1 = listCount.ToString();
                        item.Text = s1;                 //序号

                        //item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[1]) + Convert.ToInt32(info[3])) / 2));     //温度1
                        //item.SubItems.Add(Convert.ToString((Convert.ToInt32(info[2]) + Convert.ToInt32(info[4])) / 2));     //温度1
                        item.SubItems.Add(info[6]);     //烟雾浓度
                        item.SubItems.Add(info[7]);     //存在火焰

                        textBox1.Text = info[1] + "℃";//温度1
                        textBox2.Text = info[2] + "%RH";//湿度1
                                                        //textBox6.Text = info[3] + "℃";//温度2
                                                        // textBox7.Text = info[4] + "%RH";//湿度2
                        textBox3.Text = info[3] + "lx";//光强
                        textBox4.Text = info[4] + "%";//烟雾浓度
                        textBox5.Text = info[5] + "μg/m3";//PM2.5
                    }
                }
                   )
                );
            }
            catch (Exception ex)
            {
                //响铃并显示异常给用户
                System.Media.SystemSounds.Beep.Play();
                MessageBox.Show(ex.Message);

            }
        }


        private void textBox6_TextChanged_1(object sender, EventArgs e)
        {

        }
      



        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void textBox6_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter_1(object sender, EventArgs e)
        {

        }

      
        private void label14_Click(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

回复

使用道具 举报

ID:277550 发表于 2022-5-9 17:21 | 显示全部楼层
就是收到的数据没那么多,或者分多次(分段)上传了
回复

使用道具 举报

ID:1023010 发表于 2022-5-10 19:23 | 显示全部楼层
devcang 发表于 2022-5-9 17:21
就是收到的数据没那么多,或者分多次(分段)上传了

那具体应该怎么修改?
回复

使用道具 举报

ID:1023010 发表于 2022-5-10 19:54 | 显示全部楼层
大佬们,代码应该怎么修改呀?
回复

使用道具 举报

ID:883242 发表于 2022-5-10 21:17 | 显示全部楼层
增加item.SubItems的数量。
回复

使用道具 举报

ID:1023010 发表于 2022-5-13 20:07 来自手机 | 显示全部楼层
最后是用if(info.length>=7){}将传感器代码括起来解决问题的
回复

使用道具 举报

ID:203661 发表于 2022-5-14 11:56 | 显示全部楼层
把工程传上来看看
回复

使用道具 举报

ID:1026496 发表于 2022-5-14 17:18 | 显示全部楼层
                    string[] info = data.Split('/'); 这个里面的' ' 改成双引号
回复

使用道具 举报

ID:1023010 发表于 2022-5-17 09:53 | 显示全部楼层
下位机程序

室内环境监测下位机.7z

4.79 MB, 下载次数: 7

回复

使用道具 举报

ID:1023010 发表于 2022-5-17 09:56 | 显示全部楼层
yaoyao2022 发表于 2022-5-14 17:18
string[] info = data.Split('/'); 这个里面的' ' 改成双引号

这样会出错
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表