Add GetBit (SubAddress and typeof(bool)) Support
This commit is contained in:
@@ -12,6 +12,11 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
return area + " " + address;
|
return area + " " + address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string FormatAddress(string area, int address, int subAddress)
|
||||||
|
{
|
||||||
|
return area + " " + address + "." + subAddress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AddressFormaterModbus : AddressFormater
|
public class AddressFormaterModbus : AddressFormater
|
||||||
@@ -20,5 +25,10 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
return area + " " + address;
|
return area + " " + address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string FormatAddress(string area, int address, int subAddress)
|
||||||
|
{
|
||||||
|
return area + " " + address + "." + subAddress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,16 +60,29 @@ namespace Modbus.Net.Modbus
|
|||||||
string[] splitString = address.Split(' ');
|
string[] splitString = address.Split(' ');
|
||||||
string head = splitString[0];
|
string head = splitString[0];
|
||||||
string tail = splitString[1];
|
string tail = splitString[1];
|
||||||
|
string sub;
|
||||||
|
if (tail.Contains('.'))
|
||||||
|
{
|
||||||
|
string[] splitString2 = tail.Split('.');
|
||||||
|
sub = splitString2[1];
|
||||||
|
tail = splitString2[0];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sub = "0";
|
||||||
|
}
|
||||||
return isRead
|
return isRead
|
||||||
? new AddressDef()
|
? new AddressDef()
|
||||||
{
|
{
|
||||||
Area = ReadFunctionCodeDictionary[head].Code,
|
Area = ReadFunctionCodeDictionary[head].Code,
|
||||||
Address = TransDictionary[head] + int.Parse(tail) - 1,
|
Address = TransDictionary[head] + int.Parse(tail) - 1,
|
||||||
|
SubAddress = int.Parse(sub),
|
||||||
}
|
}
|
||||||
: new AddressDef()
|
: new AddressDef()
|
||||||
{
|
{
|
||||||
Area = WriteFunctionCodeDictionary[head].Code,
|
Area = WriteFunctionCodeDictionary[head].Code,
|
||||||
Address = TransDictionary[head] + int.Parse(tail) - 1,
|
Address = TransDictionary[head] + int.Parse(tail) - 1,
|
||||||
|
SubAddress = int.Parse(sub),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -109,16 +122,29 @@ namespace Modbus.Net.Modbus
|
|||||||
string[] splitString = address.Split(' ');
|
string[] splitString = address.Split(' ');
|
||||||
string head = splitString[0];
|
string head = splitString[0];
|
||||||
string tail = splitString[1];
|
string tail = splitString[1];
|
||||||
|
string sub;
|
||||||
|
if (tail.Contains('.'))
|
||||||
|
{
|
||||||
|
string[] splitString2 = tail.Split('.');
|
||||||
|
sub = splitString2[1];
|
||||||
|
tail = splitString2[0];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sub = "0";
|
||||||
|
}
|
||||||
return isRead
|
return isRead
|
||||||
? new AddressDef()
|
? new AddressDef()
|
||||||
{
|
{
|
||||||
Area = ReadFunctionCodeDictionary[head].Code,
|
Area = ReadFunctionCodeDictionary[head].Code,
|
||||||
Address = int.Parse(tail) - 1,
|
Address = int.Parse(tail) - 1,
|
||||||
|
SubAddress = int.Parse(sub),
|
||||||
}
|
}
|
||||||
: new AddressDef()
|
: new AddressDef()
|
||||||
{
|
{
|
||||||
Area = WriteFunctionCodeDictionary[head].Code,
|
Area = WriteFunctionCodeDictionary[head].Code,
|
||||||
Address = int.Parse(tail) - 1,
|
Address = int.Parse(tail) - 1,
|
||||||
|
SubAddress = int.Parse(sub),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -30,5 +30,10 @@ namespace Modbus.Net.OPC
|
|||||||
ans = ans.Substring(0, ans.Length - 1);
|
ans = ans.Substring(0, ans.Length - 1);
|
||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string FormatAddress(string area, int address, int subAddress)
|
||||||
|
{
|
||||||
|
return FormatAddress(area, address);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,18 +10,12 @@ namespace Modbus.Net.Siemens
|
|||||||
{
|
{
|
||||||
public override string FormatAddress(string area, int address)
|
public override string FormatAddress(string area, int address)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
if (area.Length > 1 &&
|
|
||||||
area.ToUpper().Substring(0, 2) == "DB")
|
|
||||||
{
|
|
||||||
return area.ToUpper() + "." + "DB" + address;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return area.ToUpper() + address;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
return area + " " + address;
|
return area + " " + address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string FormatAddress(string area, int address, int subAddress)
|
||||||
|
{
|
||||||
|
return area + " " + address + "." + subAddress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
|
||||||
namespace Modbus.Net.Siemens
|
namespace Modbus.Net.Siemens
|
||||||
{
|
{
|
||||||
@@ -28,54 +29,37 @@ namespace Modbus.Net.Siemens
|
|||||||
|
|
||||||
public override AddressDef AddressTranslate(string address, bool isRead)
|
public override AddressDef AddressTranslate(string address, bool isRead)
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
address = address.ToUpper();
|
address = address.ToUpper();
|
||||||
if (address.Substring(0, 2) == "DB")
|
|
||||||
{
|
|
||||||
var addressSplit = address.Split('.');
|
|
||||||
if (addressSplit.Length != 2) throw new FormatException();
|
|
||||||
addressSplit[0] = addressSplit[0].Substring(2);
|
|
||||||
if (addressSplit[1].Substring(0, 2) == "DB")
|
|
||||||
addressSplit[1] = addressSplit[1].Substring(2);
|
|
||||||
return new AddressDef()
|
|
||||||
{
|
|
||||||
Area = int.Parse(addressSplit[0])*256 + AreaCodeDictionary["DB"],
|
|
||||||
Address = int.Parse(addressSplit[1])
|
|
||||||
};
|
|
||||||
}
|
|
||||||
int i = 0;
|
|
||||||
int t;
|
|
||||||
while (!int.TryParse(address[i].ToString(), out t) && i < address.Length)
|
|
||||||
{
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
if (i == 0 || i >= address.Length) throw new FormatException();
|
|
||||||
string head = address.Substring(0, i);
|
|
||||||
string tail = address.Substring(i);
|
|
||||||
return
|
|
||||||
new AddressDef()
|
|
||||||
{
|
|
||||||
Area = AreaCodeDictionary[head],
|
|
||||||
Address = int.Parse(tail)
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
string[] splitString = address.Split(' ');
|
string[] splitString = address.Split(' ');
|
||||||
string head = splitString[0];
|
string head = splitString[0];
|
||||||
string tail = splitString[1];
|
string tail = splitString[1];
|
||||||
|
string sub;
|
||||||
|
if (tail.Contains('.'))
|
||||||
|
{
|
||||||
|
string[] splitString2 = tail.Split('.');
|
||||||
|
sub = splitString2[1];
|
||||||
|
tail = splitString2[0];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
sub = "0";
|
||||||
|
}
|
||||||
if (head.Length > 1 && head.Substring(0, 2) == "DB")
|
if (head.Length > 1 && head.Substring(0, 2) == "DB")
|
||||||
{
|
{
|
||||||
head = head.Substring(2);
|
head = head.Substring(2);
|
||||||
return new AddressDef()
|
return new AddressDef()
|
||||||
{
|
{
|
||||||
Area = int.Parse(head)*256 + AreaCodeDictionary["DB"],
|
Area = int.Parse(head)*256 + AreaCodeDictionary["DB"],
|
||||||
Address = int.Parse(tail)
|
Address = int.Parse(tail),
|
||||||
|
SubAddress = int.Parse(sub),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
new AddressDef()
|
new AddressDef()
|
||||||
{
|
{
|
||||||
Area = AreaCodeDictionary[head],
|
Area = AreaCodeDictionary[head],
|
||||||
Address = int.Parse(tail)
|
Address = int.Parse(tail),
|
||||||
|
SubAddress = int.Parse(sub),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ namespace Modbus.Net
|
|||||||
public override IEnumerable<CommunicationUnit> Combine(IEnumerable<AddressUnit> addresses)
|
public override IEnumerable<CommunicationUnit> Combine(IEnumerable<AddressUnit> addresses)
|
||||||
{
|
{
|
||||||
var groupedAddresses = from address in addresses
|
var groupedAddresses = from address in addresses
|
||||||
orderby address.Address
|
orderby address.Address + address.SubAddress * (0.125 / AddressTranslator.GetAreaByteLength(address.Area))
|
||||||
group address by address.Area
|
group address by address.Area
|
||||||
into grouped
|
into grouped
|
||||||
select grouped;
|
select grouped;
|
||||||
@@ -41,18 +41,18 @@ namespace Modbus.Net
|
|||||||
double getCount = 0;
|
double getCount = 0;
|
||||||
double byteCount = 0;
|
double byteCount = 0;
|
||||||
List<AddressUnit> originalAddresses = new List<AddressUnit>();
|
List<AddressUnit> originalAddresses = new List<AddressUnit>();
|
||||||
foreach (var address in groupedAddress.OrderBy(address => address.Address))
|
foreach (var address in groupedAddress.OrderBy(address => address.Address + address.SubAddress * (0.125 / AddressTranslator.GetAreaByteLength(address.Area))))
|
||||||
{
|
{
|
||||||
if (initNum < 0)
|
if (initNum < 0)
|
||||||
{
|
{
|
||||||
initNum = address.Address + (address.SubAddress + 1) * 0.125;
|
initNum = address.Address + address.SubAddress * (0.125 / AddressTranslator.GetAreaByteLength(address.Area));
|
||||||
getCount = BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName] / AddressTranslator.GetAreaByteLength(address.Area);
|
getCount = BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName] / AddressTranslator.GetAreaByteLength(address.Area);
|
||||||
byteCount = BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName];
|
byteCount = BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName];
|
||||||
originalAddresses.Add(address);
|
originalAddresses.Add(address);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (address.Address > preNum + (int) (BigEndianValueHelper.Instance.ByteLength[preType.FullName] / AddressTranslator.GetAreaByteLength(address.Area)))
|
if (address.Address + address.SubAddress * (0.125 / AddressTranslator.GetAreaByteLength(address.Area)) > preNum + BigEndianValueHelper.Instance.ByteLength[preType.FullName] / AddressTranslator.GetAreaByteLength(address.Area))
|
||||||
{
|
{
|
||||||
ans.Add(new CommunicationUnit()
|
ans.Add(new CommunicationUnit()
|
||||||
{
|
{
|
||||||
@@ -70,12 +70,12 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
getCount += BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName]/ AddressTranslator.GetAreaByteLength(address.Area);
|
getCount += BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName] / AddressTranslator.GetAreaByteLength(address.Area);
|
||||||
byteCount += BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName];
|
byteCount += BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName];
|
||||||
originalAddresses.Add(address);
|
originalAddresses.Add(address);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
preNum = address.Address;
|
preNum = address.Address + address.SubAddress * (0.125 / AddressTranslator.GetAreaByteLength(address.Area));
|
||||||
preType = address.DataType;
|
preType = address.DataType;
|
||||||
}
|
}
|
||||||
ans.Add(new CommunicationUnit()
|
ans.Add(new CommunicationUnit()
|
||||||
|
|||||||
@@ -7,13 +7,15 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public abstract class AddressFormater
|
public abstract class AddressFormater
|
||||||
{
|
{
|
||||||
|
public abstract string FormatAddress(string area, int address);
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 编码地址
|
/// 编码地址
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="area">地址所在的数据区域</param>
|
/// <param name="area">地址所在的数据区域</param>
|
||||||
/// <param name="address">地址</param>
|
/// <param name="address">地址</param>
|
||||||
|
/// <param name="subAddress">子地址</param>
|
||||||
/// <returns>编码后的地址</returns>
|
/// <returns>编码后的地址</returns>
|
||||||
public abstract string FormatAddress(string area, int address);
|
public abstract string FormatAddress(string area, int address, int subAddress);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -25,5 +27,10 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
return area + ":" + address;
|
return area + ":" + address;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string FormatAddress(string area, int address, int subAddress)
|
||||||
|
{
|
||||||
|
return area + ":" + address + ":" + subAddress;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
public int Area { get; set; }
|
public int Area { get; set; }
|
||||||
public int Address { get; set; }
|
public int Address { get; set; }
|
||||||
|
public int SubAddress { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class AreaOutputDef
|
public class AreaOutputDef
|
||||||
@@ -38,16 +39,30 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
public override AddressDef AddressTranslate(string address, bool isRead)
|
public override AddressDef AddressTranslate(string address, bool isRead)
|
||||||
{
|
{
|
||||||
int num1,num2;
|
int num1,num2,num3;
|
||||||
string[] split = address.Split(':');
|
string[] split = address.Split(':');
|
||||||
if (split.Length != 2) throw new FormatException();
|
if (split.Length == 2)
|
||||||
if (int.TryParse(split[0], out num1) && int.TryParse(split[1], out num2))
|
|
||||||
{
|
{
|
||||||
return new AddressDef()
|
if (int.TryParse(split[0], out num1) && int.TryParse(split[1], out num2))
|
||||||
{
|
{
|
||||||
Area = num1,
|
return new AddressDef()
|
||||||
Address = num2
|
{
|
||||||
};
|
Area = num1,
|
||||||
|
Address = num2
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (split.Length == 3)
|
||||||
|
{
|
||||||
|
if (int.TryParse(split[0], out num1) && int.TryParse(split[1], out num2) && int.TryParse(split[3], out num3))
|
||||||
|
{
|
||||||
|
return new AddressDef()
|
||||||
|
{
|
||||||
|
Area = num1,
|
||||||
|
Address = num2,
|
||||||
|
SubAddress = num3,
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
throw new FormatException();
|
throw new FormatException();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -163,7 +163,7 @@ namespace Modbus.Net
|
|||||||
var datasReturn =
|
var datasReturn =
|
||||||
await
|
await
|
||||||
BaseUtility.GetDatasAsync(2, 0,
|
BaseUtility.GetDatasAsync(2, 0,
|
||||||
AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address),
|
AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address, 0),
|
||||||
(int)
|
(int)
|
||||||
Math.Ceiling(communicateAddress.GetCount*
|
Math.Ceiling(communicateAddress.GetCount*
|
||||||
BigEndianValueHelper.Instance.ByteLength[
|
BigEndianValueHelper.Instance.ByteLength[
|
||||||
@@ -181,7 +181,7 @@ namespace Modbus.Net
|
|||||||
datas = datasReturn.ReturnValue;
|
datas = datasReturn.ReturnValue;
|
||||||
|
|
||||||
//如果没有数据,终止
|
//如果没有数据,终止
|
||||||
if (datas == null || datas.Length == 0 || datas.Length !=
|
if (datas == null || datas.Length == 0 || datas.Length <
|
||||||
(int)
|
(int)
|
||||||
Math.Ceiling(communicateAddress.GetCount *
|
Math.Ceiling(communicateAddress.GetCount *
|
||||||
BigEndianValueHelper.Instance.ByteLength[
|
BigEndianValueHelper.Instance.ByteLength[
|
||||||
@@ -191,8 +191,8 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
foreach (var address in communicateAddress.OriginalAddresses)
|
foreach (var address in communicateAddress.OriginalAddresses)
|
||||||
{
|
{
|
||||||
var localPos = ((address.Address - communicateAddress.Address)*8 + address.SubAddress)*
|
var localPos = (address.Address - communicateAddress.Address)*
|
||||||
AddressTranslator.GetAreaByteLength(communicateAddress.Area)/8.0;
|
AddressTranslator.GetAreaByteLength(communicateAddress.Area)+address.SubAddress/8.0;
|
||||||
var localMainPos = (int) localPos;
|
var localMainPos = (int) localPos;
|
||||||
var localSubPos = (int) ((localPos - localMainPos)*8);
|
var localSubPos = (int) ((localPos - localMainPos)*8);
|
||||||
|
|
||||||
@@ -206,7 +206,7 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
case MachineGetDataType.Address:
|
case MachineGetDataType.Address:
|
||||||
{
|
{
|
||||||
key = AddressFormater.FormatAddress(address.Area, address.Address);
|
key = AddressFormater.FormatAddress(address.Area, address.Address, address.SubAddress);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -231,13 +231,12 @@ namespace Modbus.Net
|
|||||||
new ReturnUnit
|
new ReturnUnit
|
||||||
{
|
{
|
||||||
PlcValue =
|
PlcValue =
|
||||||
Double.Parse(
|
Convert.ToDouble(
|
||||||
datasReturn.IsLittleEndian
|
datasReturn.IsLittleEndian
|
||||||
? ValueHelper.Instance.GetValue(datas, ref localMainPos, ref localSubPos, address.DataType)
|
? ValueHelper.Instance.GetValue(datas, ref localMainPos, ref localSubPos, address.DataType)
|
||||||
.ToString()
|
.ToString()
|
||||||
: BigEndianValueHelper.Instance.GetValue(datas, ref localMainPos, ref localSubPos,
|
: BigEndianValueHelper.Instance.GetValue(datas, ref localMainPos, ref localSubPos,
|
||||||
address.DataType)
|
address.DataType)) * address.Zoom,
|
||||||
.ToString()) * address.Zoom,
|
|
||||||
UnitExtend = address.UnitExtend
|
UnitExtend = address.UnitExtend
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -304,7 +303,7 @@ namespace Modbus.Net
|
|||||||
case MachineSetDataType.Address:
|
case MachineSetDataType.Address:
|
||||||
{
|
{
|
||||||
address =
|
address =
|
||||||
GetAddresses.SingleOrDefault(p => AddressFormater.FormatAddress(p.Area, p.Address) == value.Key);
|
GetAddresses.SingleOrDefault(p => AddressFormater.FormatAddress(p.Area, p.Address, p.SubAddress) == value.Key || (p.DataType != typeof(bool) && AddressFormater.FormatAddress(p.Area, p.Address) == value.Key));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case MachineSetDataType.CommunicationTag:
|
case MachineSetDataType.CommunicationTag:
|
||||||
|
|||||||
Reference in New Issue
Block a user