当前位置: 首页 > news >正文

c#:管理TCP服务端发送数据为非16进制

C#实现TCP客户端管理:支持SCPI指令的发送与接收

引言

在工业自动化、仪器控制和测试测量领域,SCPI(Standard Commands for Programmable Instruments)是一种广泛使用的标准命令语言。本文将介绍如何使用C#实现一个TCP客户端管理类,专门用于与非16进制数据格式的TCP服务端通信,特别适合处理SCPI指令的发送和接收。

核心类设计

1. TcpMultiClientManager类

TcpMultiClientManager是一个管理多个TCP客户端连接的高级封装类,主要功能包括:

public class TcpMultiClientManager : IDisposable
{private readonly List<(string Ip, int Port)> _clientConfigurations;private readonly ConcurrentDictionary<(string Ip, int Port), TcpCommunication> _clients;private readonly ConcurrentDictionary<(string Ip, int Port), (bool IsOn, string Current, string Voltage)> _deviceStatus;// ... 其他成员和方法
}
主要特点:
  • 支持管理多个TCP客户端连接
  • 线程安全的并发集合存储客户端和设备状态
  • 异步连接和消息接收
  • 设备状态监控和管理

2. TcpCommunication类

TcpCommunication是实际的TCP通信实现类,封装了底层TCP操作:

public class TcpCommunication : IDisposable
{private readonly string _ip;private readonly int _port;private TcpClient _tcpClient;private NetworkStream _networkStream;// ... 其他成员和方法
}
主要特点:
  • 封装TCP连接和流操作
  • 支持同步和异步通信
  • 提供SCPI指令发送和响应接收功能
  • 完善的错误处理和日志记录

核心功能实现

1. 初始化连接

public async Task InitializeAsync()
{var tasks = new List<Task>();foreach (var (ip, port) in _clientConfigurations){tasks.Add(Task.Run(async () =>{var client = new TcpCommunication(ip, port);if (_clients.TryAdd((ip, port), client)){await ConnectClient(ip, port);await StartReceivingMessages(ip, port);}}));}await Task.WhenAll(tasks);
}

2. SCPI指令发送与接收

同步方式:
public string SendAndSendCommand(string ip, int port, string command)
{if (_clients.TryGetValue((ip, port), out var client)){return client.TestSendCommandAndGetResponse(command);}else{logWriter.WriteLogMessage(ip + port + "TestDeviceStatus函数未找到指定的服务端!");return "0.0";}
}
异步方式:
public async Task SendCommandAsync(string command)
{if (!IsConnected){Console.WriteLine("设备未连接");logWriter.WriteLogMessage("同步接收数据设备未连接");}try{byte[] buffer = Encoding.ASCII.GetBytes(command);await _networkStream.WriteAsync(buffer, 0, buffer.Length);}catch (Exception ex){Console.WriteLine($"Error sending command: {ex.Message}");Disconnect();throw;}
}

3. 设备状态监控

public void TestDeviceStatus(string ip, int port)
{try{bool isOn = false;string CURR = SendAndSendCommand(ip, port, "MEAS:CURR?");string VOLT = SendAndSendCommand(ip, port, "MEAS:VOLT?");// 数据处理逻辑if (voltage > 5){isOn = true;}_deviceStatus.AddOrUpdate((ip, port),(isOn, CURR, VOLT),(key, oldValue) => (isOn, CURR, VOLT));}catch (Exception ex){// 错误处理}
}

使用示例

1. 初始化并连接多个设备

var configurations = new List<(string, int)>
{("192.168.1.100", 5025),("192.168.1.101", 5025),("192.168.1.102", 5025)
};var manager = new TcpMultiClientManager(configurations);
await manager.InitializeAsync();

2. 发送SCPI指令并获取响应

// 同步方式
string current = manager.SendAndSendCommand("192.168.1.100", 5025, "MEAS:CURR?");
Console.WriteLine($"Current: {current}");// 异步方式
await manager.SendCommandToClient("192.168.1.100", 5025, "OUTP ON");

3. 监控设备状态

manager.TestDeviceStatus("192.168.1.100", 5025);
var status = manager.GetAllDeviceStatus();
foreach (var s in status)
{Console.WriteLine($"IP: {s.Ip}, Port: {s.Port}, IsOn: {s.IsOn}, Current: {s.Current}, Voltage: {s.Voltage}");
}

TcpCommunication 类完整代码:

    public class TcpCommunication : IDisposable{public string Ip => _ip;private readonly string _ip;private readonly int _port;private TcpClient _tcpClient;pr
http://www.lryc.cn/news/593900.html

相关文章:

  • 网络原理——IP
  • CentOS 服务器docker pull 拉取失败
  • Docker 在 Ubuntu 系统中的详细操作指南
  • 【Docker-Day 7】揭秘 Dockerfile 启动指令:CMD、ENTRYPOINT、ENV、ARG 与 EXPOSE 详解
  • Docker实战:使用Docker部署envlinks极简个人导航页
  • 企业级安全威胁检测与响应(EDR/XDR)架构设计
  • 如何解决pip安装报错error subprocess-exited-with-error问题
  • Twisted study notes[2]
  • 六年级数学知识边界总结思考-下册
  • 在Ubutu22系统上面离线安装Go语言环境【教程】
  • 传染病监测(六):随机模型 —— 为什么小规模疫情像掷骰子?
  • 【LeetCode 热题 100】200. 岛屿数量——DFS
  • MCP实战案例|Trae2.0 一键创建旅行助手并一键部署EdgeOne
  • axios二次封装-单个、特定的实例的拦截器、所有实例的拦截器。
  • Laravel 原子锁概念讲解
  • sqli-labs靶场通关笔记:第34-37关 宽字节注入的其他情况
  • docker Neo4j
  • PDF 编辑器:多文件合并 拆分 旋转 顺序随便调 加水印 密码锁 页码背景
  • Python 进阶知识之numpy库(一)
  • 考研最高效的准备工作是什么
  • 【JDK内置工具】常用工具和实战指令
  • 30天打牢数模基础-决策树讲解
  • Docker在NAS部署MoonTV+OrionTV
  • [Python] -项目实战8- 构建一个简单的 Todo List Web 应用(Flask)
  • 深度学习×第10卷:她用一块小滤镜,在图像中找到你
  • 嵌入式硬件篇---按键
  • 嵌入式硬件篇---继电器
  • USB 2.0 vs USB 3.0:全面技术对比与选择指南
  • 2025《艾诺提亚失落之歌》新手攻略
  • 基于单片机出租车计价器设计