Add serilog
This commit is contained in:
@@ -56,6 +56,7 @@
|
|||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="1.1.2" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="1.1.2" />
|
<PackageReference Include="Microsoft.Extensions.Configuration.Xml" Version="1.1.2" />
|
||||||
<PackageReference Include="Nito.AsyncEx.Tasks" Version="1.1.0" />
|
<PackageReference Include="Nito.AsyncEx.Tasks" Version="1.1.0" />
|
||||||
|
<PackageReference Include="Serilog" Version="2.4.0" />
|
||||||
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
|
<PackageReference Include="System.Collections.NonGeneric" Version="4.3.0" />
|
||||||
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
|
<PackageReference Include="System.Threading.ThreadPool" Version="4.3.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -23,10 +23,6 @@
|
|||||||
<DocumentationFile>bin\Debug\net45\Modbus.Net.Modbus.xml</DocumentationFile>
|
<DocumentationFile>bin\Debug\net45\Modbus.Net.Modbus.xml</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.Bcl.Build" Version="1.0.21" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ModBus.Net\Modbus.Net.csproj" />
|
<ProjectReference Include="..\ModBus.Net\Modbus.Net.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -25,7 +25,6 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="H.Opc" Version="0.9.0" />
|
<PackageReference Include="H.Opc" Version="0.9.0" />
|
||||||
<PackageReference Include="Microsoft.Bcl.Build" Version="1.0.21" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
@@ -22,10 +22,6 @@
|
|||||||
<DocumentationFile>bin\Debug\net45\Modbus.Net.Siemens.xml</DocumentationFile>
|
<DocumentationFile>bin\Debug\net45\Modbus.Net.Siemens.xml</DocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<PackageReference Include="Microsoft.Bcl.Build" Version="1.0.21" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\ModBus.Net\Modbus.Net.csproj" />
|
<ProjectReference Include="..\ModBus.Net\Modbus.Net.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ using System.Linq;
|
|||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Modbus.Net
|
namespace Modbus.Net
|
||||||
{
|
{
|
||||||
@@ -65,6 +66,10 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private bool m_disposed;
|
private bool m_disposed;
|
||||||
|
|
||||||
|
private int _sendCount;
|
||||||
|
private int _receiveCount;
|
||||||
|
private int _errorCount;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造器
|
/// 构造器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -135,31 +140,26 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="readBuf">串口数据缓冲 </param>
|
/// <param name="readBuf">串口数据缓冲 </param>
|
||||||
/// <param name="bufRoom">串口数据缓冲空间大小 </param>
|
/// <param name="bufRoom">串口数据缓冲空间大小 </param>
|
||||||
/// <param name="HowTime">设置串口读放弃时间 </param>
|
/// <param name="howTime">设置串口读放弃时间 </param>
|
||||||
/// <param name="ByteTime">字节间隔最大时间 </param>
|
/// <param name="byteTime">字节间隔最大时间 </param>
|
||||||
/// <returns>串口实际读入数据个数 </returns>
|
/// <returns>串口实际读入数据个数 </returns>
|
||||||
public int ReadComm(out byte[] readBuf, int bufRoom, int HowTime, int ByteTime)
|
public int ReadComm(out byte[] readBuf, int bufRoom, int howTime, int byteTime)
|
||||||
{
|
{
|
||||||
//throw new System.NotImplementedException();
|
|
||||||
readBuf = new byte[1023];
|
readBuf = new byte[1023];
|
||||||
Array.Clear(readBuf, 0, readBuf.Length);
|
Array.Clear(readBuf, 0, readBuf.Length);
|
||||||
|
|
||||||
int nReadLen, nBytelen;
|
|
||||||
if (SerialPort.IsOpen == false)
|
if (SerialPort.IsOpen == false)
|
||||||
return -1;
|
return -1;
|
||||||
nBytelen = 0;
|
var nBytelen = 0;
|
||||||
SerialPort.ReadTimeout = HowTime;
|
SerialPort.ReadTimeout = howTime;
|
||||||
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
while (SerialPort.BytesToRead > 0)
|
while (SerialPort.BytesToRead > 0)
|
||||||
{
|
{
|
||||||
readBuf[nBytelen] = (byte) SerialPort.ReadByte();
|
readBuf[nBytelen] = (byte) SerialPort.ReadByte();
|
||||||
var bTmp = new byte[bufRoom];
|
var bTmp = new byte[bufRoom];
|
||||||
Array.Clear(bTmp, 0, bTmp.Length);
|
Array.Clear(bTmp, 0, bTmp.Length);
|
||||||
|
|
||||||
nReadLen = ReadBlock(bTmp, bufRoom, ByteTime);
|
var nReadLen = ReadBlock(bTmp, bufRoom, byteTime);
|
||||||
|
|
||||||
if (nReadLen > 0)
|
if (nReadLen > 0)
|
||||||
{
|
{
|
||||||
@@ -172,11 +172,6 @@ namespace Modbus.Net
|
|||||||
nBytelen += 1;
|
nBytelen += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new Exception(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return nBytelen;
|
return nBytelen;
|
||||||
}
|
}
|
||||||
@@ -184,31 +179,23 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 串口同步读(阻塞方式读串口,直到串口缓冲区中没有数据,靠字符间间隔超时确定没有数据)
|
/// 串口同步读(阻塞方式读串口,直到串口缓冲区中没有数据,靠字符间间隔超时确定没有数据)
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="ReadBuf">串口数据缓冲 </param>
|
/// <param name="readBuf">串口数据缓冲 </param>
|
||||||
/// <param name="ReadRoom">串口数据缓冲空间大小 </param>
|
/// <param name="readRoom">串口数据缓冲空间大小 </param>
|
||||||
/// <param name="ByteTime">字节间隔最大时间 </param>
|
/// <param name="byteTime">字节间隔最大时间 </param>
|
||||||
/// <returns>从串口实际读入的字节个数 </returns>
|
/// <returns>从串口实际读入的字节个数 </returns>
|
||||||
public int ReadBlock(byte[] ReadBuf, int ReadRoom, int ByteTime)
|
public int ReadBlock(byte[] readBuf, int readRoom, int byteTime)
|
||||||
{
|
{
|
||||||
sbyte nBytelen;
|
|
||||||
//long nByteRead;
|
|
||||||
|
|
||||||
if (SerialPort.IsOpen == false)
|
if (SerialPort.IsOpen == false)
|
||||||
return 0;
|
return 0;
|
||||||
nBytelen = 0;
|
sbyte nBytelen = 0;
|
||||||
SerialPort.ReadTimeout = ByteTime;
|
SerialPort.ReadTimeout = byteTime;
|
||||||
|
|
||||||
while (nBytelen < ReadRoom - 1 && SerialPort.BytesToRead > 0)
|
while (nBytelen < readRoom - 1 && SerialPort.BytesToRead > 0)
|
||||||
try
|
|
||||||
{
|
{
|
||||||
ReadBuf[nBytelen] = (byte) SerialPort.ReadByte();
|
readBuf[nBytelen] = (byte) SerialPort.ReadByte();
|
||||||
nBytelen++; // add one
|
nBytelen++; // add one
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
readBuf[nBytelen] = 0x00;
|
||||||
{
|
|
||||||
throw new Exception(ex.Message);
|
|
||||||
}
|
|
||||||
ReadBuf[nBytelen] = 0x00;
|
|
||||||
return nBytelen;
|
return nBytelen;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -216,72 +203,63 @@ namespace Modbus.Net
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// 字符数组转字符串16进制
|
/// 字符数组转字符串16进制
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="InBytes"> 二进制字节 </param>
|
/// <param name="inBytes"> 二进制字节 </param>
|
||||||
/// <returns>类似"01 02 0F" </returns>
|
/// <returns>类似"01 02 0F" </returns>
|
||||||
public static string ByteToString(byte[] InBytes)
|
public static string ByteToString(byte[] inBytes)
|
||||||
{
|
{
|
||||||
var StringOut = "";
|
var stringOut = "";
|
||||||
foreach (var InByte in InBytes)
|
foreach (var inByte in inBytes)
|
||||||
StringOut = StringOut + string.Format("{0:X2}", InByte) + " ";
|
stringOut = stringOut + $"{inByte:X2}" + " ";
|
||||||
|
|
||||||
return StringOut.Trim();
|
return stringOut.Trim();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// strhex 转字节数组
|
/// strhex 转字节数组
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="InString">类似"01 02 0F" 用空格分开的 </param>
|
/// <param name="inString">类似"01 02 0F" 用空格分开的 </param>
|
||||||
/// <returns> </returns>
|
/// <returns> </returns>
|
||||||
public static byte[] StringToByte(string InString)
|
public static byte[] StringToByte(string inString)
|
||||||
{
|
{
|
||||||
string[] ByteStrings;
|
var byteStrings = inString.Split(" ".ToCharArray());
|
||||||
ByteStrings = InString.Split(" ".ToCharArray());
|
var byteOut = new byte[byteStrings.Length];
|
||||||
byte[] ByteOut;
|
for (var i = 0; i <= byteStrings.Length - 1; i++)
|
||||||
ByteOut = new byte[ByteStrings.Length];
|
byteOut[i] = byte.Parse(byteStrings[i], NumberStyles.HexNumber);
|
||||||
for (var i = 0; i <= ByteStrings.Length - 1; i++)
|
return byteOut;
|
||||||
ByteOut[i] = byte.Parse(ByteStrings[i], NumberStyles.HexNumber);
|
|
||||||
return ByteOut;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// strhex 转字节数组
|
/// strhex 转字节数组
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="InString">类似"01 02 0F" 中间无空格 </param>
|
/// <param name="inString">类似"01 02 0F" 中间无空格 </param>
|
||||||
/// <returns> </returns>
|
/// <returns> </returns>
|
||||||
public static byte[] StringToByte_2(string InString)
|
public static byte[] StringToByte_2(string inString)
|
||||||
{
|
{
|
||||||
byte[] ByteOut;
|
inString = inString.Replace(" ", "");
|
||||||
InString = InString.Replace(" ", "");
|
|
||||||
try
|
var byteStrings = new string[inString.Length / 2];
|
||||||
{
|
|
||||||
var ByteStrings = new string[InString.Length / 2];
|
|
||||||
var j = 0;
|
var j = 0;
|
||||||
for (var i = 0; i < ByteStrings.Length; i++)
|
for (var i = 0; i < byteStrings.Length; i++)
|
||||||
{
|
{
|
||||||
ByteStrings[i] = InString.Substring(j, 2);
|
byteStrings[i] = inString.Substring(j, 2);
|
||||||
j += 2;
|
j += 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteOut = new byte[ByteStrings.Length];
|
var byteOut = new byte[byteStrings.Length];
|
||||||
for (var i = 0; i <= ByteStrings.Length - 1; i++)
|
for (var i = 0; i <= byteStrings.Length - 1; i++)
|
||||||
ByteOut[i] = byte.Parse(ByteStrings[i], NumberStyles.HexNumber);
|
byteOut[i] = byte.Parse(byteStrings[i], NumberStyles.HexNumber);
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new Exception(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
return ByteOut;
|
return byteOut;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 字符串 转16进制字符串
|
/// 字符串 转16进制字符串
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="InString">unico </param>
|
/// <param name="inString">unico </param>
|
||||||
/// <returns>类似“01 0f” </returns>
|
/// <returns>类似“01 0f” </returns>
|
||||||
public static string Str_To_0X(string InString)
|
public static string Str_To_0X(string inString)
|
||||||
{
|
{
|
||||||
return ByteToString(Encoding.Default.GetBytes(InString));
|
return ByteToString(Encoding.Default.GetBytes(inString));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -310,10 +288,12 @@ namespace Modbus.Net
|
|||||||
//ignore
|
//ignore
|
||||||
}
|
}
|
||||||
SerialPort.Dispose();
|
SerialPort.Dispose();
|
||||||
|
Log.Information("Com interface {Com} Disposed", _com);
|
||||||
Connectors[_com] = null;
|
Connectors[_com] = null;
|
||||||
Connectors.Remove(_com);
|
Connectors.Remove(_com);
|
||||||
}
|
}
|
||||||
Linkers.Remove(_slave);
|
Linkers.Remove(_slave);
|
||||||
|
Log.Information("Com connector {ConnectionToken} Removed", ConnectionToken);
|
||||||
}
|
}
|
||||||
m_disposed = true;
|
m_disposed = true;
|
||||||
}
|
}
|
||||||
@@ -364,10 +344,12 @@ namespace Modbus.Net
|
|||||||
if (!Linkers.ContainsKey(_slave))
|
if (!Linkers.ContainsKey(_slave))
|
||||||
Linkers.Add(_slave, _com);
|
Linkers.Add(_slave, _com);
|
||||||
SerialPort.Open();
|
SerialPort.Open();
|
||||||
|
Log.Information("Com client {ConnectionToken} connect success", ConnectionToken);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
Log.Error(e, "Com client {ConnectionToken} connect error", ConnectionToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -391,12 +373,15 @@ namespace Modbus.Net
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
Dispose();
|
Dispose();
|
||||||
|
Log.Information("Com client {ConnectionToken} disconnect success", ConnectionToken);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
Log.Error(e, "Com client {ConnectionToken} disconnect error", ConnectionToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
Log.Error(new Exception("Linkers or Connectors Dictionary not found"), "Com client {ConnectionToken} disconnect error", ConnectionToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -438,19 +423,48 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
SerialPort.Open();
|
SerialPort.Open();
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
|
Log.Error(err, "Com client {ConnectionToken} open error", ConnectionToken);
|
||||||
Dispose();
|
Dispose();
|
||||||
SerialPort.Open();
|
SerialPort.Open();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
byte[] returnBytes;
|
||||||
|
|
||||||
lock (SerialPort.Lock)
|
lock (SerialPort.Lock)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Log.Verbose("Com client {ConnectionToken} send msg length: {Length}", ConnectionToken, sendbytes.Length);
|
||||||
|
Log.Verbose("Com client {ConnectionToken} send msg: {SendBytes}", ConnectionToken, sendbytes);
|
||||||
SerialPort.Write(sendbytes, 0, sendbytes.Length);
|
SerialPort.Write(sendbytes, 0, sendbytes.Length);
|
||||||
}
|
}
|
||||||
return ReadMsg();
|
catch (Exception err)
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
{
|
||||||
|
Log.Error(err, "Com client {ConnectionToken} send msg error", ConnectionToken);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
RefreshSendCount();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
returnBytes = ReadMsg();
|
||||||
|
Log.Verbose("Com client {ConnectionToken} receive msg length: {Length}", ConnectionToken, returnBytes.Length);
|
||||||
|
Log.Verbose("Com client {ConnectionToken} receive msg: {SendBytes}", ConnectionToken, returnBytes);
|
||||||
|
}
|
||||||
|
catch (Exception e)
|
||||||
|
{
|
||||||
|
Log.Error(e, "Com client {ConnectionToken} read msg error", ConnectionToken);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
RefreshReceiveCount();
|
||||||
|
}
|
||||||
|
return returnBytes;
|
||||||
|
}
|
||||||
|
catch (Exception err)
|
||||||
|
{
|
||||||
|
Log.Error(err, "Com client {ConnectionToken} read error", ConnectionToken);
|
||||||
Dispose();
|
Dispose();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -480,19 +494,33 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
SerialPort.Open();
|
SerialPort.Open();
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
|
Log.Error(err, "Com client {ConnectionToken} open error", ConnectionToken);
|
||||||
Dispose();
|
Dispose();
|
||||||
SerialPort.Open();
|
SerialPort.Open();
|
||||||
}
|
}
|
||||||
lock (SerialPort.Lock)
|
lock (SerialPort.Lock)
|
||||||
{
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Log.Verbose("Com client {ConnectionToken} send msg length: {Length}", ConnectionToken, sendbytes.Length);
|
||||||
|
Log.Verbose("Com client {ConnectionToken} send msg: {SendBytes}", ConnectionToken, sendbytes);
|
||||||
SerialPort.Write(sendbytes, 0, sendbytes.Length);
|
SerialPort.Write(sendbytes, 0, sendbytes.Length);
|
||||||
}
|
}
|
||||||
|
catch (Exception err)
|
||||||
|
{
|
||||||
|
Log.Error(err, "Com client {ConnectionToken} send msg error", ConnectionToken);
|
||||||
|
Dispose();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
RefreshSendCount();
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
|
Log.Error(err, "Com client {ConnectionToken} reopen error", ConnectionToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -511,13 +539,33 @@ namespace Modbus.Net
|
|||||||
Array.Copy(data, 0, returndata, 0, i);
|
Array.Copy(data, 0, returndata, 0, i);
|
||||||
return returndata;
|
return returndata;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
|
Log.Error(e, "Com client {ConnectionToken} read error", ConnectionToken);
|
||||||
|
RefreshErrorCount();
|
||||||
Dispose();
|
Dispose();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
private void RefreshSendCount()
|
||||||
|
{
|
||||||
|
_sendCount++;
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} send count: {SendCount}", ConnectionToken, _sendCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RefreshReceiveCount()
|
||||||
|
{
|
||||||
|
_receiveCount++;
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} receive count: {SendCount}", ConnectionToken, _receiveCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void RefreshErrorCount()
|
||||||
|
{
|
||||||
|
_errorCount++;
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} error count: {ErrorCount}", ConnectionToken, _errorCount);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -52,6 +52,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Nito.AsyncEx" Version="4.0.1" />
|
<PackageReference Include="Nito.AsyncEx" Version="4.0.1" />
|
||||||
|
<PackageReference Include="Serilog" Version="2.4.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -60,4 +61,10 @@
|
|||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Update="ComConnector.cs">
|
||||||
|
<SubType>Component</SubType>
|
||||||
|
</Compile>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Net.Sockets;
|
using System.Net.Sockets;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
using Serilog;
|
||||||
|
|
||||||
namespace Modbus.Net
|
namespace Modbus.Net
|
||||||
{
|
{
|
||||||
@@ -34,12 +35,15 @@ namespace Modbus.Net
|
|||||||
private readonly string _host;
|
private readonly string _host;
|
||||||
private readonly int _port;
|
private readonly int _port;
|
||||||
|
|
||||||
// 2MB 的接收缓冲区,目的是一次接收完服务器发回的消息
|
/// <summary>
|
||||||
|
/// 1MB 的接收缓冲区
|
||||||
|
/// </summary>
|
||||||
private readonly byte[] _receiveBuffer = new byte[1024];
|
private readonly byte[] _receiveBuffer = new byte[1024];
|
||||||
private int _errorCount;
|
|
||||||
private int _receiveCount;
|
|
||||||
|
|
||||||
private int _sendCount;
|
private int _sendCount;
|
||||||
|
private int _receiveCount;
|
||||||
|
private int _errorCount;
|
||||||
|
|
||||||
private TcpClient _socketClient;
|
private TcpClient _socketClient;
|
||||||
|
|
||||||
private int _timeoutTime;
|
private int _timeoutTime;
|
||||||
@@ -69,7 +73,7 @@ namespace Modbus.Net
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public int TimeoutTime
|
public int TimeoutTime
|
||||||
{
|
{
|
||||||
get { return _timeoutTime; }
|
get => _timeoutTime;
|
||||||
set
|
set
|
||||||
{
|
{
|
||||||
_timeoutTime = value;
|
_timeoutTime = value;
|
||||||
@@ -111,7 +115,12 @@ namespace Modbus.Net
|
|||||||
if (_socketClient != null)
|
if (_socketClient != null)
|
||||||
{
|
{
|
||||||
CloseClientSocket();
|
CloseClientSocket();
|
||||||
_socketClient.Client.Dispose();
|
#if NET40 || NET45 || NET451 || NET452
|
||||||
|
_socketClient.Close();
|
||||||
|
#else
|
||||||
|
_socketClient.Dispose();
|
||||||
|
#endif
|
||||||
|
Log.Debug("Tcp client {ConnectionToken} Disposed", ConnectionToken);
|
||||||
}
|
}
|
||||||
m_disposed = true;
|
m_disposed = true;
|
||||||
}
|
}
|
||||||
@@ -159,19 +168,19 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
AddInfo("client connected exception: " + e.Message);
|
Log.Error(e, "Tcp client {ConnectionToken} connect error", ConnectionToken);
|
||||||
}
|
}
|
||||||
if (_socketClient.Connected)
|
if (_socketClient.Connected)
|
||||||
{
|
{
|
||||||
AddInfo("client connected.");
|
Log.Information("Tcp client {ConnectionToken} connected", ConnectionToken);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
AddInfo("connect failed.");
|
Log.Error("Tcp client {ConnectionToken} connect failed.", ConnectionToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
AddInfo("client connect exception: " + err.Message);
|
Log.Error(err, "Tcp client {ConnectionToken} connect exception", ConnectionToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -187,17 +196,13 @@ namespace Modbus.Net
|
|||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
#if NET40 || NET45 || NET451 || NET452
|
Dispose();
|
||||||
_socketClient.Close();
|
Log.Information("Tcp client {ConnectionToken} disconnected successfully", ConnectionToken);
|
||||||
#else
|
|
||||||
_socketClient.Dispose();
|
|
||||||
#endif
|
|
||||||
AddInfo("client disconnected successfully.");
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
AddInfo("client disconnected exception: " + err.Message);
|
Log.Error(err, "Tcp client {ConnectionToken} disconnected exception", ConnectionToken);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
finally
|
finally
|
||||||
@@ -206,11 +211,6 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AddInfo(string message)
|
|
||||||
{
|
|
||||||
Console.WriteLine(message);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送数据,不需要返回任何值
|
/// 发送数据,不需要返回任何值
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -236,15 +236,18 @@ namespace Modbus.Net
|
|||||||
await ConnectAsync();
|
await ConnectAsync();
|
||||||
|
|
||||||
var stream = _socketClient.GetStream();
|
var stream = _socketClient.GetStream();
|
||||||
|
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} send text len = {Length}", ConnectionToken, datagram.Length);
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} send text = {Datagram}", ConnectionToken, datagram);
|
||||||
await stream.WriteAsync(datagram, 0, datagram.Length);
|
await stream.WriteAsync(datagram, 0, datagram.Length);
|
||||||
|
|
||||||
RefreshSendCount();
|
RefreshSendCount();
|
||||||
//this.AddInfo("send text len = " + datagramText.Length.ToString());
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
AddInfo("send exception: " + err.Message);
|
Log.Error(err, "Tcp client {ConnectionToken} send exception", ConnectionToken);
|
||||||
CloseClientSocket();
|
CloseClientSocket();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -275,16 +278,22 @@ namespace Modbus.Net
|
|||||||
await ConnectAsync();
|
await ConnectAsync();
|
||||||
|
|
||||||
var stream = _socketClient.GetStream();
|
var stream = _socketClient.GetStream();
|
||||||
|
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} send text len = {Length}", ConnectionToken, datagram.Length);
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} send: {Datagram}", ConnectionToken, datagram);
|
||||||
await stream.WriteAsync(datagram, 0, datagram.Length);
|
await stream.WriteAsync(datagram, 0, datagram.Length);
|
||||||
|
|
||||||
RefreshSendCount();
|
RefreshSendCount();
|
||||||
//this.AddInfo("send text len = " + datagramText.Length.ToString());
|
|
||||||
|
|
||||||
return await ReceiveAsync(stream);
|
var receiveBytes = await ReceiveAsync(stream);
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} receive text len = {Length}", ConnectionToken, receiveBytes.Length);
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} receive: {Datagram}", ConnectionToken, receiveBytes);
|
||||||
|
|
||||||
|
return receiveBytes;
|
||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
AddInfo("send exception: " + err.Message);
|
Log.Error(err, "Tcp client {ConnectionToken} send exception", ConnectionToken);
|
||||||
CloseClientSocket();
|
CloseClientSocket();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -308,7 +317,7 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
catch (Exception err)
|
catch (Exception err)
|
||||||
{
|
{
|
||||||
AddInfo("receive exception: " + err.Message);
|
Log.Error(err, "Tcp client {ConnectionToken} receive exception", ConnectionToken);
|
||||||
CloseClientSocket();
|
CloseClientSocket();
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -323,7 +332,7 @@ namespace Modbus.Net
|
|||||||
var replyMessage = new byte[len];
|
var replyMessage = new byte[len];
|
||||||
Array.Copy(_receiveBuffer, replyMessage, len);
|
Array.Copy(_receiveBuffer, replyMessage, len);
|
||||||
|
|
||||||
//this.AddInfo("reply: " + replyMesage);
|
Log.Verbose("Tcp client {ConnectionToken} reply: {replyMessage}",ConnectionToken, replyMessage);
|
||||||
RefreshReceiveCount();
|
RefreshReceiveCount();
|
||||||
|
|
||||||
if (len <= 0)
|
if (len <= 0)
|
||||||
@@ -335,16 +344,19 @@ namespace Modbus.Net
|
|||||||
private void RefreshSendCount()
|
private void RefreshSendCount()
|
||||||
{
|
{
|
||||||
_sendCount++;
|
_sendCount++;
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} send count: {SendCount}", ConnectionToken, _sendCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RefreshReceiveCount()
|
private void RefreshReceiveCount()
|
||||||
{
|
{
|
||||||
_receiveCount++;
|
_receiveCount++;
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} receive count: {SendCount}", ConnectionToken, _receiveCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void RefreshErrorCount()
|
private void RefreshErrorCount()
|
||||||
{
|
{
|
||||||
_errorCount++;
|
_errorCount++;
|
||||||
|
Log.Verbose("Tcp client {ConnectionToken} error count: {ErrorCount}", ConnectionToken, _errorCount);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void CloseClientSocket()
|
private void CloseClientSocket()
|
||||||
@@ -358,7 +370,7 @@ namespace Modbus.Net
|
|||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
AddInfo("client close exception: " + ex.Message);
|
Log.Error(ex, "Tcp client {ConnectionToken} client close exception", ConnectionToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user