SerialPortFactory

master
杨威 1 month ago
parent a857e423d3
commit ca61f8730e

@ -39,9 +39,6 @@
<Reference Include="Chloe.SQLite, Version=5.26.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Chloe.SQLite.5.26.0\lib\net46\Chloe.SQLite.dll</HintPath>
</Reference>
<Reference Include="log4net">
<HintPath>..\Dll\log4net.dll</HintPath>
</Reference>
<Reference Include="Microsoft.VisualBasic" />
<Reference Include="mscorlib" />
<Reference Include="NewLife.Core, Version=10.10.2024.803, Culture=neutral, PublicKeyToken=8343210f0b524456, processorArchitecture=MSIL">
@ -50,9 +47,6 @@
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.3\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="nModbusPC">
<HintPath>..\Dll\nmodbuspc.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Buffers, Version=4.0.3.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
<HintPath>..\packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll</HintPath>
@ -94,10 +88,13 @@
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="TouchSocket, Version=2.1.0.0, Culture=neutral, PublicKeyToken=5f39d7da98dac6a9, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.2.1.0-rc.10\lib\net472\TouchSocket.dll</HintPath>
<HintPath>..\packages\TouchSocket.2.1.0-rc.11\lib\net472\TouchSocket.dll</HintPath>
</Reference>
<Reference Include="TouchSocket.Core, Version=2.1.0.0, Culture=neutral, PublicKeyToken=d6c415a2f58eda72, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.Core.2.1.0-rc.10\lib\net472\TouchSocket.Core.dll</HintPath>
<HintPath>..\packages\TouchSocket.Core.2.1.0-rc.11\lib\net472\TouchSocket.Core.dll</HintPath>
</Reference>
<Reference Include="TouchSocket.SerialPorts, Version=2.1.0.0, Culture=neutral, PublicKeyToken=b0dfdf1c6b51c716, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.SerialPorts.2.1.0-rc.11\lib\net472\TouchSocket.SerialPorts.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
@ -125,8 +122,7 @@
</Compile>
<Compile Include="FormUtils.cs" />
<Compile Include="TestDataFactory.cs" />
<Compile Include="Tool\hfIIReader.cs" />
<Compile Include="Tool\HFIIREADER_PARAMS.cs" />
<Compile Include="Tool\MyFixedHeaderRequestInfo.cs" />
<Compile Include="Tool\SerialPortFactory.cs" />
<EmbeddedResource Include="FrmSetting.resx">
<DependentUpon>FrmSetting.cs</DependentUpon>
@ -185,7 +181,7 @@
</Content>
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\TouchSocket.Core.2.1.0-rc.10\analyzers\dotnet\cs\TouchSocket.Core.SourceGenerator.dll" />
<Analyzer Include="..\packages\TouchSocket.Core.2.1.0-rc.11\analyzers\dotnet\cs\TouchSocket.Core.SourceGenerator.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -1,49 +0,0 @@
using System.Runtime.InteropServices;
namespace NDSD_Screwdriver.Tool
{
// Token: 0x02000002 RID: 2
[StructLayout(LayoutKind.Sequential, Pack = 4)]
public struct HFIIREADER_PARAMS
{
// Token: 0x04000001 RID: 1
public ushort addr;
// Token: 0x04000002 RID: 2
public ushort br;
// Token: 0x04000003 RID: 3
public ushort afiMode;
// Token: 0x04000004 RID: 4
public ushort invtTo;
// Token: 0x04000005 RID: 5
public ushort trgMode;
// Token: 0x04000006 RID: 6
public ushort opTagMode;
// Token: 0x04000007 RID: 7
public ushort opBlockRegAddr;
// Token: 0x04000008 RID: 8
public ushort opBlockRegNum;
// Token: 0x04000009 RID: 9
public ushort trgTimerTick;
// Token: 0x0400000A RID: 10
public ushort opFormat;
// Token: 0x0400000B RID: 11
public ushort frameHead;
// Token: 0x0400000C RID: 12
public ushort keppTimer;
// Token: 0x0400000D RID: 13
public ushort tagType;
}
}

@ -0,0 +1,83 @@
using NewLife;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TouchSocket.Core;
namespace NDSD_Screwdriver.Tool
{
public class MyFixedHeaderRequestInfo : IFixedHeaderRequestInfo
{
/// <summary>
/// 接口实现,标识数据长度
/// </summary>
public int BodyLength { get; private set; }
/// <summary>
/// 自定义属性,标识数据类型
/// </summary>
public byte DataType { get; set; }
/// <summary>
/// 自定义属性,标识指令类型
/// </summary>
public byte OrderType { get; set; }
/// <summary>
/// 自定义属性,标识实际数据
/// </summary>
public byte[] Body { get; set; }
public string GetBody()
{
// 方法1: 使用 Array.Copy
byte[] copiedBytes = new byte[BodyLength - 2];
Array.Copy(Body, 0, copiedBytes, 0, BodyLength - 2);
Array.Reverse(copiedBytes);
return copiedBytes.ToHex();
}
public bool OnParsingBody(ReadOnlySpan<byte> body)
{
if (body.Length == this.BodyLength)
{
this.Body = body.ToArray();
return true;
}
return false;
}
public bool OnParsingHeader(ReadOnlySpan<byte> header)
{
//在该示例中第一个字节表示后续的所有数据长度但是header设置的是3所以后续还应当接收length-2个长度。
this.BodyLength = header[2] + 2;
this.DataType = header[0];
this.OrderType = header[1];
return true;
}
}
public class MyFixedHeaderCustomDataHandlingAdapter : CustomFixedHeaderDataHandlingAdapter<MyFixedHeaderRequestInfo>
{
/// <summary>
/// 接口实现,指示固定包头长度
/// </summary>
public override int HeaderLength => 3;
/// <summary>
/// 获取新实例
/// </summary>
/// <returns></returns>
protected override MyFixedHeaderRequestInfo GetInstance()
{
return new MyFixedHeaderRequestInfo();
}
}
}

@ -1,60 +1,65 @@
using System.IO.Ports;
using System;
using NewLife.Log;
using NewLife;
using System.IO.Ports;
using TouchSocket.Core;
using TouchSocket.SerialPorts;
using TouchSocket.Sockets;
namespace NDSD_Screwdriver.Tool
{
public class SerialPortFactory
{
private SerialPort serialPortModbus;
private hfIIReader reader;
SerialPortClient clientSerialPortClient = new SerialPortClient();
private IWaitingClient<ISerialPortClient, IReceiverResult> waitClient;
public SerialPortFactory(string portName="COM13")
{
reader = new hfIIReader();
serialPortModbus = new SerialPort();
serialPortModbus.BaudRate = 9600;
serialPortModbus.PortName = portName;
serialPortModbus.ReadTimeout = 0;
clientSerialPortClient.Connecting = (client, e) => EasyTask.CompletedTask;//即将连接到端口
clientSerialPortClient.Connected = (client, e) => { return EasyTask.CompletedTask; };//成功连接到端口
clientSerialPortClient.Setup(new TouchSocket.Core.TouchSocketConfig()
.SetSerialPortOption(new SerialPortOption()
{
BaudRate = 115200, //波特率
DataBits = 8, //数据位
Parity = System.IO.Ports.Parity.None, //校验位
PortName = "COM13", //COM
StopBits = System.IO.Ports.StopBits.One //停止位
}).SetSerialDataHandlingAdapter(() => new MyFixedHeaderCustomDataHandlingAdapter()));
clientSerialPortClient.Connect();
waitClient = clientSerialPortClient.CreateWaitingClient(new WaitingOptions()
{
FilterFunc = response => //设置用于筛选的fun委托当返回为true时才会响应返回
true
});
}
public string Read()
{
if (!serialPortModbus.IsOpen)
try
{
serialPortModbus.Open();
ResponsedData responsedData = waitClient.SendThenResponse(new byte[] { 0x00, 0x03, 0x00, 0x50, 0x00, 0x04, 0x45, 0xC9 });
IRequestInfo requestInfo = responsedData.RequestInfo;
var myFixedHeaderRequestInfo = requestInfo as MyFixedHeaderRequestInfo;
return myFixedHeaderRequestInfo.GetBody();
}
var b = reader.Connect(serialPortModbus, 2000, 0);
string str2 = "";
ushort[] array6 = reader.ReadTagMemory(ref str2, 28, 4);
string text6 = "";
for (int j = 0; j < 4; j += 1)
catch (Exception e)
{
text6 += array6[j].ToString("X").PadLeft(4, '0');
if (text6.Length > 0)
{
text6 += "";
}
return "";
}
return text6;
}
public void Disconnect()
{
if (!serialPortModbus.IsOpen)
{
serialPortModbus.Close();
}
reader.Disconnect();
}
}

File diff suppressed because it is too large Load Diff

@ -11,6 +11,7 @@
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
<package id="TouchSocket" version="2.1.0-rc.10" targetFramework="net48" />
<package id="TouchSocket.Core" version="2.1.0-rc.10" targetFramework="net48" />
<package id="TouchSocket" version="2.1.0-rc.11" targetFramework="net48" />
<package id="TouchSocket.Core" version="2.1.0-rc.11" targetFramework="net48" />
<package id="TouchSocket.SerialPorts" version="2.1.0-rc.11" targetFramework="net48" />
</packages>

@ -73,10 +73,13 @@
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
<Reference Include="TouchSocket, Version=2.1.0.0, Culture=neutral, PublicKeyToken=5f39d7da98dac6a9, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.2.1.0-rc.10\lib\net472\TouchSocket.dll</HintPath>
<HintPath>..\packages\TouchSocket.2.1.0-rc.11\lib\net472\TouchSocket.dll</HintPath>
</Reference>
<Reference Include="TouchSocket.Core, Version=2.1.0.0, Culture=neutral, PublicKeyToken=d6c415a2f58eda72, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.Core.2.1.0-rc.10\lib\net472\TouchSocket.Core.dll</HintPath>
<HintPath>..\packages\TouchSocket.Core.2.1.0-rc.11\lib\net472\TouchSocket.Core.dll</HintPath>
</Reference>
<Reference Include="TouchSocket.SerialPorts, Version=2.1.0.0, Culture=neutral, PublicKeyToken=b0dfdf1c6b51c716, processorArchitecture=MSIL">
<HintPath>..\packages\TouchSocket.SerialPorts.2.1.0-rc.11\lib\net472\TouchSocket.SerialPorts.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
@ -91,7 +94,7 @@
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<Analyzer Include="..\packages\TouchSocket.Core.2.1.0-rc.10\analyzers\dotnet\cs\TouchSocket.Core.SourceGenerator.dll" />
<Analyzer Include="..\packages\TouchSocket.Core.2.1.0-rc.11\analyzers\dotnet\cs\TouchSocket.Core.SourceGenerator.dll" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

@ -9,6 +9,7 @@
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.3" targetFramework="net48" />
<package id="System.Threading.Tasks.Extensions" version="4.5.4" targetFramework="net48" />
<package id="System.ValueTuple" version="4.5.0" targetFramework="net48" />
<package id="TouchSocket" version="2.1.0-rc.10" targetFramework="net48" />
<package id="TouchSocket.Core" version="2.1.0-rc.10" targetFramework="net48" />
<package id="TouchSocket" version="2.1.0-rc.11" targetFramework="net48" />
<package id="TouchSocket.Core" version="2.1.0-rc.11" targetFramework="net48" />
<package id="TouchSocket.SerialPorts" version="2.1.0-rc.11" targetFramework="net48" />
</packages>
Loading…
Cancel
Save