找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2627|回復(fù): 9
打印 上一主題 下一主題
收起左側(cè)

C#上位機(jī)程序 索引超出數(shù)組界限?

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:1023010 發(fā)表于 2022-5-9 15:47 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
50黑幣

運(yùn)行上位機(jī),十秒不到就崩,不知道怎么解決?求助

原碼如下:

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 上位機(jī)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;               //數(shù)據(jù)位
                    serialPort1.Parity = System.IO.Ports.Parity.None;
                    serialPort1.StopBits = System.IO.Ports.StopBits.One;
                    serialPort1.Open();     //打開串口
                    button1.Text = "關(guān)閉串口";

                }
            }
            catch
            {
                serialPort1 = new System.IO.Ports.SerialPort();
                //刷新COM口選項(xiàng)
                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('/');
                    //下位機(jī)數(shù)據(jù)發(fā)送格式
                    //將數(shù)據(jù)顯示
                    // textBox1.Text = info[1];
                    //textBox2.Text = info[2];
                    if (str == "2")
                    {
                        ListViewItem item = new ListViewItem();      //先實(shí)例化ListViewItem這個(gè)類
                        listCount++;
                        string s1 = listCount.ToString();
                        item.Text = s1;                 //序號(hào)

                        //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";//光強(qiáng)              
                    }
                    if (str == "1")
                    {
                        ListViewItem item = new ListViewItem();      //先實(shí)例化ListViewItem這個(gè)類
                        listCount++;
                        string s1 = listCount.ToString();
                        item.Text = s1;                 //序號(hào)

                        //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('/');
                    //下位機(jī)數(shù)據(jù)發(fā)送格式
                    //將數(shù)據(jù)顯示
                    // textBox1.Text = info[1];
                    //textBox2.Text = info[2];
                    if (str == "2")
                    {
                        ListViewItem item = new ListViewItem();      //先實(shí)例化ListViewItem這個(gè)類
                        listCount1++;
                        string s1 = listCount.ToString();
                        item.Text = s1;                 //序號(hào)

                        //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";//光強(qiáng)
                        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)
        {

        }
    }
}

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:277550 發(fā)表于 2022-5-9 17:21 | 只看該作者
就是收到的數(shù)據(jù)沒那么多,或者分多次(分段)上傳了
回復(fù)

使用道具 舉報(bào)

板凳
ID:1023010 發(fā)表于 2022-5-10 19:23 | 只看該作者
devcang 發(fā)表于 2022-5-9 17:21
就是收到的數(shù)據(jù)沒那么多,或者分多次(分段)上傳了

那具體應(yīng)該怎么修改?
回復(fù)

使用道具 舉報(bào)

地板
ID:1023010 發(fā)表于 2022-5-10 19:54 | 只看該作者
大佬們,代碼應(yīng)該怎么修改呀?
回復(fù)

使用道具 舉報(bào)

5#
ID:883242 發(fā)表于 2022-5-10 21:17 | 只看該作者
增加item.SubItems的數(shù)量。
回復(fù)

使用道具 舉報(bào)

6#
ID:1023010 發(fā)表于 2022-5-13 20:07 來自手機(jī) | 只看該作者
最后是用if(info.length>=7){}將傳感器代碼括起來解決問題的
回復(fù)

使用道具 舉報(bào)

7#
ID:203661 發(fā)表于 2022-5-14 11:56 | 只看該作者
把工程傳上來看看
回復(fù)

使用道具 舉報(bào)

8#
ID:1026496 發(fā)表于 2022-5-14 17:18 | 只看該作者
                    string[] info = data.Split('/'); 這個(gè)里面的' ' 改成雙引號(hào)
回復(fù)

使用道具 舉報(bào)

9#
ID:1023010 發(fā)表于 2022-5-17 09:53 | 只看該作者
下位機(jī)程序

室內(nèi)環(huán)境監(jiān)測(cè)下位機(jī).7z

4.79 MB, 下載次數(shù): 7

回復(fù)

使用道具 舉報(bào)

10#
ID:1023010 發(fā)表于 2022-5-17 09:56 | 只看該作者
yaoyao2022 發(fā)表于 2022-5-14 17:18
string[] info = data.Split('/'); 這個(gè)里面的' ' 改成雙引號(hào)

這樣會(huì)出錯(cuò)
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表