优化虚拟驱动
This commit is contained in:
parent
d4646de379
commit
0c3d187405
@ -1,36 +1,32 @@
|
|||||||
using PluginInterface;
|
using PluginInterface;
|
||||||
using SimpleTCP;
|
using SimpleTCP;
|
||||||
using System;
|
|
||||||
using System.Text;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
namespace DriverSimTcpClient
|
namespace DriverSimTcpClient
|
||||||
{
|
{
|
||||||
[DriverSupported("SimTcpServerDevice")]
|
[DriverSupported("SimTcpServerDevice")]
|
||||||
[DriverInfoAttribute("SimTcpClient", "V1.0.0", "Copyright IoTGateway© 2022-06-04")]
|
[DriverInfo("SimTcpClient", "V1.0.0", "Copyright IoTGateway© 2022-06-04")]
|
||||||
public class SimTcpClient : IDriver
|
public class SimTcpClient : IDriver
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// tcp客户端
|
/// tcp客户端
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private SimpleTcpClient? client;
|
private SimpleTcpClient? _client;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 缓存最新的服务器返回的原始数据
|
/// 缓存最新的服务器返回的原始数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private byte[] latestRcvData;
|
private byte[]? _latestRcvData;
|
||||||
|
|
||||||
public ILogger _logger { get; set; }
|
public ILogger _logger { get; set; }
|
||||||
private readonly string _device;
|
private readonly string _device;
|
||||||
|
|
||||||
#region 配置参数
|
#region 配置参数
|
||||||
|
|
||||||
[ConfigParameter("设备Id")]
|
[ConfigParameter("设备Id")] public string DeviceId { get; set; }
|
||||||
public Guid DeviceId { get; set; }
|
|
||||||
|
|
||||||
[ConfigParameter("IP地址")]
|
[ConfigParameter("IP地址")] public string IpAddress { get; set; } = "127.0.0.1";
|
||||||
public string IpAddress { get; set; } = "127.0.0.1";
|
|
||||||
|
|
||||||
[ConfigParameter("端口号")]
|
[ConfigParameter("端口号")] public int Port { get; set; } = 6666;
|
||||||
public int Port { get; set; } = 6666;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 为了演示枚举类型在web端的录入,这里没用到 但是你可以拿到
|
/// 为了演示枚举类型在web端的录入,这里没用到 但是你可以拿到
|
||||||
@ -38,11 +34,9 @@ namespace DriverSimTcpClient
|
|||||||
[ConfigParameter("连接类型")]
|
[ConfigParameter("连接类型")]
|
||||||
public ConnectionType ConnectionType { get; set; } = ConnectionType.Long;
|
public ConnectionType ConnectionType { get; set; } = ConnectionType.Long;
|
||||||
|
|
||||||
[ConfigParameter("超时时间ms")]
|
[ConfigParameter("超时时间ms")] public int Timeout { get; set; } = 300;
|
||||||
public int Timeout { get; set; } = 300;
|
|
||||||
|
|
||||||
[ConfigParameter("最小通讯周期ms")]
|
[ConfigParameter("最小通讯周期ms")] public uint MinPeriod { get; set; } = 3000;
|
||||||
public uint MinPeriod { get; set; } = 3000;
|
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
@ -54,7 +48,6 @@ namespace DriverSimTcpClient
|
|||||||
_logger.LogInformation($"Device:[{_device}],Create()");
|
_logger.LogInformation($"Device:[{_device}],Create()");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 判断连接状态
|
/// 判断连接状态
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -63,7 +56,7 @@ namespace DriverSimTcpClient
|
|||||||
get
|
get
|
||||||
{
|
{
|
||||||
//客户端对象不为空并且客户端已连接则返回true
|
//客户端对象不为空并且客户端已连接则返回true
|
||||||
return client != null && client.TcpClient.Connected;
|
return _client != null && _client.TcpClient.Connected;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,15 +69,17 @@ namespace DriverSimTcpClient
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
//进行连接
|
//进行连接
|
||||||
client = new SimpleTcpClient().Connect(IpAddress, Port);
|
_client = new SimpleTcpClient().Connect(IpAddress, Port);
|
||||||
client.DataReceived += Client_DataReceived;
|
_client.DataReceived += Client_DataReceived;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return IsConnected;
|
return IsConnected;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 收到服务端数据
|
/// 收到服务端数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@ -94,7 +89,7 @@ namespace DriverSimTcpClient
|
|||||||
{
|
{
|
||||||
//如果收到的数据校验正确,则放在内存中
|
//如果收到的数据校验正确,则放在内存中
|
||||||
if (e.Data.Length == 8 && e.Data[0] == 0x08)
|
if (e.Data.Length == 8 && e.Data[0] == 0x08)
|
||||||
latestRcvData = e.Data;
|
_latestRcvData = e.Data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -105,14 +100,17 @@ namespace DriverSimTcpClient
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.DataReceived -= Client_DataReceived;
|
if (_client != null)
|
||||||
|
{
|
||||||
|
_client.DataReceived -= Client_DataReceived;
|
||||||
//断开连接
|
//断开连接
|
||||||
client?.Disconnect();
|
_client?.Disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
return !IsConnected;
|
return !IsConnected;
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -125,18 +123,17 @@ namespace DriverSimTcpClient
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
//释放资源
|
//释放资源
|
||||||
client?.Dispose();
|
_client?.Dispose();
|
||||||
}
|
}
|
||||||
catch (Exception)
|
catch (Exception)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 发送数据
|
/// 发送数据
|
||||||
/// </summary>
|
/// </summary>
|
||||||
private byte[] sendCmd = new byte[4] { 0x01, 0x02, 0x03, 0x04 };
|
private readonly byte[] _sendCmd = { 0x01, 0x02, 0x03, 0x04 };
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 解析并返回
|
/// 解析并返回
|
||||||
@ -155,16 +152,17 @@ namespace DriverSimTcpClient
|
|||||||
ret.Message = "起始字节编号错误";
|
ret.Message = "起始字节编号错误";
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
//连接正常则进行读取
|
//连接正常则进行读取
|
||||||
if (IsConnected)
|
if (IsConnected)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//发送请求
|
//发送请求
|
||||||
client?.Write(sendCmd);
|
_client?.Write(_sendCmd);
|
||||||
//等待恢复,这里可以优化
|
//等待恢复,这里可以优化
|
||||||
Thread.Sleep(Timeout);
|
Thread.Sleep(Timeout);
|
||||||
if (latestRcvData == null)
|
if (_latestRcvData == null)
|
||||||
{
|
{
|
||||||
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
||||||
ret.Message = "没有收到数据";
|
ret.Message = "没有收到数据";
|
||||||
@ -176,27 +174,24 @@ namespace DriverSimTcpClient
|
|||||||
{
|
{
|
||||||
case DataTypeEnum.UByte:
|
case DataTypeEnum.UByte:
|
||||||
case DataTypeEnum.Byte:
|
case DataTypeEnum.Byte:
|
||||||
ret.Value = latestRcvData[startIndex];
|
ret.Value = _latestRcvData[startIndex];
|
||||||
break;
|
break;
|
||||||
case DataTypeEnum.Int16:
|
case DataTypeEnum.Int16:
|
||||||
var buffer16 = latestRcvData.Skip(startIndex).Take(2).ToArray();
|
var buffer16 = _latestRcvData.Skip(startIndex).Take(2).ToArray();
|
||||||
ret.Value = BitConverter.ToInt16(new byte[] { buffer16[0], buffer16[1] }, 0);
|
ret.Value = BitConverter.ToInt16(new[] { buffer16[0], buffer16[1] }, 0);
|
||||||
break;
|
break;
|
||||||
case DataTypeEnum.Float:
|
case DataTypeEnum.Float:
|
||||||
//拿到有用的数据
|
//拿到有用的数据
|
||||||
var buffer32 = latestRcvData.Skip(startIndex).Take(4).ToArray();
|
var buffer32 = _latestRcvData.Skip(startIndex).Take(4).ToArray();
|
||||||
//大小端转换一下
|
//大小端转换一下
|
||||||
ret.Value = BitConverter.ToSingle(new byte[] { buffer32[3], buffer32[2], buffer32[1], buffer32[0] }, 0);
|
ret.Value = BitConverter.ToSingle(
|
||||||
break;
|
new[] { buffer32[3], buffer32[2], buffer32[1], buffer32[0] }, 0);
|
||||||
default:
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
||||||
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
||||||
ret.Message = $"读取失败,{ex.Message}";
|
ret.Message = $"读取失败,{ex.Message}";
|
||||||
}
|
}
|
||||||
@ -206,11 +201,12 @@ namespace DriverSimTcpClient
|
|||||||
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
||||||
ret.Message = "连接失败";
|
ret.Message = "连接失败";
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<RpcResponse> WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg)
|
public async Task<RpcResponse> WriteAsync(string requestId, string method, DriverAddressIoArgModel ioarg)
|
||||||
{
|
{
|
||||||
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
|
RpcResponse rpcResponse = new() { IsSuccess = false, Description = "设备驱动内未实现写入功能" };
|
||||||
return rpcResponse;
|
return rpcResponse;
|
||||||
|
Loading…
Reference in New Issue
Block a user