DatabindXY,集合已修改;可能无法执行枚举操作。
List<float> datalist = new List<float>();
List<float> timelist = new List<float>();
public float[][] listArray = new float[128][];
public float[][] listFFT = new float[128][];
public float[][] listShock = new float[128][];
public float[][] ideal_line = new float[128][];
public int frames = 0; //listArray中行数,偶数行代表时间数据,奇数行代表加速度数据
public int curShowFrame = 0; //显示上下帧数据时的参数
private void server_start() //上位机作为服务器,接收下位机发来的数据包
{
while (true)
{
try
{
int port = 2001; //监听2001端口
string host = "127.0.0.1"; //本机回发的Ip
IPAddress ip = IPAddress.Parse(host);
IPEndPoint ipe = new IPEndPoint(ip, port);
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//创建一个Socket类
s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
//this.label12.Text = this.label12.Text + "绑定2001端口并开始监听.....\n";
s.Bind(ipe); //绑定2001端口
s.Listen(0); //开始监听
//this.label12.Text = this.label12.Text + "等待客户发送请求......\n";
Socket temp = s.Accept(); //为新建连接创建新的Socket。
// this.label12.Text = this.label12.Text + "获得一个客户端,开始接收数据......\n";
byte[] bytes = new byte[1200];
int bytesint;
bytesint = temp.Receive(bytes, bytes.Length, 0);//从客户端接受信息
int n = 0;
Byte[] changebytes = new byte[4]; //暂存四个字节数据
for (int j = 0; j < 4 * 2 * 128; j++)
{
if (n == 4)
{
//Array.Reverse(changebytes);
if (j <= 2 * 2 * 128)
datalist.Add(BitConverter.ToSingle(changebytes, 0)); //存储加速度序列
else
timelist.Add(BitConverter.ToSingle(changebytes, 0));
n = 0;
j = j - 1;
}
else if (j == 4 * 2 * 128 - 1)
{
changebytes[n++] = bytes[j];
timelist.Add(BitConverter.ToSingle(changebytes, 0));//存储时间序列
}
else
changebytes[n++] = bytes[j];
}
frames = frames + 2;
curShowFrame = frames;
listArray[frames - 2] = new float[128];
listArray[frames - 1] = new float[128];
for (int k = 0; k < 128; k++)
{
listArray[frames - 2][k] = timelist[k]; //存入交叉数组中,偶数行为时间值,奇数行为加速度值
listArray[frames - 1][k] = datalist[k];
}
timelist.RemoveRange(0, 128); //清空数据
datalist.RemoveRange(0, 128);
#region 时域显示
if (mainPP.auto_position == "峰值匹配")
{
float tolerance_max = 0.0f;
float tolerance_min = 0.0f;
tolerance_max = Convert.ToSingle(0.2 * mainPP.amplitude);
tolerance_min = Convert.ToSingle(-0.2 * mainPP.amplitude);
ideal_line[frames-2] = new float[128];
ideal_line[frames-1] = new float[128];
for (int i = 0; i < 128; i++)
{
ideal_line[frames-2][i] = Convert.ToSingle(mainPP.amplitude * Math.Sin(i * Math.PI / 128)) + tolerance_max;
ideal_line[frames-1][i] = Convert.ToSingle(mainPP.amplitude * Math.Sin(i * Math.PI / 128)) + tolerance_min;
}
chart1.ChartAreas[0].AxisY.Minimum = -5;
chart1.Series["ShowLine"].Points.DataBindXY(listArray[frames - 2], listArray[frames - 1]);
chart1.Series["正理想脉宽"].Points.DataBindXY(listArray[frames - 2], ideal_line[frames-2]);
chart1.Series["负理想脉宽"].Points.DataBindXY(listArray[frames - 2], ideal_line[frames-1]);
chart1.Series["正理想脉宽"].IsXValueIndexed = true;
chart1.Series["负理想脉宽"].IsXValueIndexed = true;
chart1.Series["ShowLine"].IsXValueIndexed = true;
chart1.Update();
}
#endregion
string sendStr = "确认";
byte[] bs2 = Encoding.UTF8.GetBytes(sendStr);
temp.Send(bs2, bs2.Length, 0); //返回客户端成功信息
temp.Close();
s.Close();
}
catch (ArgumentNullException e0)
{
MessageBox.Show(e0.ToString());
}
catch (SocketException e2)
{
MessageBox.Show(e2.ToString());
}
}
}
目的是用mschart产生3条曲线,正理想脉宽,负理想脉宽,ShowLine。ShowLine是通过socket接收过来的数据,listArray,ideal_line通过DatabindXY绑定在chart中,第一次接收数据能显示曲线,但是第二次接收数据后却报错,错误为“集合已修改;可能无法执行枚举操作。”;该错误在单步运行时却不会发生,很莫名其妙,希望懂的人帮我解决些,万分感激!