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

@@ -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),
};
}