Test Temp Update

This commit is contained in:
parallelbgls
2017-01-21 11:29:24 +08:00
parent 1e7e30bbcd
commit 1c89e5590e
5 changed files with 329 additions and 70 deletions

View File

@@ -6,8 +6,48 @@ using System.Threading.Tasks;
namespace Modbus.Net.Tests namespace Modbus.Net.Tests
{ {
public class AddressMaker public sealed class AddressMaker
{ {
public TestAreas TestAreas { get; set; }
public TestAddresses TestAddresses { get; set; }
private AddressFormater AddressFormater { get; set; }
private AddressTranslator AddressTranslator { get; set; }
public List<AddressUnit> MakeAddresses(string areaKey, string addressKey, bool isRead)
{
var combinedAddress = new List<string>();
foreach (var area in TestAreas[areaKey])
{
foreach (var address in TestAddresses[addressKey])
{
for (double currentAddress = address.Item1, i = 0;
i < address.Item2;
currentAddress +=
ValueHelper.Instance.ByteLength[address.Item3.FullName]/
AddressTranslator.GetAreaByteLength(area), i++)
{
combinedAddress.Add(AddressFormater.FormatAddress(area, (int) currentAddress, (int)
((currentAddress - (int) currentAddress)/
(ValueHelper.Instance.ByteLength[address.Item3.FullName]/
AddressTranslator.GetAreaByteLength(area)))));
}
}
}
return combinedAddress.Select(p =>
{
var translateAns = AddressTranslator.AddressTranslate(p, isRead);
return new AddressUnit()
{
Area = translateAns.AreaString,
Address = translateAns.Address,
SubAddress = translateAns.SubAddress,
Id = p,
CommunicationTag = p
};
}).ToList();
}
} }
} }

View File

@@ -1,27 +0,0 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Modbus.Net.Tests
{
[TestClass]
public class AddressTest
{
[TestMethod]
public void CombinerTest()
{
}
[TestMethod]
public void FormaterTest()
{
}
[TestMethod]
public void TranslatorTest()
{
}
}
}

View File

@@ -1,4 +1,5 @@
using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -8,9 +9,9 @@ namespace Modbus.Net.Tests
{ {
public sealed class TestAddresses public sealed class TestAddresses
{ {
private Dictionary<string, Tuple<int, int, int>[]> Addresses { get; } = new Dictionary<string, Tuple<int, int, int>[]>(); private Dictionary<string, Tuple<double, int, Type>[]> Addresses { get; }
public Tuple<int, int, int>[] this[string index] public Tuple<double, int, Type>[] this[string index]
{ {
get { return Addresses[index]; } get { return Addresses[index]; }
set set
@@ -25,11 +26,20 @@ namespace Modbus.Net.Tests
} }
} }
} }
public TestAddresses(Dictionary<string, Tuple<double, int, Type>[]> addresses)
{
Addresses = addresses.ToDictionary(address=>address.Key, address=>address.Value);
}
public IEnumerable<string> Keys => Addresses.Keys.AsEnumerable();
public IEnumerable<Tuple<double, int, Type>[]> Values => Addresses.Values.AsEnumerable();
} }
public sealed class TestAreas public sealed class TestAreas
{ {
private Dictionary<string, string[]> Areas { get; } = new Dictionary<string, string[]>(); private Dictionary<string, string[]> Areas { get; }
public string[] this[string index] public string[] this[string index]
{ {
@@ -46,5 +56,109 @@ namespace Modbus.Net.Tests
} }
} }
} }
public TestAreas(Dictionary<string, string[]> addresses)
{
Areas = addresses.ToDictionary(address => address.Key, address => address.Value);
}
public IEnumerable<string> Keys => Areas.Keys.AsEnumerable();
public IEnumerable<string[]> Values => Areas.Values.AsEnumerable();
}
public sealed class BaseTest
{
public static TestAreas TestAreasModbus => new TestAreas(new Dictionary<string, string[]>
{
{
"Coil",new []{"0X", "1X"}
},
{
"Register", new [] {"3X", "4X"}
}
});
public static TestAddresses TestAddresses => new TestAddresses(new Dictionary<string, Tuple<double, int, Type>[]>
{
{
"Coil.Single.Min", new []
{
new Tuple<double, int, Type>(0, 1, typeof(bool))
}
},
{
"Coil.Single.Normal", new[]
{
new Tuple<double, int, Type>(100, 1, typeof(bool))
}
},
{
"Coil.Single.MaxOverFlow", new[]
{
new Tuple<double, int, Type>(100000, 1, typeof(bool))
}
},
{
"Coil.Multi.Normal", new[]
{
new Tuple<double, int, Type>(0, 30, typeof(bool))
}
},
{
"Register.Single.Short", new[]
{
new Tuple<double, int, Type>(0, 1, typeof(ushort))
}
},
{
"Register.Continus.Short", new[]
{
new Tuple<double, int, Type>(0, 10, typeof(ushort))
}
},
{
"Register.Continus.Byte", new[]
{
new Tuple<double, int, Type>(0, 10, typeof(byte))
}
},
{
"Register.Continus.Int", new[]
{
new Tuple<double, int, Type>(0, 10, typeof(uint))
}
},
{
"Register.Continus.Bit", new []
{
new Tuple<double, int, Type>(0.5, 8, typeof(bool))
}
},
{
"Register.Duplicate.Short", new []
{
new Tuple<double, int, Type>(0, 10, typeof(ushort)),
new Tuple<double, int, Type>(15, 25, typeof(ushort)),
new Tuple<double, int, Type>(50, 20, typeof(ushort))
}
},
{
"Register.Cross.Short", new []
{
new Tuple<double, int, Type>(0, 10, typeof(ushort)),
new Tuple<double, int, Type>(5, 10, typeof(ushort)),
new Tuple<double, int, Type>(10, 10, typeof(ushort))
}
},
{
"Register.Duplicate.Multi", new []
{
new Tuple<double, int, Type>(0, 10, typeof(byte)),
new Tuple<double, int, Type>(20, 10, typeof(byte)),
new Tuple<double, int, Type>(30, 16, typeof(bool))
}
},
});
} }
} }

View File

@@ -1,62 +1,177 @@
using System; using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting; using Microsoft.VisualStudio.TestTools.UnitTesting;
using Modbus.Net.Modbus;
namespace Modbus.Net.Tests namespace Modbus.Net.Tests
{ {
[TestClass] [TestClass]
public class CommunicationTest public class CommunicationTest
{ {
public void CommunicationInvoke(string addressFullName, int count) protected AddressMaker AddressMaker = new AddressMaker()
{ {
TestAreas = BaseTest.TestAreasModbus,
TestAddresses = BaseTest.TestAddresses
};
protected BaseMachine Machine = new ModbusMachine(ModbusType.Tcp, "192.168.3.12", null, true, 2, 0);
[TestMethod]
public async Task CoilSingle()
{
var addresses = AddressMaker.MakeAddresses("Coil", "Coil.Single.Normal", false);
Machine.AddressCombiner = new AddressCombinerNumericJump(20, Machine.AddressTranslator);
Machine.GetAddresses = addresses;
var ans = await Machine.SetDatasAsync(MachineSetDataType.Address, new Dictionary<string, double>()
{
{
"0X 100", 1
},
{
"1X 100", 1
}
});
var addresses2 = AddressMaker.MakeAddresses("Coil", "Coil.Signle.Normal", true);
Machine.AddressCombiner = new AddressCombinerNumericJump(20, Machine.AddressTranslator);
Machine.GetAddresses = addresses2;
var ans2 = await Machine.GetDatasAsync(MachineGetDataType.Address);
Assert.AreEqual(ans2["0X 100"], 1);
Assert.AreEqual(ans2["1X 100"], 1);
} }
[TestMethod] [TestMethod]
public void CoilSingleRead() public async Task CoilMuiltiRead()
{ {
var addresses = AddressMaker.MakeAddresses("Coil", "Coil.Multi.Normal", false);
Machine.AddressCombiner = new AddressCombinerNumericJump(20, Machine.AddressTranslator);
Machine.GetAddresses = addresses;
var ans = await Machine.SetDatasAsync(MachineSetDataType.Address, new Dictionary<string, double>()
{
{
"0X 3", 1
},
{
"0X 4", 1
},
{
"0X 16", 1
},
{
"0X 22", 1
},
{
"1X 3", 1
},
{
"1X 4", 1
},
{
"1X 16", 1
},
{
"1X 22", 1
}
});
var addresses2 = AddressMaker.MakeAddresses("Coil", "Coil.Multi.Normal", true);
Machine.AddressCombiner = new AddressCombinerNumericJump(20, Machine.AddressTranslator);
Machine.GetAddresses = addresses;
var ans2 = await Machine.GetDatasAsync(MachineGetDataType.Address);
Assert.AreEqual(ans2["0X 3"], 1);
Assert.AreEqual(ans2["0X 4"], 1);
Assert.AreEqual(ans2["0X 16"], 1);
Assert.AreEqual(ans2["0X 22"], 1);
Assert.AreEqual(ans2["0X 7"], 0);
Assert.AreEqual(ans2["0X 29"], 0);
Assert.AreEqual(ans2["1X 3"], 1);
Assert.AreEqual(ans2["1X 4"], 1);
Assert.AreEqual(ans2["1X 16"], 1);
Assert.AreEqual(ans2["1X 22"], 1);
Assert.AreEqual(ans2["1X 7"], 0);
Assert.AreEqual(ans2["1X 29"], 0);
} }
[TestMethod] [TestMethod]
public void CoilMuiltiRead() public async Task RegisterSingleRead()
{ {
var addresses = AddressMaker.MakeAddresses("Register", "Register.Single.Short", false);
Machine.AddressCombiner = new AddressCombinerNumericJump(20, Machine.AddressTranslator);
Machine.GetAddresses = addresses;
var ans = await Machine.SetDatasAsync(MachineSetDataType.Address, new Dictionary<string, double>()
{
{
"3X 0", 32767
},
{
"4X 0", 32767
}
});
var addresses2 = AddressMaker.MakeAddresses("Register", "Register.Single.Short", true);
Machine.AddressCombiner = new AddressCombinerNumericJump(20, Machine.AddressTranslator);
Machine.GetAddresses = addresses;
var ans2 = await Machine.GetDatasAsync(MachineGetDataType.Address);
Assert.AreEqual(ans2["3X 0"], 32767);
Assert.AreEqual(ans2["4X 0"], 32767);
} }
[TestMethod] [TestMethod]
public void CoilSingleWrite() public async Task RegistertMultiRead()
{ {
var addresses = AddressMaker.MakeAddresses("Register", "Register.Duplicate.Short", false);
} Machine.AddressCombiner = new AddressCombinerNumericJump(20, Machine.AddressTranslator);
Machine.GetAddresses = addresses;
[TestMethod] var ans = await Machine.SetDatasAsync(MachineSetDataType.Address, new Dictionary<string, double>()
public void CoilMuiltiWrite()
{ {
}
[TestMethod]
public void RegisterSingleRead()
{ {
"3X 4", 17
} },
[TestMethod]
public void RegistertMultiRead()
{ {
"3X 7", 20
} },
[TestMethod]
public void RegisterSingleWrite()
{ {
"3X 17", 5255
} },
[TestMethod]
public void RegistertMultiWrite()
{ {
"3X 18", 3019
},
{
"3X 55", 192
},
{
"4X 4", 18
},
{
"4X 7", 21
},
{
"4X 17", 5256
},
{
"4X 18", 3020
},
{
"4X 55", 193
}
});
var addresses2 = AddressMaker.MakeAddresses("Register", "Register.Duplicate.Short", true);
Machine.AddressCombiner = new AddressCombinerNumericJump(20, Machine.AddressTranslator);
Machine.GetAddresses = addresses;
var ans2 = await Machine.GetDatasAsync(MachineGetDataType.Address);
Assert.AreEqual(ans2["3X 4"], 17);
Assert.AreEqual(ans2["3X 7"], 20);
Assert.AreEqual(ans2["3X 17"], 5255);
Assert.AreEqual(ans2["3X 18"], 3019);
Assert.AreEqual(ans2["3X 55"], 192);
Assert.AreEqual(ans2["4X 4"], 18);
Assert.AreEqual(ans2["4X 7"], 21);
Assert.AreEqual(ans2["4X 17"], 5256);
Assert.AreEqual(ans2["4X 18"], 3020);
Assert.AreEqual(ans2["4X 55"], 193);
}
} }
} }
}

View File

@@ -53,11 +53,28 @@
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="AddressMaker.cs" /> <Compile Include="AddressMaker.cs" />
<Compile Include="AddressTest.cs" />
<Compile Include="BaseTest.cs" /> <Compile Include="BaseTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="CommunicationTest.cs" /> <Compile Include="CommunicationTest.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Modbus.Net\Modbus.Net.Modbus\Modbus.Net.Modbus.csproj">
<Project>{fdca72ba-6d06-4de0-b873-c11c4ac853ad}</Project>
<Name>Modbus.Net.Modbus</Name>
</ProjectReference>
<ProjectReference Include="..\..\Modbus.Net\Modbus.Net.OPC\Modbus.Net.OPC.csproj">
<Project>{97f5a329-357a-4813-baae-58e71cc6fa87}</Project>
<Name>Modbus.Net.OPC</Name>
</ProjectReference>
<ProjectReference Include="..\..\Modbus.Net\Modbus.Net.Siemens\Modbus.Net.Siemens.csproj">
<Project>{6258f9d9-0df4-497f-9f3b-6d2f6f752a21}</Project>
<Name>Modbus.Net.Siemens</Name>
</ProjectReference>
<ProjectReference Include="..\..\Modbus.Net\ModBus.Net\Modbus.Net.csproj">
<Project>{124ebef2-8960-4447-84cf-1d683b1ef7cc}</Project>
<Name>Modbus.Net</Name>
</ProjectReference>
</ItemGroup>
<Choose> <Choose>
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'"> <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
<ItemGroup> <ItemGroup>