检查一下你的代码.特别是timer的Elapsed 事件的部分

解决方案 »

  1.   

    把主要代码贴上来就是与timer和Elapsed
      

  2.   

    我觉得没必要自己写RTU的modbus驱动,我都是先用OPC,比如:Kepware之类的先采集到OPC中,然后用OPC Client采集数据。
    如果实在要写的话,我觉得如楼上的兄弟们说的主要问题还是在于Timer问题,我想如果是使用timer,问题可能还在于elapsed和触发的时机和次数。
    对于timer事件,系统是并发执行的,也就是说:如果你的时间间隔过短,就有可能存在着由一个timer触发的多个elapsed在不同线程中执行的情况,可能你没有考虑这种情况下的线程同步问题。
      

  3.   

    我在Timer触发之后就 就把TImer停止,待处理线程结束之后才重新启动Timer。
    我也考虑过利用OPC,但还涉及到流量计算等等 最后还是决定自己写。
     void tm_Elapsed(object sender, ElapsedEventArgs e)
            {
                //throw new Exception("The method or operation is not implemented.");
                tm.Stop();
                tm.Enabled = false;
                DataCollect();   
            }
    DataCollect()里边会启动一个访问RTU的线程 ,在这个线程结束之后,才会启动该Timer。
     public void DataCollect()
            {
                try
                {
                    control = true;
                    if (client == null)
                    {
                        client = new TcpClient();
                        try
                        {
                            client.Connect(m_HostIP, m_PortNo);
                            m_msg.SendMsg("[" + DateTime.Now.ToString() + "] 与服务器[" + m_HostIP + "] 建立连接");
                            UpdateStationState(m_StationIndex, true);
                            LogToTxt("[" + DateTime.Now.ToString() + "] 与服务器[" + m_HostIP + "] 建立连接");
                        }
                        catch (Exception ex)
                        {
                            m_msg.SendMsg("[" + DateTime.Now.ToString() + "] 与服务器[" + m_HostIP + "]无法建立连接 " + ex.Message);
                            LogToTxt("[" + DateTime.Now.ToString() + "] 与服务器[" + m_HostIP + "]无法建立连接");
                            UpdateStationState(m_StationIndex, false );
                            tm.Enabled = true;
                            tm.Start();
                            return;
                        }
                    }
                    if (client.Connected == false)
                    {
                        try
                        {
                            client.Close();
                            if (netstream != null)
                            {
                                netstream.Close();
                            }
                            client = null;
                            client = new TcpClient();
                            client.Connect(m_HostIP, m_PortNo);
                            m_msg.SendMsg("[" + DateTime.Now.ToString() + "] 与服务器[" + m_HostIP + "] 建立连接");
                            UpdateStationState(m_StationIndex, true);
                            LogToTxt("[" + DateTime.Now.ToString() + "] 与服务器[" + m_HostIP + "] 建立连接");
                        }
                        catch (Exception ex)
                        {
                            m_msg.SendMsg("[" + DateTime.Now.ToString() + "] 与服务器[" + m_HostIP + "]无法建立连接 "+ex.Message );
                            LogToTxt("[" + DateTime.Now.ToString() + "] 与服务器[" + m_HostIP + "]无法建立连接");
                            UpdateStationState(m_StationIndex, false );
                            tm.Enabled = true;
                            tm.Start();
                            return;
                        }
                    }                Thread thread1 = new Thread(new ThreadStart(DataReceive));
                    thread1.Start();
                   // DataReceive();
                }
                catch (Exception ee)
                {
                    m_msg.SendMsg("[" + DateTime.Now.ToString() + "] 无法建立连接," + ee.Message);
                    LogToTxt("[" + DateTime.Now.ToString() + "] 无法建立连接," + ee.Message);
                    UpdateStationState(m_StationIndex, false );
                }
            }
    private void DataReceive()        
    {
    try
    {
    }
    catch()
    {
    }
    finally
    {
         if (tm.Enabled == false)
         {
              tm.Stop();
              tm.Enabled = true;
              tm.Start();
          }
    }
    }
      

  4.   

    大哥 我是刚学习这个  如果你代码不是很保密 请发个给我啊   我好学习一下:[email protected]
      

  5.   

    我最近也在写ModBus的东西,我是用线程写的,在线程里打开TCP侦听,使用Sleep来循环读取数据,现在状况良好,最开始也有经常断连的情况,原因发送了命令后设备没有回应导致TCP读取超时,超时后TCP会自动断连(开始为这件事烦了很久),这种情况有可能是设备没正确识别命令或命令本身就出错,比如地址错误等设备不回复信息,我的解决办法是自己写了一个接收缓冲类,在该类里执行TCP的读取操作,读取不设置超时(解决了超时断连的情况),命令发送后程序在缓冲类里读取数据。
      

  6.   

    http://www.control.com/1026219150/index_html