2017-12-21 Update 2 Serial MultiStation(Not Test)

This commit is contained in:
parallelbgls
2017-12-21 15:50:49 +08:00
parent 8488151f2e
commit 8614905006

View File

@@ -215,6 +215,7 @@ namespace Modbus.Net
if (disposing) if (disposing)
{ {
// Release managed resources // Release managed resources
Controller.SendStop();
} }
// Release unmanaged resources // Release unmanaged resources
if (SerialPort != null) if (SerialPort != null)
@@ -233,6 +234,7 @@ namespace Modbus.Net
Log.Information("Com interface {Com} Disposed", _com); Log.Information("Com interface {Com} Disposed", _com);
Connectors[_com] = null; Connectors[_com] = null;
Connectors.Remove(_com); Connectors.Remove(_com);
ReceiveMsgThreadStop();
} }
Linkers.Remove(_slave); Linkers.Remove(_slave);
Log.Information("Com connector {ConnectionToken} Removed", ConnectionToken); Log.Information("Com connector {ConnectionToken} Removed", ConnectionToken);
@@ -292,6 +294,7 @@ namespace Modbus.Net
try try
{ {
if (!Connectors.ContainsKey(_com)) if (!Connectors.ContainsKey(_com))
{
Connectors.Add(_com, new SerialPortLock Connectors.Add(_com, new SerialPortLock
{ {
PortName = _com, PortName = _com,
@@ -301,12 +304,24 @@ namespace Modbus.Net
DataBits = _dataBits, DataBits = _dataBits,
ReadTimeout = _timeoutTime ReadTimeout = _timeoutTime
}); });
}
if (!Linkers.ContainsKey(_slave)) if (!Linkers.ContainsKey(_slave))
{
Linkers.Add(_slave, _com); Linkers.Add(_slave, _com);
SerialPort.Open(); }
Log.Information("Com client {ConnectionToken} connect success", ConnectionToken); if (!SerialPort.IsOpen)
{
lock (SerialPort)
{
SerialPort.Open();
ReceiveMsgThreadStart();
}
}
Controller.SendStart(); Controller.SendStart();
ReceiveMsgThreadStart();
Log.Information("Com client {ConnectionToken} connect success", ConnectionToken);
return true; return true;
} }
catch (Exception e) catch (Exception e)
@@ -334,8 +349,6 @@ namespace Modbus.Net
if (Linkers.ContainsKey(_slave) && Connectors.ContainsKey(_com)) if (Linkers.ContainsKey(_slave) && Connectors.ContainsKey(_com))
try try
{ {
ReceiveMsgThreadStop();
Controller.SendStop();
Dispose(); Dispose();
Log.Information("Com client {ConnectionToken} disconnect success", ConnectionToken); Log.Information("Com client {ConnectionToken} disconnect success", ConnectionToken);
return true; return true;