新手求助!关于C#写Modbus TCP的问题 谢谢! 检查一下你的代码.特别是timer的Elapsed 事件的部分 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把主要代码贴上来就是与timer和Elapsed 我觉得没必要自己写RTU的modbus驱动,我都是先用OPC,比如:Kepware之类的先采集到OPC中,然后用OPC Client采集数据。如果实在要写的话,我觉得如楼上的兄弟们说的主要问题还是在于Timer问题,我想如果是使用timer,问题可能还在于elapsed和触发的时机和次数。对于timer事件,系统是并发执行的,也就是说:如果你的时间间隔过短,就有可能存在着由一个timer触发的多个elapsed在不同线程中执行的情况,可能你没有考虑这种情况下的线程同步问题。 我在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(); }}} 大哥 我是刚学习这个 如果你代码不是很保密 请发个给我啊 我好学习一下:[email protected] 我最近也在写ModBus的东西,我是用线程写的,在线程里打开TCP侦听,使用Sleep来循环读取数据,现在状况良好,最开始也有经常断连的情况,原因发送了命令后设备没有回应导致TCP读取超时,超时后TCP会自动断连(开始为这件事烦了很久),这种情况有可能是设备没正确识别命令或命令本身就出错,比如地址错误等设备不回复信息,我的解决办法是自己写了一个接收缓冲类,在该类里执行TCP的读取操作,读取不设置超时(解决了超时断连的情况),命令发送后程序在缓冲类里读取数据。 http://www.control.com/1026219150/index_html 急求C#问题解答。。 如何把string转换为char[]以及byte[] 如何在程序中设置一个下拉框中内容改变的事件? 如何把SQL語句調用的數據用代碼將數據顯示在水晶報表里 Mdi的问题! 菜鸟进来C#数据库开发 怎么做像1616.com或者雅虎的那种天气预报 关于动态菜单的问题 急!寻求"运行时间超时"问题的解决方法,up有分! 新手的一个问题 Active Directory 创建用户出错 C#中点击弹出窗口的问题,高手来看看。
如果实在要写的话,我觉得如楼上的兄弟们说的主要问题还是在于Timer问题,我想如果是使用timer,问题可能还在于elapsed和触发的时机和次数。
对于timer事件,系统是并发执行的,也就是说:如果你的时间间隔过短,就有可能存在着由一个timer触发的多个elapsed在不同线程中执行的情况,可能你没有考虑这种情况下的线程同步问题。
我也考虑过利用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();
}
}
}