2014-09-19 update 1
This commit is contained in:
@@ -7,9 +7,6 @@ namespace ModBus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class AddressTranslator
|
public abstract class AddressTranslator
|
||||||
{
|
{
|
||||||
protected static AddressTranslator _instance;
|
|
||||||
public Dictionary<string, short> TransDictionary;
|
|
||||||
|
|
||||||
public abstract ushort AddressTranslate(string address);
|
public abstract ushort AddressTranslate(string address);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,6 +15,8 @@ namespace ModBus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public class AddressTranslatorNA200H : AddressTranslator
|
public class AddressTranslatorNA200H : AddressTranslator
|
||||||
{
|
{
|
||||||
|
public Dictionary<string, short> TransDictionary;
|
||||||
|
|
||||||
private AddressTranslatorNA200H()
|
private AddressTranslatorNA200H()
|
||||||
{
|
{
|
||||||
TransDictionary = new Dictionary<string, short>();
|
TransDictionary = new Dictionary<string, short>();
|
||||||
@@ -36,15 +35,6 @@ namespace ModBus.Net
|
|||||||
TransDictionary.Add("V", 0);
|
TransDictionary.Add("V", 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static AddressTranslator GetInstance()
|
|
||||||
{
|
|
||||||
if (_instance == null)
|
|
||||||
{
|
|
||||||
_instance = new AddressTranslatorNA200H();
|
|
||||||
}
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override ushort AddressTranslate(string address)
|
public override ushort AddressTranslate(string address)
|
||||||
{
|
{
|
||||||
address = address.ToUpper();
|
address = address.ToUpper();
|
||||||
@@ -60,4 +50,17 @@ namespace ModBus.Net
|
|||||||
return (ushort) (TransDictionary[head] + ushort.Parse(tail) - 1);
|
return (ushort) (TransDictionary[head] + ushort.Parse(tail) - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class AddressTranslatorBase : AddressTranslator
|
||||||
|
{
|
||||||
|
public override ushort AddressTranslate(string address)
|
||||||
|
{
|
||||||
|
ushort num;
|
||||||
|
if (ushort.TryParse(address, out num))
|
||||||
|
{
|
||||||
|
return num;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -16,6 +16,7 @@ namespace ModBus.Net
|
|||||||
/// <returns>数据是否正确接收</returns>
|
/// <returns>数据是否正确接收</returns>
|
||||||
protected ProtocalLinker _protocalLinker;
|
protected ProtocalLinker _protocalLinker;
|
||||||
|
|
||||||
|
|
||||||
protected BaseProtocal()
|
protected BaseProtocal()
|
||||||
{
|
{
|
||||||
Protocals = new Dictionary<string, ProtocalUnit>();
|
Protocals = new Dictionary<string, ProtocalUnit>();
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
BelongAddress = belongAddress;
|
BelongAddress = belongAddress;
|
||||||
FunctionCode = (int)ModbusProtocalReg.ReadCoilStatus;
|
FunctionCode = (int)ModbusProtocalReg.ReadCoilStatus;
|
||||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
StartAddress = _addressTranslator.AddressTranslate(startAddress);
|
||||||
GetCount = getCount;
|
GetCount = getCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +133,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
BelongAddress = belongAddress;
|
BelongAddress = belongAddress;
|
||||||
FunctionCode = (int)ModbusProtocalReg.ReadInputStatus;
|
FunctionCode = (int)ModbusProtocalReg.ReadInputStatus;
|
||||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
StartAddress = _addressTranslator.AddressTranslate(startAddress);
|
||||||
GetCount = getCount;
|
GetCount = getCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,7 +200,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
BelongAddress = belongAddress;
|
BelongAddress = belongAddress;
|
||||||
FunctionCode = (int)ModbusProtocalReg.ReadHoldRegister;
|
FunctionCode = (int)ModbusProtocalReg.ReadHoldRegister;
|
||||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
StartAddress = _addressTranslator.AddressTranslate(startAddress);
|
||||||
GetCount = getCount;
|
GetCount = getCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -326,7 +326,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
BelongAddress = belongAddress;
|
BelongAddress = belongAddress;
|
||||||
FunctionCode = (int)ModbusProtocalReg.ReadInputRegister;
|
FunctionCode = (int)ModbusProtocalReg.ReadInputRegister;
|
||||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
StartAddress = _addressTranslator.AddressTranslate(startAddress);
|
||||||
GetCount = getCount;
|
GetCount = getCount;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -388,7 +388,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
BelongAddress = belongAddress;
|
BelongAddress = belongAddress;
|
||||||
FunctionCode = (int)ModbusProtocalReg.WriteOneCoil;
|
FunctionCode = (int)ModbusProtocalReg.WriteOneCoil;
|
||||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
StartAddress = _addressTranslator.AddressTranslate(startAddress);
|
||||||
WriteValue = writeValue;
|
WriteValue = writeValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -449,7 +449,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
BelongAddress = belongAddress;
|
BelongAddress = belongAddress;
|
||||||
FunctionCode = (int)ModbusProtocalReg.WriteOneRegister;
|
FunctionCode = (int)ModbusProtocalReg.WriteOneRegister;
|
||||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
StartAddress = _addressTranslator.AddressTranslate(startAddress);
|
||||||
WriteValue = writeValue;
|
WriteValue = writeValue;
|
||||||
}
|
}
|
||||||
public byte BelongAddress { get; private set; }
|
public byte BelongAddress { get; private set; }
|
||||||
@@ -509,7 +509,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
BelongAddress = belongAddress;
|
BelongAddress = belongAddress;
|
||||||
FunctionCode = (int)ModbusProtocalReg.WriteMultiCoil;
|
FunctionCode = (int)ModbusProtocalReg.WriteMultiCoil;
|
||||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
StartAddress = _addressTranslator.AddressTranslate(startAddress);
|
||||||
WriteCount = (ushort)writeValue.Length;
|
WriteCount = (ushort)writeValue.Length;
|
||||||
WriteByteCount = WriteCount % 8 > 0 ? (byte)(WriteCount / 8 + 1) : (byte)(WriteCount / 8);
|
WriteByteCount = WriteCount % 8 > 0 ? (byte)(WriteCount / 8 + 1) : (byte)(WriteCount / 8);
|
||||||
WriteValue = new byte[WriteByteCount];
|
WriteValue = new byte[WriteByteCount];
|
||||||
@@ -587,7 +587,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
BelongAddress = belongAddress;
|
BelongAddress = belongAddress;
|
||||||
FunctionCode = (int)ModbusProtocalReg.WriteMultiRegister;
|
FunctionCode = (int)ModbusProtocalReg.WriteMultiRegister;
|
||||||
StartAddress = AddressTranslatorNA200H.GetInstance().AddressTranslate(startAddress);
|
StartAddress = _addressTranslator.AddressTranslate(startAddress);
|
||||||
WriteCount = (ushort)writeValue.Length;
|
WriteCount = (ushort)writeValue.Length;
|
||||||
WriteByteCount = (byte)(WriteCount * 2);
|
WriteByteCount = (byte)(WriteCount * 2);
|
||||||
WriteValue = writeValue.Clone() as object[];
|
WriteValue = writeValue.Clone() as object[];
|
||||||
|
|||||||
@@ -7,6 +7,14 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
public abstract class ProtocalUnit : IProtocalFormatting
|
public abstract class ProtocalUnit : IProtocalFormatting
|
||||||
{
|
{
|
||||||
|
protected static AddressTranslator _addressTranslator = new AddressTranslatorBase();
|
||||||
|
|
||||||
|
public ProtocalUnit SetAddressTranslator(AddressTranslator addressTranslator)
|
||||||
|
{
|
||||||
|
_addressTranslator = addressTranslator;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 格式化,将输入结构转换为字节数组
|
/// 格式化,将输入结构转换为字节数组
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -39,86 +47,7 @@ namespace ModBus.Net
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static byte[] TranslateContent(params object[] contents)
|
public static byte[] TranslateContent(params object[] contents)
|
||||||
{
|
{
|
||||||
bool b = false;
|
return ValueHelper.Instance.ObjectArrayToByteArray(contents);
|
||||||
//先查找传入的结构中有没有数组,有的话将其打开
|
|
||||||
var newContentsList = new List<object>();
|
|
||||||
foreach (object content in contents)
|
|
||||||
{
|
|
||||||
string t = content.GetType().ToString();
|
|
||||||
if (t.Substring(t.Length - 2, 2) == "[]")
|
|
||||||
{
|
|
||||||
b = true;
|
|
||||||
IEnumerable<object> contentArray =
|
|
||||||
ArrayList.Adapter((Array)content).ToArray(typeof(object)).OfType<object>();
|
|
||||||
newContentsList.AddRange(contentArray);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
newContentsList.Add(content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//重新调用一边这个函数,这个传入的参数中一定没有数组。
|
|
||||||
if (b) return TranslateContent(newContentsList.ToArray());
|
|
||||||
//把参数一个一个翻译为相对应的字节,然后拼成一个队列
|
|
||||||
var translateTarget = new List<byte>();
|
|
||||||
foreach (object content in contents)
|
|
||||||
{
|
|
||||||
string t = content.GetType().ToString();
|
|
||||||
switch (t)
|
|
||||||
{
|
|
||||||
case "System.Int16":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((short)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "System.Int32":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((int)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "System.Int64":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((long)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "System.UInt16":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((ushort)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "System.UInt32":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((uint)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "System.UInt64":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((ulong)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "System.Single":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((float)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "System.Double":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((double)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case "System.Byte":
|
|
||||||
{
|
|
||||||
translateTarget.AddRange(ValueHelper.Instance.GetBytes((byte)content));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
{
|
|
||||||
throw new NotImplementedException("没有实现除整数以外的其它转换方式");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//最后把队列转换为数组
|
|
||||||
return translateTarget.ToArray();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace ModBus.Net
|
namespace ModBus.Net
|
||||||
{
|
{
|
||||||
@@ -33,7 +36,7 @@ namespace ModBus.Net
|
|||||||
|
|
||||||
protected static ValueHelper _Instance = null;
|
protected static ValueHelper _Instance = null;
|
||||||
|
|
||||||
internal static ValueHelper Instance
|
public static ValueHelper Instance
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
{
|
{
|
||||||
@@ -154,6 +157,105 @@ namespace ModBus.Net
|
|||||||
pos += 8;
|
pos += 8;
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public byte[] ObjectArrayToByteArray(object[] contents)
|
||||||
|
{
|
||||||
|
bool b = false;
|
||||||
|
//先查找传入的结构中有没有数组,有的话将其打开
|
||||||
|
var newContentsList = new List<object>();
|
||||||
|
foreach (object content in contents)
|
||||||
|
{
|
||||||
|
string t = content.GetType().ToString();
|
||||||
|
if (t.Substring(t.Length - 2, 2) == "[]")
|
||||||
|
{
|
||||||
|
b = true;
|
||||||
|
IEnumerable<object> contentArray =
|
||||||
|
ArrayList.Adapter((Array)content).ToArray(typeof(object)).OfType<object>();
|
||||||
|
newContentsList.AddRange(contentArray);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
newContentsList.Add(content);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//重新调用一边这个函数,这个传入的参数中一定没有数组。
|
||||||
|
if (b) return ObjectArrayToByteArray(newContentsList.ToArray());
|
||||||
|
//把参数一个一个翻译为相对应的字节,然后拼成一个队列
|
||||||
|
var translateTarget = new List<byte>();
|
||||||
|
foreach (object content in contents)
|
||||||
|
{
|
||||||
|
string t = content.GetType().ToString();
|
||||||
|
switch (t)
|
||||||
|
{
|
||||||
|
case "System.Int16":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((short)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "System.Int32":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((int)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "System.Int64":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((long)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "System.UInt16":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((ushort)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "System.UInt32":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((uint)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "System.UInt64":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((ulong)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "System.Single":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((float)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "System.Double":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((double)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case "System.Byte":
|
||||||
|
{
|
||||||
|
translateTarget.AddRange(Instance.GetBytes((byte)content));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
throw new NotImplementedException("没有实现除整数以外的其它转换方式");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//最后把队列转换为数组
|
||||||
|
return translateTarget.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetBit(ushort number, int pos)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (pos < 0 && pos > 15) throw new IndexOutOfRangeException();
|
||||||
|
int ans = number % 2;
|
||||||
|
int i = 15;
|
||||||
|
while (i >= pos)
|
||||||
|
{
|
||||||
|
ans = number%2;
|
||||||
|
number /= 2;
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class LittleEndianValueHelper : ValueHelper
|
internal class LittleEndianValueHelper : ValueHelper
|
||||||
|
|||||||
Reference in New Issue
Block a user