ModbusFactory.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using System;
  2. using System.Collections.Generic;
  3. using NModbus.Device.MessageHandlers;
  4. using System.Linq;
  5. using System.Net.Sockets;
  6. using NModbus.Extensions;
  7. using NModbus.Data;
  8. using NModbus.Device;
  9. using NModbus.IO;
  10. using NModbus.Logging;
  11. namespace NModbus
  12. {
  13. public class ModbusFactory : IModbusFactory
  14. {
  15. /// <summary>
  16. /// The "built-in" message handlers.
  17. /// </summary>
  18. private static readonly IModbusFunctionService[] BuiltInFunctionServices =
  19. {
  20. new ReadCoilsService(),
  21. new ReadInputsService(),
  22. new ReadHoldingRegistersService(),
  23. new ReadInputRegistersService(),
  24. new DiagnosticsService(),
  25. new WriteSingleCoilService(),
  26. new WriteSingleRegisterService(),
  27. new WriteMultipleCoilsService(),
  28. new WriteMultipleRegistersService(),
  29. new WriteFileRecordService(),
  30. new ReadWriteMultipleRegistersService(),
  31. };
  32. private readonly IDictionary<byte, IModbusFunctionService> _functionServices;
  33. /// <summary>
  34. /// Create a factory which uses the built in standard slave function handlers.
  35. /// </summary>
  36. public ModbusFactory()
  37. {
  38. _functionServices = BuiltInFunctionServices.ToDictionary(s => s.FunctionCode, s => s);
  39. Logger = NullModbusLogger.Instance;
  40. }
  41. /// <summary>
  42. /// Create a factory which optionally uses the built in function services and allows custom services to be added.
  43. /// </summary>
  44. /// <param name="functionServices">User provided function services.</param>
  45. /// <param name="includeBuiltIn">If true, the built in function services are included. Otherwise, all function services will come from the functionService parameter.</param>
  46. /// <param name="logger">Logger</param>
  47. public ModbusFactory(
  48. IEnumerable<IModbusFunctionService> functionServices = null,
  49. bool includeBuiltIn = true,
  50. IModbusLogger logger = null)
  51. {
  52. Logger = logger ?? NullModbusLogger.Instance;
  53. //Determine if we're including the built in services
  54. if (includeBuiltIn)
  55. {
  56. //Make a dictionary out of the built in services
  57. _functionServices = BuiltInFunctionServices
  58. .ToDictionary(s => s.FunctionCode, s => s);
  59. }
  60. else
  61. {
  62. //Create an empty dictionary
  63. _functionServices = new Dictionary<byte, IModbusFunctionService>();
  64. }
  65. if (functionServices != null)
  66. {
  67. //Add and replace the provided function services as necessary.
  68. foreach (IModbusFunctionService service in functionServices)
  69. {
  70. //This will add or replace the service.
  71. _functionServices[service.FunctionCode] = service;
  72. }
  73. }
  74. }
  75. public IModbusSlave CreateSlave(byte unitId, ISlaveDataStore dataStore = null)
  76. {
  77. if (dataStore == null)
  78. dataStore = new DefaultSlaveDataStore();
  79. return new ModbusSlave(unitId, dataStore, GetAllFunctionServices());
  80. }
  81. public IModbusSlaveNetwork CreateSlaveNetwork(IModbusRtuTransport transport)
  82. {
  83. return new ModbusSerialSlaveNetwork(transport, this, Logger);
  84. }
  85. public IModbusSlaveNetwork CreateSlaveNetwork(IModbusAsciiTransport transport)
  86. {
  87. return new ModbusSerialSlaveNetwork(transport, this, Logger);
  88. }
  89. public IModbusTcpSlaveNetwork CreateSlaveNetwork(TcpListener tcpListener)
  90. {
  91. return new ModbusTcpSlaveNetwork(tcpListener, this, Logger);
  92. }
  93. public IModbusSlaveNetwork CreateSlaveNetwork(UdpClient client)
  94. {
  95. return new ModbusUdpSlaveNetwork(client, this, Logger);
  96. }
  97. public IModbusRtuTransport CreateRtuTransport(IStreamResource streamResource)
  98. {
  99. return new ModbusRtuTransport(streamResource, this, Logger);
  100. }
  101. public IModbusAsciiTransport CreateAsciiTransport(IStreamResource streamResource)
  102. {
  103. return new ModbusAsciiTransport(streamResource, this, Logger);
  104. }
  105. public IModbusTransport CreateIpTransport(IStreamResource streamResource)
  106. {
  107. return new ModbusIpTransport(streamResource, this, Logger);
  108. }
  109. public IModbusLogger Logger { get; }
  110. public IModbusFunctionService[] GetAllFunctionServices()
  111. {
  112. return _functionServices
  113. .Values
  114. .ToArray();
  115. }
  116. public IModbusSerialMaster CreateMaster(IModbusSerialTransport transport)
  117. {
  118. return new ModbusSerialMaster(transport);
  119. }
  120. public IModbusMaster CreateMaster(UdpClient client)
  121. {
  122. var adapter = new UdpClientAdapter(client);
  123. var transport = new ModbusIpTransport(adapter, this, Logger);
  124. return new ModbusIpMaster(transport);
  125. }
  126. public IModbusMaster CreateMaster(TcpClient client)
  127. {
  128. var adapter = new TcpClientAdapter(client);
  129. var transport = new ModbusIpTransport(adapter, this, Logger);
  130. return new ModbusIpMaster(transport);
  131. }
  132. public IModbusMaster CreateMaster(Socket client)
  133. {
  134. var adapter = new SocketAdapter(client);
  135. var transport = new ModbusRtuTransport(adapter, this, Logger);
  136. return new ModbusSerialMaster(transport);
  137. }
  138. public IModbusFunctionService GetFunctionService(byte functionCode)
  139. {
  140. return _functionServices.GetValueOrDefault(functionCode);
  141. }
  142. }
  143. }