Home > Net >  C # abnormal collection modified; May not be able to perform the enumeration operations
C # abnormal collection modified; May not be able to perform the enumeration operations

Time:09-28

Recent study using c # write a serial port test software, from a serial port receives the data, and the data map into a waveform, used in the practical application of control chart is (measurement studio2015 waveformgraph not useful) in the test found that chart after 1000-2000 data points in the drawing, application can appear abnormal, when I comment the add data to the chart after the function of running without exception occurs,

Abnormal remind is: "System. InvalidOperationException" type of untreated anomalies in the System. Windows. Forms. DataVisualization. DLL,

Don't know if you have good solution,


Anomaly map and the code is as follows:




The code is as follows:
 using System; 
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

Using System. The Runtime. InteropServices;
Using System. Windows. Forms. DataVisualization. Charting;
using System.IO;
Using System. IO. Ports;
Using NationalInstruments;
//using NationalInstruments. Controls. The Data;


The namespace HRM200Test
{

Public partial class Form1: Form
{
Int Timercount=0;//for the timer count
Byte [] gDataToSend=new byte [2] {0 x00 to 0 x00};//data


Public _click ()
{
InitializeComponent ();
System. Windows. Forms. Control. CheckForIllegalCrossThreadCalls=false;
//timer1. Start ();
}
Internal class flagclass
{
Public static int button1_click_flag=0;
Public static int button4_click_flag=0;
Public static int button5_click_flag=0;
Public static int PCR_Run_flag=0;
Public static int StepCnt=0;
Public static int LoopCnt=0;
Public static int TimeValue=https://bbs.csdn.net/topics/0;
Public static int Predegeneration_flag=0;
Public static int DaRxCnt=0;
Public static byte [] DataRxBuf=new byte [50].
Public static byte [] TemperByte=new byte [5].
}
Private void button1_Click (object sender, EventArgs e)
{
If (flagclass. Button1_click_flag==0)
{
Try
{
SerialPort1. PortName=comboBox1. Text;
SerialPort1. BaudRate=the Convert. ToInt32 (comboBox2. Text, 10);//decimal data conversion
SerialPort1. The Open ();
Flagclass. Button1_click_flag=1;
For the BackColor=SystemColors. ActiveCaption;
For the Text="close serial port";
}
Catch
{
MessageBox. Show (" port error, please check the serial port ", "error");
}
}
The else
{
Try
{

SerialPort1. Close ();//close a serial port
For the Text="open the serial port";
For the Enabled=true;//open the serial port button available
For the BackColor=SystemColors. InactiveBorder;
Flagclass. Button1_click_flag=0;
}
Catch (Exception)//Exception err)//usually close serial port will not go wrong, so don't need to add the handler
{

}
}
}

Private void Form1_Load (object sender, EventArgs e)
{
SearchAndAddSerialToComboBox (serialPort1 comboBox1);
ComboBox2. SelectedIndex=3;
Button4. Enabled=false;
ChartInit ();
}

Private void serialPort1_DataReceived (object sender, SerialDataReceivedEventArgs e)
{
Int LEN=50;
Int16 I=0;
Fdata=https://bbs.csdn.net/topics/new float float [] [4].
Byte [] crcvalue=https://bbs.csdn.net/topics/new byte [2] {0 x00 to 0 x00};
Byte [] buffer1=new byte (LEN);
Int [] buf1=new int [100].
LEN=0;
LEN=serialPort1. BytesToRead;
SerialPort1. Read (buffer1, 0, LEN);
If (buffer1 [0]==0 x41 & amp; & Buffer1 [1]==0 x04 & amp; & CRC16_2 (buffer1) [0]==buffer1 [18] & amp; & CRC16_2 (buffer1) [1]==buffer1 [19])
{
For (int j=0; J & lt; 4. J++)
{
Flagclass. TemperByte [3]=buffer1 [j * 4 + 2];
Flagclass. TemperByte [2]=buffer1 [j * 4 + 3];
Flagclass. TemperByte [1]=buffer1 [j * 4 + 4];
Flagclass. TemperByte [0]=buffer1 [j] * 4 + 5;
Fdata=BitConverter [j]. J ToSingle (flagclass. TemperByte, 0);
}
Try
{
Chart2. Series [0]. Points. AddY (fdata [1]).
TextBox11. Text=fdata [1]. The ToString ();
If ((chart2 ChartAreas [0]. AxisX. The Maximum - chart2. ChartAreas [0]. AxisX. Minimum) & gt;
=1024)Chart2. ChartAreas [0]. AxisX. Minimum=chart2. ChartAreas [0]. AxisX. Maximum - 1024;
}
Catch {
MessageBox. Show (" error!" , "z");
}
}
The else
{
Strings str1=System. Text. Encoding. The Default. Get string (buffer1);//buffer1. ToString ();
TextBox1. AppendText (str1);//add content
TextBox1. Text. ToArray ();
}

}
Private void SendDataToSerialPort (SerialPort MyPort, byte [] DataToSend)//single-byte send data
{
Const byte Nlen=0 x07;
Byte [] crcvalue=https://bbs.csdn.net/topics/new byte [2] {0 x00 to 0 x00};
Crcvalue [0]=CRC16 (DataToSend) [0];
Crcvalue [1]=CRC16 (DataToSend) [1];
Byte [] DatasToWrite=new byte [Nlen] {0 x54, DataToSend [0], DataToSend [1], crcvalue [0], crcvalue [1], 0 x0d, 0 x0a};//packet
If (serialPort1. IsOpen)
{
Try
{
//MyPort. WriteLine (" ");
MyPort. Write (DatasToWrite, 0, Nlen);//send data
}
Catch
{
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related