Add Comments
This commit is contained in:
@@ -3,6 +3,9 @@ using System.Linq;
|
|||||||
|
|
||||||
namespace Modbus.Net.Modbus
|
namespace Modbus.Net.Modbus
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Modbus地址翻译器基类
|
||||||
|
/// </summary>
|
||||||
public abstract class ModbusTranslatorBase : AddressTranslator
|
public abstract class ModbusTranslatorBase : AddressTranslator
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="com">串口地址</param>
|
/// <param name="ip">ip地址</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
/// <param name="endian">端格式</param>
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionType">连接类型</param>
|
/// <param name="connectionType">连接类型</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">读写的地址</param>
|
/// <param name="getAddresses">读写的地址</param>
|
||||||
@@ -33,6 +34,7 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionType">连接类型</param>
|
/// <param name="connectionType">连接类型</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">读写的地址</param>
|
/// <param name="getAddresses">读写的地址</param>
|
||||||
@@ -55,6 +57,7 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionType">连接类型</param>
|
/// <param name="connectionType">连接类型</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">读写的地址</param>
|
/// <param name="getAddresses">读写的地址</param>
|
||||||
@@ -76,6 +79,7 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionType">连接类型</param>
|
/// <param name="connectionType">连接类型</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">读写的地址</param>
|
/// <param name="getAddresses">读写的地址</param>
|
||||||
|
|||||||
@@ -396,7 +396,6 @@ namespace Modbus.Net.Modbus
|
|||||||
var translateAddress = addressTranslator.AddressTranslate(startAddress, false, true);
|
var translateAddress = addressTranslator.AddressTranslate(startAddress, false, true);
|
||||||
FunctionCode = (byte) translateAddress.Area;
|
FunctionCode = (byte) translateAddress.Area;
|
||||||
StartAddress = (ushort) translateAddress.Address;
|
StartAddress = (ushort) translateAddress.Address;
|
||||||
int a = 0, b = 0;
|
|
||||||
var writeByteValue =
|
var writeByteValue =
|
||||||
FunctionCode == (byte) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil
|
FunctionCode == (byte) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil
|
||||||
? ((bool) writeValue
|
? ((bool) writeValue
|
||||||
|
|||||||
@@ -5,11 +5,17 @@ using System.Text;
|
|||||||
|
|
||||||
namespace Modbus.Net.Modbus
|
namespace Modbus.Net.Modbus
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Rtu透传字节伸缩
|
||||||
|
/// </summary>
|
||||||
public class ModbusRtuInTcpProtocolLinkerBytesExtend : ModbusRtuProtocolLinkerBytesExtend
|
public class ModbusRtuInTcpProtocolLinkerBytesExtend : ModbusRtuProtocolLinkerBytesExtend
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Ascii透传字节伸缩
|
||||||
|
/// </summary>
|
||||||
public class ModbusAsciiInTcpProtocolLinkerBytesExtend : ModbusAsciiProtocolLinkerBytesExtend
|
public class ModbusAsciiInTcpProtocolLinkerBytesExtend : ModbusAsciiProtocolLinkerBytesExtend
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|||||||
@@ -35,7 +35,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="com">串口</param>
|
/// <param name="ip">ip地址</param>
|
||||||
|
/// <param name="port">端口号</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
/// <param name="endian">端格式</param>
|
||||||
|
|||||||
@@ -35,6 +35,9 @@ namespace Modbus.Net.Modbus
|
|||||||
AsciiInTcp = 4,
|
AsciiInTcp = 4,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 写单个单元的方法接口
|
||||||
|
/// </summary>
|
||||||
public interface IUtilityMethodWriteSingle : IUtilityMethod
|
public interface IUtilityMethodWriteSingle : IUtilityMethod
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ namespace Modbus.Net.OPC.FBox
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="localSequence">页名称</param>
|
/// <param name="localSequence">页名称</param>
|
||||||
/// <param name="linkerName">设备名称</param>
|
/// <param name="linkerName">设备名称</param>
|
||||||
/// <param name="getAddresses">获取地址</param>
|
/// <param name="getAddresses">获取地址</param>
|
||||||
@@ -36,6 +37,7 @@ namespace Modbus.Net.OPC.FBox
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="localSequence">页名称</param>
|
/// <param name="localSequence">页名称</param>
|
||||||
/// <param name="linkerName">设备名称</param>
|
/// <param name="linkerName">设备名称</param>
|
||||||
/// <param name="getAddresses">获取地址</param>
|
/// <param name="getAddresses">获取地址</param>
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">需要读写的数据</param>
|
/// <param name="getAddresses">需要读写的数据</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
@@ -29,6 +30,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">需要读写的数据</param>
|
/// <param name="getAddresses">需要读写的数据</param>
|
||||||
public OpcDaMachine(TKey id, string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
public OpcDaMachine(TKey id, string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
||||||
@@ -45,6 +47,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">需要读写的数据</param>
|
/// <param name="getAddresses">需要读写的数据</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
@@ -60,6 +63,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">需要读写的数据</param>
|
/// <param name="getAddresses">需要读写的数据</param>
|
||||||
public OpcDaMachine(string id, string connectionString, IEnumerable<AddressUnit> getAddresses)
|
public OpcDaMachine(string id, string connectionString, IEnumerable<AddressUnit> getAddresses)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要读写的地址</param>
|
/// <param name="getAddresses">需要读写的地址</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
protected OpcMachine(TKey id, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
protected OpcMachine(TKey id, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||||
@@ -30,6 +31,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要读写的地址</param>
|
/// <param name="getAddresses">需要读写的地址</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
protected OpcMachine(string id, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
protected OpcMachine(string id, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">需要读写的数据</param>
|
/// <param name="getAddresses">需要读写的数据</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
@@ -29,6 +30,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">需要读写的数据</param>
|
/// <param name="getAddresses">需要读写的数据</param>
|
||||||
public OpcUaMachine(TKey id, string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
public OpcUaMachine(TKey id, string connectionString, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
||||||
@@ -45,6 +47,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">需要读写的数据</param>
|
/// <param name="getAddresses">需要读写的数据</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
@@ -60,6 +63,7 @@ namespace Modbus.Net.OPC
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造函数
|
/// 构造函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="connectionString">连接地址</param>
|
/// <param name="connectionString">连接地址</param>
|
||||||
/// <param name="getAddresses">需要读写的数据</param>
|
/// <param name="getAddresses">需要读写的数据</param>
|
||||||
public OpcUaMachine(string id, string connectionString, IEnumerable<AddressUnit> getAddresses)
|
public OpcUaMachine(string id, string connectionString, IEnumerable<AddressUnit> getAddresses)
|
||||||
|
|||||||
@@ -18,11 +18,18 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
//protected event MessageReturnDelegate MessageReturn;
|
//protected event MessageReturnDelegate MessageReturn;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 增加传输控制器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="controller">传输控制器</param>
|
||||||
public void AddController(IController controller)
|
public void AddController(IController controller)
|
||||||
{
|
{
|
||||||
Controller = controller;
|
Controller = controller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 传输控制器
|
||||||
|
/// </summary>
|
||||||
protected IController Controller { get; set; }
|
protected IController Controller { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -7,18 +7,30 @@ using Serilog;
|
|||||||
|
|
||||||
namespace Modbus.Net
|
namespace Modbus.Net
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 控制器基类
|
||||||
|
/// </summary>
|
||||||
public abstract class BaseController : IController
|
public abstract class BaseController : IController
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 等待的消息队列
|
||||||
|
/// </summary>
|
||||||
protected List<MessageWaitingDef> WaitingMessages { get; set; }
|
protected List<MessageWaitingDef> WaitingMessages { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 消息维护线程
|
||||||
|
/// </summary>
|
||||||
protected Task SendingThread { get; set; }
|
protected Task SendingThread { get; set; }
|
||||||
protected bool _taskCancel = false;
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造器
|
||||||
|
/// </summary>
|
||||||
protected BaseController()
|
protected BaseController()
|
||||||
{
|
{
|
||||||
WaitingMessages = new List<MessageWaitingDef>();
|
WaitingMessages = new List<MessageWaitingDef>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IController.AddMessage(byte[])" />
|
||||||
public MessageWaitingDef AddMessage(byte[] sendMessage)
|
public MessageWaitingDef AddMessage(byte[] sendMessage)
|
||||||
{
|
{
|
||||||
var def = new MessageWaitingDef
|
var def = new MessageWaitingDef
|
||||||
@@ -32,13 +44,15 @@ namespace Modbus.Net
|
|||||||
return def;
|
return def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送消息的实际内部方法
|
||||||
|
/// </summary>
|
||||||
protected abstract void SendingMessageControlInner();
|
protected abstract void SendingMessageControlInner();
|
||||||
|
|
||||||
public void SendStop()
|
/// <inheritdoc cref="IController.SendStop" />
|
||||||
{
|
public abstract void SendStop();
|
||||||
_taskCancel = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IController.SendStart" />
|
||||||
public void SendStart()
|
public void SendStart()
|
||||||
{
|
{
|
||||||
if (SendingThread == null)
|
if (SendingThread == null)
|
||||||
@@ -47,6 +61,7 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IController.Clear" />
|
||||||
public void Clear()
|
public void Clear()
|
||||||
{
|
{
|
||||||
lock (WaitingMessages)
|
lock (WaitingMessages)
|
||||||
@@ -55,6 +70,10 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将信息添加到队列
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="def">需要添加的信息信息</param>
|
||||||
protected virtual void AddMessageToList(MessageWaitingDef def)
|
protected virtual void AddMessageToList(MessageWaitingDef def)
|
||||||
{
|
{
|
||||||
lock (WaitingMessages)
|
lock (WaitingMessages)
|
||||||
@@ -63,8 +82,14 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取信息的检索关键字
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">待确认的信息</param>
|
||||||
|
/// <returns>信息的检索关键字</returns>
|
||||||
protected abstract string GetKeyFromMessage(byte[] message);
|
protected abstract string GetKeyFromMessage(byte[] message);
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IController.ConfirmMessage(byte[])" />
|
||||||
public bool ConfirmMessage(byte[] receiveMessage)
|
public bool ConfirmMessage(byte[] receiveMessage)
|
||||||
{
|
{
|
||||||
var def = GetMessageFromWaitingList(receiveMessage);
|
var def = GetMessageFromWaitingList(receiveMessage);
|
||||||
@@ -81,8 +106,15 @@ namespace Modbus.Net
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 从等待队列中匹配信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="receiveMessage">返回的信息</param>
|
||||||
|
/// <returns>从等待队列中匹配的信息</returns>
|
||||||
protected abstract MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage);
|
protected abstract MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage);
|
||||||
|
|
||||||
|
/// <inheritdoc cref="IController.ForceRemoveWaitingMessage(MessageWaitingDef)"/>
|
||||||
|
/// <param name="def"></param>
|
||||||
public void ForceRemoveWaitingMessage(MessageWaitingDef def)
|
public void ForceRemoveWaitingMessage(MessageWaitingDef def)
|
||||||
{
|
{
|
||||||
lock (WaitingMessages)
|
lock (WaitingMessages)
|
||||||
@@ -92,16 +124,34 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 等待信息的定义
|
||||||
|
/// </summary>
|
||||||
public class MessageWaitingDef
|
public class MessageWaitingDef
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 信息的关键字
|
||||||
|
/// </summary>
|
||||||
public string Key { get; set; }
|
public string Key { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送的信息
|
||||||
|
/// </summary>
|
||||||
public byte[] SendMessage { get; set; }
|
public byte[] SendMessage { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 接收的信息
|
||||||
|
/// </summary>
|
||||||
public byte[] ReceiveMessage { get; set; }
|
public byte[] ReceiveMessage { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送的信号
|
||||||
|
/// </summary>
|
||||||
public EventWaitHandle SendMutex { get; set; }
|
public EventWaitHandle SendMutex { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 接收的信号
|
||||||
|
/// </summary>
|
||||||
public EventWaitHandle ReceiveMutex { get; set; }
|
public EventWaitHandle ReceiveMutex { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -92,6 +92,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造器
|
/// 构造器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
||||||
protected BaseMachine(string id, IEnumerable<AddressUnit> getAddresses) : base(id, getAddresses)
|
protected BaseMachine(string id, IEnumerable<AddressUnit> getAddresses) : base(id, getAddresses)
|
||||||
{
|
{
|
||||||
@@ -100,6 +101,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造器
|
/// 构造器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
protected BaseMachine(string id, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
protected BaseMachine(string id, IEnumerable<AddressUnit> getAddresses, bool keepConnect)
|
||||||
@@ -110,6 +112,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造器
|
/// 构造器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
/// <param name="slaveAddress">从站地址</param>
|
/// <param name="slaveAddress">从站地址</param>
|
||||||
@@ -134,6 +137,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造器
|
/// 构造器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
||||||
protected BaseMachine(TKey id, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
protected BaseMachine(TKey id, IEnumerable<AddressUnit<TUnitKey>> getAddresses)
|
||||||
: this(id, getAddresses, false)
|
: this(id, getAddresses, false)
|
||||||
@@ -143,6 +147,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造器
|
/// 构造器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
protected BaseMachine(TKey id, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
protected BaseMachine(TKey id, IEnumerable<AddressUnit<TUnitKey>> getAddresses, bool keepConnect)
|
||||||
@@ -155,6 +160,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造器
|
/// 构造器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
/// <param name="id">设备的ID号</param>
|
||||||
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
/// <param name="getAddresses">需要与设备通讯的地址</param>
|
||||||
/// <param name="keepConnect">是否保持连接</param>
|
/// <param name="keepConnect">是否保持连接</param>
|
||||||
/// <param name="slaveAddress">从站地址</param>
|
/// <param name="slaveAddress">从站地址</param>
|
||||||
@@ -188,8 +194,8 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public AddressTranslator AddressTranslator
|
public AddressTranslator AddressTranslator
|
||||||
{
|
{
|
||||||
get { return BaseUtility.AddressTranslator; }
|
get => BaseUtility.AddressTranslator;
|
||||||
set { BaseUtility.AddressTranslator = value; }
|
set => BaseUtility.AddressTranslator = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -377,6 +377,9 @@ namespace Modbus.Net
|
|||||||
return returnBytes.ByteToString();
|
return returnBytes.ByteToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 确认串口是否已经打开,如果没有打开则尝试打开两次,连续失败直接释放连接资源
|
||||||
|
/// </summary>
|
||||||
protected void CheckOpen()
|
protected void CheckOpen()
|
||||||
{
|
{
|
||||||
if (!SerialPort.IsOpen)
|
if (!SerialPort.IsOpen)
|
||||||
@@ -439,6 +442,10 @@ namespace Modbus.Net
|
|||||||
return messageSendingdef;
|
return messageSendingdef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送数据,不确认
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">需要发送的数据</param>
|
||||||
protected override async Task SendMsgWithoutConfirm(byte[] message)
|
protected override async Task SendMsgWithoutConfirm(byte[] message)
|
||||||
{
|
{
|
||||||
using (await SerialPort.Lock.LockAsync())
|
using (await SerialPort.Lock.LockAsync())
|
||||||
@@ -459,11 +466,13 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override void ReceiveMsgThreadStart()
|
protected override void ReceiveMsgThreadStart()
|
||||||
{
|
{
|
||||||
_receiveThread = Task.Run(()=>ReceiveMessage());
|
_receiveThread = Task.Run(()=>ReceiveMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
protected override void ReceiveMsgThreadStop()
|
protected override void ReceiveMsgThreadStop()
|
||||||
{
|
{
|
||||||
_taskCancel = true;
|
_taskCancel = true;
|
||||||
|
|||||||
@@ -8,17 +8,30 @@ using Serilog;
|
|||||||
|
|
||||||
namespace Modbus.Net
|
namespace Modbus.Net
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 先入先出式控制器
|
||||||
|
/// </summary>
|
||||||
public class FifoController : BaseController
|
public class FifoController : BaseController
|
||||||
{
|
{
|
||||||
private MessageWaitingDef _currentSendingPos;
|
private MessageWaitingDef _currentSendingPos;
|
||||||
|
|
||||||
|
private bool _taskCancel = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 间隔时间
|
||||||
|
/// </summary>
|
||||||
public int AcquireTime { get; }
|
public int AcquireTime { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="acquireTime">间隔时间</param>
|
||||||
public FifoController(int acquireTime)
|
public FifoController(int acquireTime)
|
||||||
{
|
{
|
||||||
AcquireTime = acquireTime;
|
AcquireTime = acquireTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="BaseController.SendingMessageControlInner"/>
|
||||||
protected override void SendingMessageControlInner()
|
protected override void SendingMessageControlInner()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -64,11 +77,19 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="BaseController.SendStop"/>
|
||||||
|
public override void SendStop()
|
||||||
|
{
|
||||||
|
_taskCancel = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="BaseController.GetKeyFromMessage(byte[])"/>
|
||||||
protected override string GetKeyFromMessage(byte[] message)
|
protected override string GetKeyFromMessage(byte[] message)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="BaseController.GetMessageFromWaitingList(byte[])"/>
|
||||||
protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage)
|
protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage)
|
||||||
{
|
{
|
||||||
return WaitingMessages.FirstOrDefault();
|
return WaitingMessages.FirstOrDefault();
|
||||||
|
|||||||
@@ -1,17 +1,43 @@
|
|||||||
namespace Modbus.Net
|
namespace Modbus.Net
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 传输控制器接口
|
||||||
|
/// </summary>
|
||||||
public interface IController
|
public interface IController
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 增加信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sendMessage">需要发送的信息</param>
|
||||||
|
/// <returns></returns>
|
||||||
MessageWaitingDef AddMessage(byte[] sendMessage);
|
MessageWaitingDef AddMessage(byte[] sendMessage);
|
||||||
|
|
||||||
void SendStop();
|
/// <summary>
|
||||||
|
/// 启动传输控制线程
|
||||||
|
/// </summary>
|
||||||
void SendStart();
|
void SendStart();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 关闭传输控制线程
|
||||||
|
/// </summary>
|
||||||
|
void SendStop();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 清空所有待发送的信息
|
||||||
|
/// </summary>
|
||||||
void Clear();
|
void Clear();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 将返回的信息绑定到发送的信息上,并对信息进行确认
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="receiveMessage">返回的信息</param>
|
||||||
|
/// <returns>是否正常确认</returns>
|
||||||
bool ConfirmMessage(byte[] receiveMessage);
|
bool ConfirmMessage(byte[] receiveMessage);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 没有任何返回时强行删除等待队列上的信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="def">需要强行删除的信息</param>
|
||||||
void ForceRemoveWaitingMessage(MessageWaitingDef def);
|
void ForceRemoveWaitingMessage(MessageWaitingDef def);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ namespace Modbus.Net
|
|||||||
/// <typeparam name="TParamIn">向Connector传入的类型</typeparam>
|
/// <typeparam name="TParamIn">向Connector传入的类型</typeparam>
|
||||||
/// <typeparam name="TParamOut">从Connector返回的类型</typeparam>
|
/// <typeparam name="TParamOut">从Connector返回的类型</typeparam>
|
||||||
/// <typeparam name="TProtocolUnit">协议单元的类型</typeparam>
|
/// <typeparam name="TProtocolUnit">协议单元的类型</typeparam>
|
||||||
|
/// <typeparam name="TPipeUnit">管道的类型</typeparam>
|
||||||
public interface IProtocol<TParamIn, TParamOut, TProtocolUnit, TPipeUnit>
|
public interface IProtocol<TParamIn, TParamOut, TProtocolUnit, TPipeUnit>
|
||||||
where TProtocolUnit : class, IProtocolFormatting<TParamIn, TParamOut>
|
where TProtocolUnit : class, IProtocolFormatting<TParamIn, TParamOut>
|
||||||
where TParamOut : class
|
where TParamOut : class
|
||||||
|
|||||||
@@ -8,20 +8,37 @@ using Serilog;
|
|||||||
|
|
||||||
namespace Modbus.Net
|
namespace Modbus.Net
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 通讯号匹配模式的控制器
|
||||||
|
/// </summary>
|
||||||
public class MatchController : BaseController
|
public class MatchController : BaseController
|
||||||
{
|
{
|
||||||
private MessageWaitingDef _currentSendingPos;
|
private MessageWaitingDef _currentSendingPos;
|
||||||
|
|
||||||
|
private bool _taskCancel = false;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 获取间隔
|
||||||
|
/// </summary>
|
||||||
public int AcquireTime { get; }
|
public int AcquireTime { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 匹配字典
|
||||||
|
/// </summary>
|
||||||
protected ICollection<int>[] KeyMatches { get; }
|
protected ICollection<int>[] KeyMatches { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 构造器
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="keyMatches">匹配字典,每个Collection代表一个匹配集合,每一个匹配集合中的数字代表需要匹配的位置,最后计算出来的数字是所有位置数字按照集合排序后叠放在一起</param>
|
||||||
|
/// <param name="acquireTime">获取间隔</param>
|
||||||
public MatchController(ICollection<int>[] keyMatches, int acquireTime)
|
public MatchController(ICollection<int>[] keyMatches, int acquireTime)
|
||||||
{
|
{
|
||||||
KeyMatches = keyMatches;
|
KeyMatches = keyMatches;
|
||||||
AcquireTime = acquireTime;
|
AcquireTime = acquireTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="BaseController.SendingMessageControlInner" />
|
||||||
protected override void SendingMessageControlInner()
|
protected override void SendingMessageControlInner()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
@@ -67,6 +84,13 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="BaseController.SendStop" />
|
||||||
|
public override void SendStop()
|
||||||
|
{
|
||||||
|
_taskCancel = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="BaseController.GetKeyFromMessage(byte[])" />
|
||||||
protected override string GetKeyFromMessage(byte[] message)
|
protected override string GetKeyFromMessage(byte[] message)
|
||||||
{
|
{
|
||||||
string ans = "";
|
string ans = "";
|
||||||
@@ -82,6 +106,7 @@ namespace Modbus.Net
|
|||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc cref="BaseController.GetMessageFromWaitingList(byte[])" />
|
||||||
protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage)
|
protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage)
|
||||||
{
|
{
|
||||||
var returnKey = GetKeyFromMessage(receiveMessage);
|
var returnKey = GetKeyFromMessage(receiveMessage);
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
using System;
|
using System.Globalization;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
|
||||||
|
|
||||||
namespace Modbus.Net
|
namespace Modbus.Net
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 串口辅助类
|
||||||
|
/// </summary>
|
||||||
public static class SerialPortHelper
|
public static class SerialPortHelper
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -237,6 +237,10 @@ namespace Modbus.Net
|
|||||||
return messageSendingdef;
|
return messageSendingdef;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 发送信息,不进行返回确认
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="message">发送的信息</param>
|
||||||
protected override async Task SendMsgWithoutConfirm(byte[] message)
|
protected override async Task SendMsgWithoutConfirm(byte[] message)
|
||||||
{
|
{
|
||||||
var datagram = message;
|
var datagram = message;
|
||||||
@@ -261,11 +265,17 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 启动获取线程
|
||||||
|
/// </summary>
|
||||||
protected override void ReceiveMsgThreadStart()
|
protected override void ReceiveMsgThreadStart()
|
||||||
{
|
{
|
||||||
_receiveThread = Task.Run(ReceiveMessage);
|
_receiveThread = Task.Run(ReceiveMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 停止获取线程
|
||||||
|
/// </summary>
|
||||||
protected override void ReceiveMsgThreadStop()
|
protected override void ReceiveMsgThreadStop()
|
||||||
{
|
{
|
||||||
_taskCancel = true;
|
_taskCancel = true;
|
||||||
@@ -317,7 +327,7 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 接收消息,并转换成字符串
|
/// 接收消息,并转换成字符串
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="len"></param>
|
/// <param name="len">消息的长度</param>
|
||||||
private byte[] CheckReplyDatagram(int len)
|
private byte[] CheckReplyDatagram(int len)
|
||||||
{
|
{
|
||||||
var replyMessage = new byte[len];
|
var replyMessage = new byte[len];
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ namespace CrossLamp.Controllers
|
|||||||
light.SetStart(lamps[6]);
|
light.SetStart(lamps[6]);
|
||||||
return Json(light, JsonRequestBehavior.AllowGet);
|
return Json(light, JsonRequestBehavior.AllowGet);
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user