Merge
This commit is contained in:
@@ -12,9 +12,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusAsciiInTcpProtocal(byte slaveAddress, byte masterAddress)
|
||||||
public ModbusAsciiInTcpProtocol(byte slaveAddress, byte masterAddress, Endian endian)
|
: this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress)
|
||||||
: this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,9 +23,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="ip">ip地址</param>
|
/// <param name="ip">ip地址</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusAsciiInTcpProtocal(string ip, byte slaveAddress, byte masterAddress)
|
||||||
public ModbusAsciiInTcpProtocol(string ip, byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
ProtocolLinker = new ModbusAsciiInTcpProtocolLinker(ip, slaveAddress);
|
ProtocolLinker = new ModbusAsciiInTcpProtocolLinker(ip, slaveAddress);
|
||||||
}
|
}
|
||||||
@@ -38,9 +36,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="port">端口</param>
|
/// <param name="port">端口</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusAsciiInTcpProtocal(string ip, int port, byte slaveAddress, byte masterAddress)
|
||||||
public ModbusAsciiInTcpProtocol(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
ProtocolLinker = new ModbusTcpProtocolLinker(ip, port);
|
ProtocolLinker = new ModbusTcpProtocolLinker(ip, port);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusAsciiProtocal(byte slaveAddress, byte masterAddress)
|
||||||
public ModbusAsciiProtocol(byte slaveAddress, byte masterAddress, Endian endian)
|
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress)
|
||||||
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,9 +23,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="com">串口地址</param>
|
/// <param name="com">串口地址</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusAsciiProtocal(string com, byte slaveAddress, byte masterAddress)
|
||||||
public ModbusAsciiProtocol(string com, byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
ProtocolLinker = new ModbusAsciiProtocolLinker(com, slaveAddress);
|
ProtocolLinker = new ModbusAsciiProtocolLinker(com, slaveAddress);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -98,9 +98,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="slaveAddress">从站地址</param>
|
/// <param name="slaveAddress">从站地址</param>
|
||||||
/// <param name="masterAddress">主站地址</param>
|
/// <param name="masterAddress">主站地址</param>
|
||||||
/// <param name="endian">端格式</param>
|
protected ModbusProtocal(byte slaveAddress, byte masterAddress)
|
||||||
protected ModbusProtocol(byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress, Endian.BigEndianLsb)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,9 +13,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusRtuInTcpProtocal(byte slaveAddress, byte masterAddress)
|
||||||
public ModbusRtuInTcpProtocol(byte slaveAddress, byte masterAddress, Endian endian)
|
: this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress)
|
||||||
: this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -25,9 +24,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="ip">ip地址</param>
|
/// <param name="ip">ip地址</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusRtuInTcpProtocal(string ip, byte slaveAddress, byte masterAddress)
|
||||||
public ModbusRtuInTcpProtocol(string ip, byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
ProtocolLinker = new ModbusTcpProtocolLinker(ip);
|
ProtocolLinker = new ModbusTcpProtocolLinker(ip);
|
||||||
}
|
}
|
||||||
@@ -39,9 +37,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="port">端口号</param>
|
/// <param name="port">端口号</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusRtuInTcpProtocal(string ip, int port, byte slaveAddress, byte masterAddress)
|
||||||
public ModbusRtuInTcpProtocol(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
ProtocolLinker = new ModbusRtuInTcpProtocolLinker(ip, port);
|
ProtocolLinker = new ModbusRtuInTcpProtocolLinker(ip, port);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusRtuProtocal(byte slaveAddress, byte masterAddress)
|
||||||
public ModbusRtuProtocol(byte slaveAddress, byte masterAddress, Endian endian)
|
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress)
|
||||||
: this(ConfigurationManager.AppSettings["COM"], slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,9 +23,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="com">串口</param>
|
/// <param name="com">串口</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusRtuProtocal(string com, byte slaveAddress, byte masterAddress)
|
||||||
public ModbusRtuProtocol(string com, byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
ProtocolLinker = new ModbusRtuProtocolLinker(com, slaveAddress);
|
ProtocolLinker = new ModbusRtuProtocolLinker(com, slaveAddress);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,9 +12,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusTcpProtocal(byte slaveAddress, byte masterAddress)
|
||||||
public ModbusTcpProtocol(byte slaveAddress, byte masterAddress, Endian endian)
|
: this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress)
|
||||||
: this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,9 +23,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="ip">ip地址</param>
|
/// <param name="ip">ip地址</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusTcpProtocal(string ip, byte slaveAddress, byte masterAddress)
|
||||||
public ModbusTcpProtocol(string ip, byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
ProtocolLinker = new ModbusTcpProtocolLinker(ip);
|
ProtocolLinker = new ModbusTcpProtocolLinker(ip);
|
||||||
}
|
}
|
||||||
@@ -38,9 +36,8 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="port">端口</param>
|
/// <param name="port">端口</param>
|
||||||
/// <param name="slaveAddress">从站号</param>
|
/// <param name="slaveAddress">从站号</param>
|
||||||
/// <param name="masterAddress">主站号</param>
|
/// <param name="masterAddress">主站号</param>
|
||||||
/// <param name="endian">端格式</param>
|
public ModbusTcpProtocal(string ip, int port, byte slaveAddress, byte masterAddress)
|
||||||
public ModbusTcpProtocol(string ip, int port, byte slaveAddress, byte masterAddress, Endian endian)
|
: base(slaveAddress, masterAddress)
|
||||||
: base(slaveAddress, masterAddress, endian)
|
|
||||||
{
|
{
|
||||||
ProtocolLinker = new ModbusTcpProtocolLinker(ip, port);
|
ProtocolLinker = new ModbusTcpProtocolLinker(ip, port);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ namespace Modbus.Net.Modbus
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 写单个单元的方法接口
|
/// 写单个数据方法接口
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public interface IUtilityMethodWriteSingle : IUtilityMethod
|
public interface IUtilityMethodWriteSingle : IUtilityMethod
|
||||||
{
|
{
|
||||||
@@ -148,18 +148,18 @@ namespace Modbus.Net.Modbus
|
|||||||
case ModbusType.Rtu:
|
case ModbusType.Rtu:
|
||||||
{
|
{
|
||||||
Wrapper = ConnectionString == null
|
Wrapper = ConnectionString == null
|
||||||
? new ModbusRtuProtocol(SlaveAddress, MasterAddress, Endian)
|
? new ModbusRtuProtocal(SlaveAddress, MasterAddress, Endian)
|
||||||
: new ModbusRtuProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian);
|
: new ModbusRtuProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//Tcp协议
|
//Tcp协议
|
||||||
case ModbusType.Tcp:
|
case ModbusType.Tcp:
|
||||||
{
|
{
|
||||||
Wrapper = ConnectionString == null
|
Wrapper = ConnectionString == null
|
||||||
? new ModbusTcpProtocol(SlaveAddress, MasterAddress, Endian)
|
? new ModbusTcpProtocal(SlaveAddress, MasterAddress, Endian)
|
||||||
: (ConnectionStringPort == null
|
: (ConnectionStringPort == null
|
||||||
? new ModbusTcpProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian)
|
? new ModbusTcpProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian)
|
||||||
: new ModbusTcpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress,
|
: new ModbusTcpProtocal(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress,
|
||||||
MasterAddress, Endian));
|
MasterAddress, Endian));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -167,18 +167,18 @@ namespace Modbus.Net.Modbus
|
|||||||
case ModbusType.Ascii:
|
case ModbusType.Ascii:
|
||||||
{
|
{
|
||||||
Wrapper = ConnectionString == null
|
Wrapper = ConnectionString == null
|
||||||
? new ModbusAsciiProtocol(SlaveAddress, MasterAddress, Endian)
|
? new ModbusAsciiProtocal(SlaveAddress, MasterAddress, Endian)
|
||||||
: new ModbusAsciiProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian);
|
: new ModbusAsciiProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//Rtu协议
|
//Rtu协议
|
||||||
case ModbusType.RtuInTcp:
|
case ModbusType.RtuInTcp:
|
||||||
{
|
{
|
||||||
Wrapper = ConnectionString == null
|
Wrapper = ConnectionString == null
|
||||||
? new ModbusRtuInTcpProtocol(SlaveAddress, MasterAddress, Endian)
|
? new ModbusRtuInTcpProtocal(SlaveAddress, MasterAddress, Endian)
|
||||||
: (ConnectionStringPort == null
|
: (ConnectionStringPort == null
|
||||||
? new ModbusRtuInTcpProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian)
|
? new ModbusRtuInTcpProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian)
|
||||||
: new ModbusRtuInTcpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress,
|
: new ModbusRtuInTcpProtocal(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress,
|
||||||
MasterAddress, Endian));
|
MasterAddress, Endian));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -186,10 +186,10 @@ namespace Modbus.Net.Modbus
|
|||||||
case ModbusType.AsciiInTcp:
|
case ModbusType.AsciiInTcp:
|
||||||
{
|
{
|
||||||
Wrapper = ConnectionString == null
|
Wrapper = ConnectionString == null
|
||||||
? new ModbusAsciiInTcpProtocol(SlaveAddress, MasterAddress, Endian)
|
? new ModbusAsciiInTcpProtocal(SlaveAddress, MasterAddress, Endian)
|
||||||
: (ConnectionStringPort == null
|
: (ConnectionStringPort == null
|
||||||
? new ModbusAsciiInTcpProtocol(ConnectionString, SlaveAddress, MasterAddress, Endian)
|
? new ModbusAsciiInTcpProtocal(ConnectionString, SlaveAddress, MasterAddress, Endian)
|
||||||
: new ModbusAsciiInTcpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress,
|
: new ModbusAsciiInTcpProtocal(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress,
|
||||||
MasterAddress, Endian));
|
MasterAddress, Endian));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
60
Tests/Modbus.Net.Tests/EndianTest.cs
Normal file
60
Tests/Modbus.Net.Tests/EndianTest.cs
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||||
|
using Modbus.Net.Modbus;
|
||||||
|
|
||||||
|
namespace Modbus.Net.Tests
|
||||||
|
{
|
||||||
|
[TestClass]
|
||||||
|
public class EndianTest
|
||||||
|
{
|
||||||
|
private BaseMachine _modbusTcpMachine;
|
||||||
|
|
||||||
|
private BaseMachine _modbusTcpMachine2;
|
||||||
|
|
||||||
|
[TestInitialize]
|
||||||
|
public void Init()
|
||||||
|
{
|
||||||
|
_modbusTcpMachine = new ModbusMachine(ModbusType.Tcp, "127.0.0.1", null, true, 1, 0);
|
||||||
|
|
||||||
|
_modbusTcpMachine2 = new ModbusMachine(ModbusType.Tcp, "127.0.0.1", null, true, 1, 0, Endian.LittleEndianLsb);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestMethod]
|
||||||
|
public async Task ModbusEndianSingle()
|
||||||
|
{
|
||||||
|
Random r = new Random();
|
||||||
|
|
||||||
|
var addresses = new List<AddressUnit>
|
||||||
|
{
|
||||||
|
new AddressUnit
|
||||||
|
{
|
||||||
|
Id = "0",
|
||||||
|
Area = "4X",
|
||||||
|
Address = 1,
|
||||||
|
SubAddress = 0,
|
||||||
|
CommunicationTag = "A1",
|
||||||
|
DataType = typeof(ushort)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
var dic1 = new Dictionary<string, double>()
|
||||||
|
{
|
||||||
|
{
|
||||||
|
"4X 1", r.Next(0, UInt16.MaxValue)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
_modbusTcpMachine.GetAddresses = addresses;
|
||||||
|
_modbusTcpMachine2.GetAddresses = addresses;
|
||||||
|
await _modbusTcpMachine.SetDatasAsync(MachineSetDataType.Address, dic1);
|
||||||
|
var ans = await _modbusTcpMachine.GetDatasAsync(MachineGetDataType.Address);
|
||||||
|
var ans2 = await _modbusTcpMachine2.GetDatasAsync(MachineGetDataType.Address);
|
||||||
|
Assert.AreEqual(ans["4X 1.0"].PlcValue, dic1["4X 1"]);
|
||||||
|
Assert.AreEqual(ans2["4X 1.0"].PlcValue, (ushort)dic1["4X 1"] % 256 * 256 + (ushort)dic1["4X 1"] / 256);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -52,6 +52,7 @@
|
|||||||
</Otherwise>
|
</Otherwise>
|
||||||
</Choose>
|
</Choose>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Compile Include="EndianTest.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="ModbusTest.cs" />
|
<Compile Include="ModbusTest.cs" />
|
||||||
<Compile Include="SiemensTest.cs" />
|
<Compile Include="SiemensTest.cs" />
|
||||||
|
|||||||
Reference in New Issue
Block a user