場景模擬
假設你有一批非標設備需要對接,對方提供了如下協議檔案:
協議概述
設備作為TCPServer,埠6666
位元組序:Little-Endian,即低地址存放低位
請求回復
需要你主動發起讀取請求:0x01 02 03 04
設備回復:0x08 01 41 D6 3D 71 1A 20
引數說明
- 總位元組數
(byte[0])即0x08:用于簡單的校驗
- 運行狀態
(byte[1])即0x01:1為運行;其他為停止
- 設備溫度
(byte[2]-byte[5])即0x41 D6 3D 71:單精度浮點數值26.78
- 電機轉速
(byte[6]-byte[7])即0x1A 20:對應16進制無符號整型,倍率0.01值66.88
驅動開發
我們根據上面的協議,開發驅動,請先瀏覽上一篇驅動簡介
創建驅動專案
-
在解決方案->Drivers檔案夾,右鍵添加->新建專案->C#類別庫

-
專案名DriverSimTcpClient,放在
iotgateway\Plugins\Drivers路徑下

-
修改Class1為SimTcpClient
-
雙擊專案,修改配置

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<OutputPath>../../../IoTGateway/bin/Debug/net6.0/drivers</OutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SimpleTCP.Core" Version="1.0.4" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\PluginInterface\PluginInterface.csproj" />
</ItemGroup>
</Project>
:::info 說明
OutputPath節點指定了生成專案的檔案夾
SimpleTCP.Core是一個TCP客戶端庫(你也可以自己寫)
ProjectReference節點參考了PluginInterface專案
CopyLocalLockFileAssemblies節點可以確保你參考的nuget拷貝到driver檔案夾下
:::
撰寫專案代碼
using PluginInterface;
using SimpleTCP;
using System;
using System.Text;
namespace DriverSimTcpClient
{
[DriverSupported("SimTcpServerDevice")]
[DriverInfoAttribute("SimTcpClient", "V1.0.0", "Copyright iotgateway? 2022-06-04")]
public class SimTcpClient : IDriver
{
/// <summary>
/// tcp客戶端
/// </summary>
private SimpleTcpClient? client;
/// <summary>
/// 快取最新的服務器回傳的原始資料
/// </summary>
private byte[] latestRcvData;
#region 配置引數
[ConfigParameter("設備Id")]
public Guid DeviceId { get; set; }
[ConfigParameter("IP地址")]
public string IpAddress { get; set; } = "127.0.0.1";
[ConfigParameter("埠號")]
public int Port { get; set; } = 6666;
/// <summary>
/// 為了演示列舉型別在web端的錄入,這里沒用到 但是你可以拿到
/// </summary>
[ConfigParameter("連接型別")]
public ConnectionType ConnectionType { get; set; } = ConnectionType.Long;
[ConfigParameter("超時時間ms")]
public int Timeout { get; set; } = 300;
[ConfigParameter("最小通訊周期ms")]
public uint MinPeriod { get; set; } = 3000;
#endregion
public SimTcpClient(Guid deviceId)
{
DeviceId = deviceId;
}
/// <summary>
/// 判斷連接狀態
/// </summary>
public bool IsConnected
{
get
{
//客戶端物件不為空并且客戶端已連接則回傳true
return client != null && client.TcpClient.Connected;
}
}
/// <summary>
/// 進行連接
/// </summary>
/// <returns>連接是否成功</returns>
public bool Connect()
{
try
{
//進行連接
client = new SimpleTcpClient().Connect(IpAddress, Port);
client.DataReceived += Client_DataReceived;
}
catch (Exception)
{
return false;
}
return IsConnected;
}
/// <summary>
/// 收到服務端資料
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Client_DataReceived(object? sender, Message e)
{
//如果收到的資料校驗正確,則放在記憶體中
if (e.Data.Length == 8 && e.Data[0] == 0x08)
latestRcvData = https://www.cnblogs.com/whdong/archive/2022/06/05/e.Data;
}
///
/// 斷開連接
///
/// 斷開是否成功
public bool Close()
{
try
{
client.DataReceived -= Client_DataReceived;
//斷開連接
client?.Disconnect();
return !IsConnected;
}
catch (Exception)
{
return false;
}
}
///
/// 釋放
///
public void Dispose()
{
try
{
//釋放資源
client?.Dispose();
}
catch (Exception)
{
}
}
///
/// 發送資料
///
private byte[] sendCmd = new byte[4] { 0x01, 0x02, 0x03, 0x04 };
///
/// 決議并回傳
///
///
注冊驅動
- 生成
DriverSimTcpClient專案iotgateway\IoTGateway\bin\Debug\net6.0\drivers\net6.0路徑下可以看到生成了DriverSimTcpClient.dll - 運行IoTGateway,訪問本地518埠
- 添加驅動
網關配置->驅動管理->添加

:::warning 注意
添加驅動后需要重啟一下專案,后面會優化
:::
創建設備
- 采集配置->設備維護->添加設備

添加變數
- 采集配置->設備維護->添加設備
手動添加或者通過excel批量匯入下面變數
| 變數名 | 方法 | 地址 | 型別 | 運算式 | 設備名 |
|---|---|---|---|---|---|
| 運行狀態 | Read | 1 | uint8 | 模擬設備 | |
| 設備溫度 | Read | 2 | float | 模擬設備 | |
| 電機轉速 | Read | 6 | int16 | raw*0.01 | 模擬設備 |
開始采集
采集配置->設備維護->編輯設備

啟動TcpServer
運行你熟悉的TCPServer測驗工具,啟動埠6666,網關客戶端連接后發送回應報文

查看資料

轉載請註明出處,本文鏈接:https://www.uj5u.com/net/486164.html
標籤:.NET技术
上一篇:WPF開發學生資訊管理系統【WPF+Prism+MAH+WebApi】(完)
下一篇:WPF-實作螢屏截圖(一)
