2015-07-10 update 1
This commit is contained in:
@@ -55,6 +55,7 @@ namespace ModBus.Net
|
|||||||
foreach (var communicateAddress in CommunicateAddresses)
|
foreach (var communicateAddress in CommunicateAddresses)
|
||||||
{
|
{
|
||||||
var datas = BaseUtility.GetDatas<byte>(2, 0, AddressFormater.FormatAddress(communicateAddress.Area,communicateAddress.Address), communicateAddress.GetCount);
|
var datas = BaseUtility.GetDatas<byte>(2, 0, AddressFormater.FormatAddress(communicateAddress.Area,communicateAddress.Address), communicateAddress.GetCount);
|
||||||
|
if (datas == null) return null;
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
while (pos < communicateAddress.GetCount)
|
while (pos < communicateAddress.GetCount)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -48,33 +48,57 @@ namespace ModBus.Net
|
|||||||
|
|
||||||
public virtual object[] GetDatas(byte belongAddress, byte masterAddress, string startAddress,
|
public virtual object[] GetDatas(byte belongAddress, byte masterAddress, string startAddress,
|
||||||
KeyValuePair<Type, int> getTypeAndCount)
|
KeyValuePair<Type, int> getTypeAndCount)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
string typeName = getTypeAndCount.Key.FullName;
|
string typeName = getTypeAndCount.Key.FullName;
|
||||||
double bCount = ValueHelper.Instance.ByteLength[typeName];
|
double bCount = ValueHelper.Instance.ByteLength[typeName];
|
||||||
byte[] getBytes = GetDatas(belongAddress, masterAddress, startAddress, (int)Math.Ceiling(bCount * getTypeAndCount.Value));
|
byte[] getBytes = GetDatas(belongAddress, masterAddress, startAddress,
|
||||||
|
(int) Math.Ceiling(bCount*getTypeAndCount.Value));
|
||||||
return ValueHelper.Instance.ByteArrayToObjectArray(getBytes, getTypeAndCount);
|
return ValueHelper.Instance.ByteArrayToObjectArray(getBytes, getTypeAndCount);
|
||||||
}
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public virtual T[] GetDatas<T>(byte belongAddress, byte masterAddress, string startAddress,
|
public virtual T[] GetDatas<T>(byte belongAddress, byte masterAddress, string startAddress,
|
||||||
int getByteCount)
|
int getByteCount)
|
||||||
{
|
{
|
||||||
var getBytes = GetDatas(belongAddress, masterAddress, startAddress, new KeyValuePair<Type, int>(typeof(T), getByteCount));
|
try
|
||||||
|
{
|
||||||
|
var getBytes = GetDatas(belongAddress, masterAddress, startAddress,
|
||||||
|
new KeyValuePair<Type, int>(typeof (T), getByteCount));
|
||||||
return ValueHelper.Instance.ObjectArrayToDestinationArray<T>(getBytes);
|
return ValueHelper.Instance.ObjectArrayToDestinationArray<T>(getBytes);
|
||||||
}
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public virtual object[] GetDatas(byte belongAddress, byte masterAddress, string startAddress,
|
public virtual object[] GetDatas(byte belongAddress, byte masterAddress, string startAddress,
|
||||||
IEnumerable<KeyValuePair<Type, int>> getTypeAndCountList)
|
IEnumerable<KeyValuePair<Type, int>> getTypeAndCountList)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
int bAllCount = 0;
|
int bAllCount = 0;
|
||||||
foreach (var getTypeAndCount in getTypeAndCountList)
|
foreach (var getTypeAndCount in getTypeAndCountList)
|
||||||
{
|
{
|
||||||
string typeName = getTypeAndCount.Key.FullName;
|
string typeName = getTypeAndCount.Key.FullName;
|
||||||
double bCount = ValueHelper.Instance.ByteLength[typeName];
|
double bCount = ValueHelper.Instance.ByteLength[typeName];
|
||||||
bAllCount += (int)Math.Ceiling(bCount*getTypeAndCount.Value);
|
bAllCount += (int)Math.Ceiling(bCount * getTypeAndCount.Value);
|
||||||
}
|
}
|
||||||
byte[] getBytes = GetDatas(belongAddress, masterAddress, startAddress, bAllCount);
|
byte[] getBytes = GetDatas(belongAddress, masterAddress, startAddress, bAllCount);
|
||||||
return ValueHelper.Instance.ByteArrayToObjectArray(getBytes, getTypeAndCountList);
|
return ValueHelper.Instance.ByteArrayToObjectArray(getBytes, getTypeAndCountList);
|
||||||
}
|
}
|
||||||
|
catch (Exception)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 设置数据
|
/// 设置数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -225,7 +225,7 @@ namespace ModBus.Net
|
|||||||
_timer.Elapsed += MaintainTasks;
|
_timer.Elapsed += MaintainTasks;
|
||||||
}
|
}
|
||||||
_timer.Start();
|
_timer.Start();
|
||||||
MaintainTasks(null,null);
|
//MaintainTasks(null,null);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -258,12 +258,15 @@ namespace ModBus.Net
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void AddMachines(IEnumerable<BaseMachine> machines)
|
public void AddMachines(IEnumerable<BaseMachine> machines)
|
||||||
|
{
|
||||||
|
lock (_machines)
|
||||||
{
|
{
|
||||||
foreach (var machine in machines)
|
foreach (var machine in machines)
|
||||||
{
|
{
|
||||||
AddMachine(machine);
|
AddMachine(machine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void RemoveMachineWithToken(string machineToken)
|
public void RemoveMachineWithToken(string machineToken)
|
||||||
{
|
{
|
||||||
@@ -289,14 +292,22 @@ namespace ModBus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void MaintainTasks(object sender, System.Timers.ElapsedEventArgs e)
|
private void MaintainTasks(object sender, System.Timers.ElapsedEventArgs e)
|
||||||
{
|
{
|
||||||
|
AsyncHelper.RunSync(MaintainTasksAsync);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task MaintainTasksAsync()
|
||||||
|
{
|
||||||
|
HashSet<BaseMachine> saveMachines = new HashSet<BaseMachine>();
|
||||||
lock (_machines)
|
lock (_machines)
|
||||||
{
|
{
|
||||||
foreach (var machine in _machines)
|
saveMachines.UnionWith(_machines);
|
||||||
{
|
|
||||||
RunTask(machine);
|
|
||||||
}
|
}
|
||||||
|
foreach (var machine in saveMachines)
|
||||||
|
{
|
||||||
|
await RunTask(machine);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -327,12 +338,12 @@ namespace ModBus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RunTask(BaseMachine machine)
|
private async Task RunTask(BaseMachine machine)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//var ans = machine.GetDatas();
|
//var ans = machine.GetDatas();
|
||||||
var ans = _tasks.StartNew(machine.GetDatas).Result;
|
var ans = await _tasks.StartNew(machine.GetDatas);
|
||||||
if (ReturnValues != null)
|
if (ReturnValues != null)
|
||||||
{
|
{
|
||||||
ReturnValues(new KeyValuePair<int, Dictionary<string,ReturnUnit>>(machine.Id, ans));
|
ReturnValues(new KeyValuePair<int, Dictionary<string,ReturnUnit>>(machine.Id, ans));
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Net;
|
||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace ModBus.Net
|
namespace ModBus.Net
|
||||||
@@ -61,7 +63,10 @@ namespace ModBus.Net
|
|||||||
|
|
||||||
public override bool IsConnected
|
public override bool IsConnected
|
||||||
{
|
{
|
||||||
get { return _socketClient != null && _socketClient.Connected; }
|
get
|
||||||
|
{
|
||||||
|
return _socketClient != null && _socketClient.Client != null && _socketClient.Connected;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
@@ -78,7 +83,59 @@ namespace ModBus.Net
|
|||||||
return AsyncHelper.RunSync(ConnectAsync);
|
return AsyncHelper.RunSync(ConnectAsync);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ManualResetEvent _timeoutObject = new ManualResetEvent(false);
|
||||||
|
private bool _isConnectionSuccessful;
|
||||||
public override async Task<bool> ConnectAsync()
|
public override async Task<bool> ConnectAsync()
|
||||||
|
{
|
||||||
|
_timeoutObject.Reset();
|
||||||
|
_socketClient = new TcpClient();
|
||||||
|
_socketClient.BeginConnect(_host, _port, new AsyncCallback(CallBackMethod), _socketClient);
|
||||||
|
if (_timeoutObject.WaitOne(TimeoutTime, false))
|
||||||
|
{
|
||||||
|
if (_isConnectionSuccessful)
|
||||||
|
{
|
||||||
|
AddInfo("client connected.");
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
AddInfo("connect failed.");
|
||||||
|
_socketClient = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_socketClient.Close();
|
||||||
|
AddInfo("connect failed.");
|
||||||
|
_socketClient = null;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void CallBackMethod(IAsyncResult asyncresult)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_isConnectionSuccessful = false;
|
||||||
|
TcpClient tcpclient = asyncresult.AsyncState as TcpClient;
|
||||||
|
if (tcpclient != null && tcpclient.Client != null)
|
||||||
|
{
|
||||||
|
_isConnectionSuccessful = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
AddInfo("client connected exception: " + ex.Message);
|
||||||
|
_isConnectionSuccessful = false;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
_timeoutObject.Set();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*public override async Task<bool> ConnectAsync()
|
||||||
{
|
{
|
||||||
if (_socketClient != null)
|
if (_socketClient != null)
|
||||||
{
|
{
|
||||||
@@ -88,6 +145,7 @@ namespace ModBus.Net
|
|||||||
{
|
{
|
||||||
_socketClient = new TcpClient
|
_socketClient = new TcpClient
|
||||||
{
|
{
|
||||||
|
SendTimeout = TimeoutTime,
|
||||||
ReceiveTimeout = TimeoutTime
|
ReceiveTimeout = TimeoutTime
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -113,7 +171,7 @@ namespace ModBus.Net
|
|||||||
AddInfo("client connect exception: " + err.Message);
|
AddInfo("client connect exception: " + err.Message);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
public override bool Disconnect()
|
public override bool Disconnect()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user