Open GoogleCodeExporter opened 8 years ago
Hi,
please have a look at the:
http://groups.google.com/group/nmodbus-discuss/browse_thread/thread/4dbf37e02e70
9e95
Kind regards,
David
Original comment by david.ba...@gmail.com
on 7 Jan 2010 at 3:48
as alternative you may implement Modbus.IO.IStreamResource which will reconnect
on failure.
Sample unchecked code:
{{{
public class SafeNetworkStream : Modbus.IO.IStreamResource, IDisposable
{
private TcpClient tcp = null; // internal tcp client
private Uri target = null; // tcp://1.2.3.4:502/
private int _rt = 500; // read timeout
private int _wt = 1000; // write timeout
public SafeNetworkStream(Uri target)
{
this.target = target;
Connect();
}
public void Dispose()
{
var t = tcp;
tcp = null;
if (t != null)
try
{
t.Close();
}
catch { }
}
public void DiscardInBuffer()
{
var t = tcp;
if(t != null)
try
{
if (!t.Connected) return;
var s = t.GetStream();
int a;
while ((a = t.Available) > 0)
s.Read(new byte[a], 0, a); // HACK
}
catch { }
}
public int InfiniteTimeout { get { return Timeout.Infinite; } }
public int ReadTimeout
{
get { return _rt; }
set
{
_rt = value;
if (tcp != null) tcp.ReceiveTimeout = _rt;
}
}
public int WriteTimeout
{
get { return _wt; }
set
{
_wt = value;
if (tcp != null) tcp.SendTimeout = _wt;
}
}
private void Connect()
{
if(Monitor.TryEnter(target))
try
{
var t = tcp;
tcp = null;
ThreadPool.QueueUserWorkItem(_connect);
if (t != null) t.Close();
}
catch { }
finally { Monitor.Exit(target); }
}
object _connectLock = new object();
private void _connect(object e)
{
if(Monitor.TryEnter(_connectLock))
try
{
int port = target.Port;
var t = new TcpClient(target.Host, port <= 0 ? 502 : port)
{
ReceiveBufferSize = 10000,
ReceiveTimeout = _rt,
SendTimeout = _wt
};
if (t.Connected) tcp = t;
}
catch { }
finally { Monitor.Exit(_connectLock); }
}
public int Read(byte[] buffer, int offset, int count)
{
var t = tcp;
if (t != null)
try
{
return t.GetStream().Read(buffer, offset, count);
}
catch { }
Connect();
return -1;
}
public void Write(byte[] buf, int ofs, int cnt)
{
var t = tcp;
if(t != null)
try
{
t.GetStream().Write(buf, ofs, cnt);
return;
}
catch { }
Connect();
}
}
}}}
Original comment by unejir...@gmail.com
on 27 Jul 2010 at 6:16
Original issue reported on code.google.com by
e...@drpa.co.za
on 29 Oct 2009 at 8:58