2015-04-09 update 2
This commit is contained in:
@@ -83,5 +83,17 @@ namespace ModBus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 协议连接开始
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public abstract bool Connect();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 协议连接断开
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public abstract bool Disconnect();
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -73,10 +73,12 @@
|
|||||||
<Compile Include="ModbusTcpProtocal.cs" />
|
<Compile Include="ModbusTcpProtocal.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="SimenseProtocal.cs" />
|
<Compile Include="SimenseProtocal.cs" />
|
||||||
|
<Compile Include="SimenseStructDefinition.cs" />
|
||||||
<Compile Include="SimenseTcpProtocal.cs" />
|
<Compile Include="SimenseTcpProtocal.cs" />
|
||||||
<Compile Include="SimenseProtocalLinkerBytesExtend.cs" />
|
<Compile Include="SimenseProtocalLinkerBytesExtend.cs" />
|
||||||
<Compile Include="SimenseTcpProtocalLinker.cs" />
|
<Compile Include="SimenseTcpProtocalLinker.cs" />
|
||||||
<Compile Include="SimenseUtility.cs" />
|
<Compile Include="SimenseUtility.cs" />
|
||||||
|
<Compile Include="TaskManager.cs" />
|
||||||
<Compile Include="TcpConnector.cs">
|
<Compile Include="TcpConnector.cs">
|
||||||
<SubType>Code</SubType>
|
<SubType>Code</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
@@ -41,7 +41,15 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
public abstract class ModbusProtocal : BaseProtocal
|
public abstract class ModbusProtocal : BaseProtocal
|
||||||
{
|
{
|
||||||
|
public override bool Connect()
|
||||||
|
{
|
||||||
|
return ProtocalLinker.Connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Disconnect()
|
||||||
|
{
|
||||||
|
return ProtocalLinker.Disconnect();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#region 读PLC数据
|
#region 读PLC数据
|
||||||
|
|||||||
@@ -14,6 +14,17 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
get { return _baseConnector.IsConnected; }
|
get { return _baseConnector.IsConnected; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool Connect()
|
||||||
|
{
|
||||||
|
return _baseConnector.Connect();
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool Disconnect()
|
||||||
|
{
|
||||||
|
return _baseConnector.Disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送并接收数据
|
/// 发送并接收数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
96
Modbus.Net/ModBus.Net/SimenseStructDefinition.cs
Normal file
96
Modbus.Net/ModBus.Net/SimenseStructDefinition.cs
Normal file
@@ -0,0 +1,96 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace ModBus.Net
|
||||||
|
{
|
||||||
|
public struct TodClockStatus
|
||||||
|
{
|
||||||
|
public bool KV
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var pos = 15;
|
||||||
|
return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos);
|
||||||
|
}
|
||||||
|
set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 15, value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte K0_4
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var pos = 0;
|
||||||
|
var byteValue = ValueHelper.Instance.GetByte(ValueHelper.Instance.GetBytes(TodValue), ref pos);
|
||||||
|
return (byte)(byteValue%64/4);
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
var pos = 0;
|
||||||
|
var byteValue = ValueHelper.Instance.GetByte(ValueHelper.Instance.GetBytes(TodValue), ref pos);
|
||||||
|
byteValue = (byte)(byteValue - (byteValue%128/4) + value);
|
||||||
|
TodValue = (ushort)(TodValue%128 + byteValue*128);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ZNA
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var pos = 5;
|
||||||
|
return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos);
|
||||||
|
}
|
||||||
|
set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 5, value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte UA
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var pos = 3;
|
||||||
|
var low = ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos) ? 1 : 0;
|
||||||
|
var high = ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos) ? 1 : 0;
|
||||||
|
high *= 2;
|
||||||
|
return (byte) (high + low);
|
||||||
|
}
|
||||||
|
set
|
||||||
|
{
|
||||||
|
TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 3, value % 2 >= 1);
|
||||||
|
TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 4, value / 2 >= 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public bool UZS
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var pos = 2;
|
||||||
|
return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos);
|
||||||
|
}
|
||||||
|
set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 2, value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool ESY
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var pos = 1;
|
||||||
|
return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos);
|
||||||
|
}
|
||||||
|
set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 1, value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool SYA
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var pos = 0;
|
||||||
|
return ValueHelper.Instance.GetBit(ValueHelper.Instance.GetBytes(TodValue), ref pos);
|
||||||
|
}
|
||||||
|
set { TodValue = ValueHelper.Instance.SetBit(ValueHelper.Instance.GetBytes(TodValue), 0, value); }
|
||||||
|
}
|
||||||
|
|
||||||
|
public ushort TodValue { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -32,14 +32,13 @@ namespace ModBus.Net
|
|||||||
_tdpuSize = tdpuSize;
|
_tdpuSize = tdpuSize;
|
||||||
_ip = ip;
|
_ip = ip;
|
||||||
connectTryCount = 0;
|
connectTryCount = 0;
|
||||||
Connected();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override byte[] SendReceive(params object[] content)
|
public override byte[] SendReceive(params object[] content)
|
||||||
{
|
{
|
||||||
while (!ProtocalLinker.IsConnected)
|
while (!ProtocalLinker.IsConnected)
|
||||||
{
|
{
|
||||||
Connected();
|
Connect();
|
||||||
}
|
}
|
||||||
return base.SendReceive(content);
|
return base.SendReceive(content);
|
||||||
}
|
}
|
||||||
@@ -49,7 +48,7 @@ namespace ModBus.Net
|
|||||||
if (!ProtocalLinker.IsConnected)
|
if (!ProtocalLinker.IsConnected)
|
||||||
{
|
{
|
||||||
if (connectTryCount > 10) return null;
|
if (connectTryCount > 10) return null;
|
||||||
Connected();
|
Connect();
|
||||||
}
|
}
|
||||||
return base.SendReceive(unit, content);
|
return base.SendReceive(unit, content);
|
||||||
}
|
}
|
||||||
@@ -59,16 +58,29 @@ namespace ModBus.Net
|
|||||||
return base.SendReceive(unit, content);
|
return base.SendReceive(unit, content);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void Connected()
|
public override bool Connect()
|
||||||
{
|
{
|
||||||
connectTryCount++;
|
connectTryCount++;
|
||||||
ProtocalLinker = new SimenseTcpProtocalLinker(_ip);
|
ProtocalLinker = new SimenseTcpProtocalLinker(_ip);
|
||||||
var inputStruct = new CreateReferenceSimenseInputStruct(_tdpuSize, _taspSrc, _tsapDst);
|
if (ProtocalLinker.Connect())
|
||||||
var outputStruct =
|
{
|
||||||
(CreateReferenceSimenseOutputStruct)ForceSendReceive(this[typeof(CreateReferenceSimenseProtocal)], inputStruct);
|
var inputStruct = new CreateReferenceSimenseInputStruct(_tdpuSize, _taspSrc, _tsapDst);
|
||||||
if (!ProtocalLinker.IsConnected) return;
|
var outputStruct =
|
||||||
var inputStruct2 = new EstablishAssociationSimenseInputStruct(0x0101, _maxCalling, _maxCalled, _maxPdu);
|
(CreateReferenceSimenseOutputStruct)
|
||||||
var outputStruct2 = (EstablishAssociationSimenseOutputStruct)SendReceive(this[typeof(EstablishAssociationSimenseProtocal)], inputStruct2);
|
ForceSendReceive(this[typeof (CreateReferenceSimenseProtocal)], inputStruct);
|
||||||
|
if (!ProtocalLinker.IsConnected) return false;
|
||||||
|
var inputStruct2 = new EstablishAssociationSimenseInputStruct(0x0101, _maxCalling, _maxCalled, _maxPdu);
|
||||||
|
var outputStruct2 =
|
||||||
|
(EstablishAssociationSimenseOutputStruct)
|
||||||
|
SendReceive(this[typeof (EstablishAssociationSimenseProtocal)], inputStruct2);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override bool Disconnect()
|
||||||
|
{
|
||||||
|
return ProtocalLinker.Disconnect();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ namespace ModBus.Net
|
|||||||
|
|
||||||
protected TcpProtocalLinker(string ip, int port)
|
protected TcpProtocalLinker(string ip, int port)
|
||||||
{
|
{
|
||||||
_baseConnector = new TcpConnector(ip, port, 10000);
|
_baseConnector = new TcpConnector(ip, port, 30);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -46,7 +46,7 @@ namespace ModBus.Net
|
|||||||
|
|
||||||
#region Factory
|
#region Factory
|
||||||
|
|
||||||
protected static ValueHelper _Instance = null;
|
protected static ValueHelper _Instance;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// ValueHelper单例的实例
|
/// ValueHelper单例的实例
|
||||||
|
|||||||
Reference in New Issue
Block a user