Skip to content

Commit c0a1ad4

Browse files
committed
增加DmM7510读写类
1 parent 4f82861 commit c0a1ad4

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

Src/ConsoleApp1/ConsoleApp1.csproj

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<ProjectReference Include="..\Helevn.OCV.DMM\Helevn.OCV.DMM.csproj" />
12+
</ItemGroup>
13+
14+
</Project>

Src/ConsoleApp1/Program.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// See https://aka.ms/new-console-template for more information
2+
using Helevn.OCV.DMM;
3+
4+
while (true)
5+
{
6+
var ctrl = new OcvCtrl("OCV1");
7+
try
8+
{
9+
await ctrl.EnsureConnectAsync();
10+
while (true)
11+
{
12+
var v = await ctrl.ReadAsync(DefaultCmd.ReadOcvCmd);
13+
Console.WriteLine($"值等于:{v}");
14+
await Task.Delay(1000);
15+
}
16+
}
17+
catch (Exception ex)
18+
{
19+
await ctrl.DisposeAsync();
20+
Console.WriteLine(ex.Message);
21+
}
22+
await Task.Delay(1000);
23+
}

Src/Helevn.OCV.DMM.sln

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@ Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio Version 17
44
VisualStudioVersion = 17.10.35027.167
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Helevn.OCV.DMM", "Helevn.OCV.DMM\Helevn.OCV.DMM.csproj", "{A3B87D1C-9DE4-42DB-BB8A-680BA94A698A}"
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Helevn.OCV.DMM", "Helevn.OCV.DMM\Helevn.OCV.DMM.csproj", "{A3B87D1C-9DE4-42DB-BB8A-680BA94A698A}"
77
EndProject
88
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{1755D5FA-8928-48EE-ABE6-7D92BE0D0A58}"
99
ProjectSection(SolutionItems) = preProject
1010
global.json = global.json
1111
paket.dependencies = paket.dependencies
1212
EndProjectSection
1313
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp1", "ConsoleApp1\ConsoleApp1.csproj", "{5AB7C636-A76B-4437-828D-D4F14200EEA1}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -21,6 +23,10 @@ Global
2123
{A3B87D1C-9DE4-42DB-BB8A-680BA94A698A}.Debug|Any CPU.Build.0 = Debug|Any CPU
2224
{A3B87D1C-9DE4-42DB-BB8A-680BA94A698A}.Release|Any CPU.ActiveCfg = Release|Any CPU
2325
{A3B87D1C-9DE4-42DB-BB8A-680BA94A698A}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{5AB7C636-A76B-4437-828D-D4F14200EEA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{5AB7C636-A76B-4437-828D-D4F14200EEA1}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{5AB7C636-A76B-4437-828D-D4F14200EEA1}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{5AB7C636-A76B-4437-828D-D4F14200EEA1}.Release|Any CPU.Build.0 = Release|Any CPU
2430
EndGlobalSection
2531
GlobalSection(SolutionProperties) = preSolution
2632
HideSolutionNode = FALSE

Src/Helevn.OCV.DMM/DefaultCmd.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Helevn.OCV.DMM
2+
{
3+
public class DefaultCmd
4+
{
5+
/// <summary>
6+
/// DMM7510 读取命令字
7+
/// </summary>
8+
public static byte[] ReadOcvCmd => [0x52, 0x65, 0x61, 0x64, 0x3F, 0x0A];
9+
}
10+
}

Src/Helevn.OCV.DMM/OcvCtrl.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System.Net.Sockets;
2+
using System.Text;
3+
4+
namespace Helevn.OCV.DMM
5+
{
6+
public class OcvCtrl(string name)
7+
{
8+
/// <summary>
9+
/// 名称
10+
/// </summary>
11+
public readonly string Name = name;
12+
private TcpClient? _tcpClient = null;
13+
public bool Connect => _tcpClient?.Connected ?? false;
14+
15+
/// <summary>
16+
/// 小数点保留位数
17+
/// </summary>
18+
public int Digits { get; set; } = 5;
19+
/// <summary>
20+
/// 最大值限位
21+
/// </summary>
22+
public int LimitMaxValue { get; set; } = 99999;
23+
/// <summary>
24+
/// 最小值限位
25+
/// </summary>
26+
public int LimitMinValue { get; set; } = -99999;
27+
/// <summary>
28+
/// 读取超时限制
29+
/// <para/>
30+
/// ms
31+
/// </summary>
32+
public int LimitOverTime { get; set; } = 1000;
33+
34+
/// <summary>
35+
/// IP地址
36+
/// </summary>
37+
public string IpAddress { get; set; } = "127.0.0.1";
38+
/// <summary>
39+
/// IP端口号
40+
/// </summary>
41+
public int IpPort { get; set; } = 502;
42+
43+
public async Task<double> ReadAsync(byte[] cmds)
44+
{
45+
try
46+
{
47+
var stream = _tcpClient!.GetStream();
48+
await stream.WriteAsync(cmds);
49+
byte[] rbuffer = new byte[256];
50+
var revleng = await stream.ReadAsync(rbuffer, new CancellationTokenSource(LimitOverTime).Token);
51+
byte[] recbuffer = new byte[revleng];
52+
for (int i = 0; i < revleng; i++)
53+
{
54+
recbuffer[i] = rbuffer[i];
55+
}
56+
var str = Encoding.Default.GetString(recbuffer);
57+
var r = double.TryParse(str, out double v);
58+
if (!r)
59+
v = double.NaN;
60+
v = Math.Round(v, Digits);
61+
v = Math.Min(v, LimitMaxValue);
62+
v = Math.Max(v, LimitMinValue);
63+
return v;
64+
}
65+
catch
66+
{
67+
throw;
68+
}
69+
}
70+
public async Task EnsureConnectAsync()
71+
{
72+
_tcpClient ??= new TcpClient();
73+
if (!_tcpClient.Connected)
74+
{
75+
await _tcpClient.ConnectAsync(IpAddress, IpPort);
76+
}
77+
}
78+
public Task DisposeAsync()
79+
{
80+
if (_tcpClient != null)
81+
{
82+
_tcpClient.Close();
83+
_tcpClient.Dispose();
84+
_tcpClient = null;
85+
}
86+
return Task.CompletedTask;
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)