Add GetBit (SubAddress and typeof(bool)) Support

This commit is contained in:
罗圣
2016-08-29 17:15:29 +08:00
parent c5e7f36e59
commit e1cb42a499
9 changed files with 106 additions and 66 deletions

View File

@@ -12,6 +12,11 @@ namespace Modbus.Net.Modbus
{
return area + " " + address;
}
public override string FormatAddress(string area, int address, int subAddress)
{
return area + " " + address + "." + subAddress;
}
}
public class AddressFormaterModbus : AddressFormater
@@ -20,5 +25,10 @@ namespace Modbus.Net.Modbus
{
return area + " " + address;
}
public override string FormatAddress(string area, int address, int subAddress)
{
return area + " " + address + "." + subAddress;
}
}
}

View File

@@ -60,16 +60,29 @@ namespace Modbus.Net.Modbus
string[] splitString = address.Split(' ');
string head = splitString[0];
string tail = splitString[1];
string sub;
if (tail.Contains('.'))
{
string[] splitString2 = tail.Split('.');
sub = splitString2[1];
tail = splitString2[0];
}
else
{
sub = "0";
}
return isRead
? new AddressDef()
{
Area = ReadFunctionCodeDictionary[head].Code,
Address = TransDictionary[head] + int.Parse(tail) - 1,
SubAddress = int.Parse(sub),
}
: new AddressDef()
{
Area = WriteFunctionCodeDictionary[head].Code,
Address = TransDictionary[head] + int.Parse(tail) - 1,
SubAddress = int.Parse(sub),
};
}
@@ -109,16 +122,29 @@ namespace Modbus.Net.Modbus
string[] splitString = address.Split(' ');
string head = splitString[0];
string tail = splitString[1];
string sub;
if (tail.Contains('.'))
{
string[] splitString2 = tail.Split('.');
sub = splitString2[1];
tail = splitString2[0];
}
else
{
sub = "0";
}
return isRead
? new AddressDef()
{
Area = ReadFunctionCodeDictionary[head].Code,
Address = int.Parse(tail) - 1,
SubAddress = int.Parse(sub),
}
: new AddressDef()
{
Area = WriteFunctionCodeDictionary[head].Code,
Address = int.Parse(tail) - 1,
SubAddress = int.Parse(sub),
};
}

View File

@@ -30,5 +30,10 @@ namespace Modbus.Net.OPC
ans = ans.Substring(0, ans.Length - 1);
return ans;
}
public override string FormatAddress(string area, int address, int subAddress)
{
return FormatAddress(area, address);
}
}
}

View File

@@ -10,18 +10,12 @@ namespace Modbus.Net.Siemens
{
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;
}
public override string FormatAddress(string area, int address, int subAddress)
{
return area + " " + address + "." + subAddress;
}
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace Modbus.Net.Siemens
{
@@ -28,54 +29,37 @@ namespace Modbus.Net.Siemens
public override AddressDef AddressTranslate(string address, bool isRead)
{
/*
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 head = splitString[0];
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")
{
head = head.Substring(2);
return new AddressDef()
{
Area = int.Parse(head)*256 + AreaCodeDictionary["DB"],
Address = int.Parse(tail)
Address = int.Parse(tail),
SubAddress = int.Parse(sub),
};
}
return
new AddressDef()
{
Area = AreaCodeDictionary[head],
Address = int.Parse(tail)
Address = int.Parse(tail),
SubAddress = int.Parse(sub),
};
}

View File

@@ -27,7 +27,7 @@ namespace Modbus.Net
public override IEnumerable<CommunicationUnit> Combine(IEnumerable<AddressUnit> 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
into grouped
select grouped;
@@ -41,18 +41,18 @@ namespace Modbus.Net
double getCount = 0;
double byteCount = 0;
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)
{
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);
byteCount = BigEndianValueHelper.Instance.ByteLength[address.DataType.FullName];
originalAddresses.Add(address);
}
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()
{
@@ -70,12 +70,12 @@ namespace Modbus.Net
}
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];
originalAddresses.Add(address);
}
}
preNum = address.Address;
preNum = address.Address + address.SubAddress * (0.125 / AddressTranslator.GetAreaByteLength(address.Area));
preType = address.DataType;
}
ans.Add(new CommunicationUnit()

View File

@@ -7,13 +7,15 @@ namespace Modbus.Net
/// </summary>
public abstract class AddressFormater
{
public abstract string FormatAddress(string area, int address);
/// <summary>
/// 编码地址
/// </summary>
/// <param name="area">地址所在的数据区域</param>
/// <param name="address">地址</param>
/// <param name="subAddress">子地址</param>
/// <returns>编码后的地址</returns>
public abstract string FormatAddress(string area, int address);
public abstract string FormatAddress(string area, int address, int subAddress);
}
/// <summary>
@@ -25,5 +27,10 @@ namespace Modbus.Net
{
return area + ":" + address;
}
public override string FormatAddress(string area, int address, int subAddress)
{
return area + ":" + address + ":" + subAddress;
}
}
}

View File

@@ -7,6 +7,7 @@ namespace Modbus.Net
{
public int Area { get; set; }
public int Address { get; set; }
public int SubAddress { get; set; }
}
public class AreaOutputDef
@@ -38,16 +39,30 @@ namespace Modbus.Net
{
public override AddressDef AddressTranslate(string address, bool isRead)
{
int num1,num2;
int num1,num2,num3;
string[] split = address.Split(':');
if (split.Length != 2) throw new FormatException();
if (int.TryParse(split[0], out num1) && int.TryParse(split[1], out num2))
if (split.Length == 2)
{
return new AddressDef()
if (int.TryParse(split[0], out num1) && int.TryParse(split[1], out num2))
{
Area = num1,
Address = num2
};
return new AddressDef()
{
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();
}

View File

@@ -163,7 +163,7 @@ namespace Modbus.Net
var datasReturn =
await
BaseUtility.GetDatasAsync(2, 0,
AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address),
AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address, 0),
(int)
Math.Ceiling(communicateAddress.GetCount*
BigEndianValueHelper.Instance.ByteLength[
@@ -181,7 +181,7 @@ namespace Modbus.Net
datas = datasReturn.ReturnValue;
//如果没有数据,终止
if (datas == null || datas.Length == 0 || datas.Length !=
if (datas == null || datas.Length == 0 || datas.Length <
(int)
Math.Ceiling(communicateAddress.GetCount *
BigEndianValueHelper.Instance.ByteLength[
@@ -191,8 +191,8 @@ namespace Modbus.Net
foreach (var address in communicateAddress.OriginalAddresses)
{
var localPos = ((address.Address - communicateAddress.Address)*8 + address.SubAddress)*
AddressTranslator.GetAreaByteLength(communicateAddress.Area)/8.0;
var localPos = (address.Address - communicateAddress.Address)*
AddressTranslator.GetAreaByteLength(communicateAddress.Area)+address.SubAddress/8.0;
var localMainPos = (int) localPos;
var localSubPos = (int) ((localPos - localMainPos)*8);
@@ -206,7 +206,7 @@ namespace Modbus.Net
}
case MachineGetDataType.Address:
{
key = AddressFormater.FormatAddress(address.Area, address.Address);
key = AddressFormater.FormatAddress(address.Area, address.Address, address.SubAddress);
break;
}
default:
@@ -231,13 +231,12 @@ namespace Modbus.Net
new ReturnUnit
{
PlcValue =
Double.Parse(
Convert.ToDouble(
datasReturn.IsLittleEndian
? ValueHelper.Instance.GetValue(datas, ref localMainPos, ref localSubPos, address.DataType)
.ToString()
: BigEndianValueHelper.Instance.GetValue(datas, ref localMainPos, ref localSubPos,
address.DataType)
.ToString()) * address.Zoom,
address.DataType)) * address.Zoom,
UnitExtend = address.UnitExtend
});
}
@@ -304,7 +303,7 @@ namespace Modbus.Net
case MachineSetDataType.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;
}
case MachineSetDataType.CommunicationTag: