现有一wpf程序,通过MPI通讯协议 连接西门子PLC300
连接成功后 开启2个线程监听两个M地址的值是否为true,如果为true则向本地数据库插入数据,
监听时间M1地址2秒,M2地址5秒
连接PLC成功后
                    isThreadRunFZ = true;  
                    threadFZ = new Thread(ThreadReadServerFZ);
                    threadFZ.IsBackground = true;
                    threadFZ.Start();                    isThreadRunZL = true;
                    threadZL = new Thread(ThreadReadServerZL);
                    threadZL.IsBackground = true;
                    threadZL.Start();======================================================
private void ThreadReadServerZL()
        {
            while (isThreadRunZL)
            {
                Thread.Sleep(timeSleepZL);//2秒
                try
                {
                    bool saveType = false;
                    DCProdave.GetMBitData(150, 0, ref saveType);
                    if (saveType)//如果M地址值为true
                    {
                        Dispatcher.Invoke(new Action<short>(AddDataCurve), (short)1);//执行数据库插入方法
                    }
                }
                catch (Exception ex)
                {
                    return;
                    //MessageBox.Show("读取失败1:" + ex.Message);
                }
            }
        }private void ThreadReadServerFZ()
        {
            while (isThreadRunFZ)
            {
                Thread.Sleep(timeSleepFZ);//5秒
                try
                {
                    bool saveTypeFZ = false;
                    DCProdave.GetMBitData(42, 0, ref saveTypeFZ);
                    if (saveTypeFZ)
                    {
                        if (PitNoFZ == "")
                        {
                            访问webservice得到返回值。。
                        }
                        Dispatcher.Invoke(new Action<string>(AddDataCurveFZ), PitNoFZ);//插入数据库
                    }
                    else
                    {
                       。。
                    }
                }
                catch (Exception ex)
                {
                    //return;
                    //MessageBox.Show("读取失败2:" + ex.Message);
                }
            }
        }========================================================================一直监听,只要点了wincc上某个按钮,改变M地址值,我就向数据库插入数据。
可是运行一会就程序崩溃。。根本就不catch任何错误,直接“xxx应用程序已停止工作” 欲哭无泪啊。

解决方案 »

  1.   

    看下windows日志分析一下
      

  2.   

    日志分析是读取M地址报错,难道是读取频率太快,而MPI只支持低频率?
    Application: WpfApp1.exe
    Framework Version: v4.0.30319
    Description: The process was terminated due to an unhandled exception.
    Exception Info: System.AccessViolationException
    Stack:
       at Model_DAL_SQLHelper.DCProdave.mb_bittest(Int32, Int32, Byte ByRef)
       at Model_DAL_SQLHelper.DCProdave.GetMBitData(Int32, Int32, Boolean ByRef)
       at WpfApp1.MainWindow.ThreadReadServerZL()
       at System.Threading.ThreadHelper.ThreadStart_Context(System.Object)
       at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
       at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
       at System.Threading.ThreadHelper.ThreadStart()
      

  3.   

    at Model_DAL_SQLHelper.DCProdave.mb_bittest(Int32, Int32, Byte ByRef)//读取M地址
    at Model_DAL_SQLHelper.DCProdave.GetMBitData(Int32, Int32, Boolean ByRef)//读取M地址
    at WpfApp1.MainWindow.ThreadReadServerZL() //线程