ModbusSlaveService.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using ModbusSimulationProtocol.Interface;
  2. using NModbus;
  3. using System.Net;
  4. using System.Net.Sockets;
  5. using SlaveDataStore = ModbusSimulationProtocol.Data.SlaveDataStore;
  6. namespace ModbusSimulationProtocol.Services;
  7. public class ModbusSlaveService : IModbusSlaveService
  8. {
  9. private readonly IModbusLogger? _logger;
  10. private readonly ModbusFactory _factory;
  11. private readonly ISlaveDataStore _slaveDataStore;
  12. private IModbusTcpSlaveNetwork? _network;
  13. private bool disposedValue;
  14. public ModbusSlaveService(IModbusLogger? logger)
  15. {
  16. _logger = logger;
  17. _factory = new ModbusFactory(logger: _logger);
  18. _slaveDataStore = new SlaveDataStore();
  19. }
  20. public ISlaveDataStore SlaveDataStore => _slaveDataStore;
  21. public bool Initialize(string ip, ushort port, byte slaveId = 1)
  22. {
  23. if (string.IsNullOrWhiteSpace(ip))
  24. {
  25. WriteLog(LoggingLevel.Error, "Incorrect ip address.");
  26. return false;
  27. }
  28. if (_network is not null)
  29. {
  30. WriteLog(LoggingLevel.Warning, "The slave network has been created.");
  31. return false;
  32. }
  33. try
  34. {
  35. TcpListener listener = new(IPAddress.Parse(ip), (int)port);
  36. _network = _factory.CreateSlaveNetwork(listener);
  37. var slave = _factory.CreateSlave(slaveId, _slaveDataStore);
  38. _network.AddSlave(slave);
  39. WriteLog(LoggingLevel.Information, "Completed initailization.");
  40. return true;
  41. }
  42. catch (Exception ex)
  43. {
  44. WriteLog(LoggingLevel.Error, ex.ToString());
  45. return false;
  46. }
  47. }
  48. public bool Open()
  49. {
  50. if (_network is null)
  51. {
  52. WriteLog(LoggingLevel.Error, "Performs initializing before doing any operation.");
  53. return false;
  54. }
  55. Task.Run(async () =>
  56. {
  57. try
  58. {
  59. WriteLog(LoggingLevel.Information, "Starts Tcp Listening");
  60. await _network.ListenAsync();
  61. }
  62. catch (Exception ex)
  63. {
  64. WriteLog(LoggingLevel.Error, ex.ToString());
  65. }
  66. });
  67. return true;
  68. }
  69. public bool TryWriteValues<TPoint>(IPointSource<TPoint> source, ushort address, TPoint[] points)
  70. {
  71. if (_network is null)
  72. {
  73. WriteLog(LoggingLevel.Error, "Performs initializing before doing any operation.");
  74. return false;
  75. }
  76. try
  77. {
  78. source.WritePoints(address, points);
  79. return true;
  80. }
  81. catch (Exception ex)
  82. {
  83. WriteLog(LoggingLevel.Error, ex.ToString());
  84. return false;
  85. }
  86. }
  87. public bool TryReadValues<TPoint>(IPointSource<TPoint> source, ushort address, ushort count, out TPoint[]? outPoints)
  88. {
  89. if (_network is null)
  90. {
  91. WriteLog(LoggingLevel.Error, "Performs initializing before doing any operation.");
  92. outPoints = null;
  93. return false;
  94. }
  95. try
  96. {
  97. outPoints = source.ReadPoints(address, count);
  98. return true;
  99. }
  100. catch (Exception ex)
  101. {
  102. WriteLog(LoggingLevel.Error, ex.ToString());
  103. outPoints = null;
  104. return false;
  105. }
  106. }
  107. private void WriteLog(LoggingLevel loggingLevel, string log)
  108. {
  109. _logger?.Log(loggingLevel, $"[{nameof(ModbusSlaveService)}]:{log}");
  110. }
  111. protected virtual void Dispose(bool disposing)
  112. {
  113. if (!disposedValue)
  114. {
  115. if (disposing)
  116. {
  117. // TODO: dispose managed state (managed objects)
  118. _network?.Dispose();
  119. }
  120. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  121. // TODO: set large fields to null
  122. disposedValue = true;
  123. }
  124. }
  125. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  126. // ~ModbusSlaveService()
  127. // {
  128. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  129. // Dispose(disposing: false);
  130. // }
  131. public void Dispose()
  132. {
  133. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  134. Dispose(disposing: true);
  135. GC.SuppressFinalize(this);
  136. }
  137. }