当前位置:网站首页>Lire Modbus TCP avec nmodbus
Lire Modbus TCP avec nmodbus
2022-01-15 02:10:07 【La lave de Rodinia】
Écris un NmodbusHelper
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.Sockets;
using System.Net;
using Modbus.Device;
using thinger.cn;
namespace NmodbusHelper
{
public class Modbus4TCP
{
private TcpClient tcpClient = null;
private ModbusIpMaster master;
#region Ouverture et fermetureSocket
public bool Connect(string ip, int port)
{
try
{
tcpClient = new TcpClient();
tcpClient.Connect(IPAddress.Parse(ip), port);
//IPEndPoint ie = new IPEndPoint(IPAddress.Parse(ip), port);
//tcpClient.Connect(ie);
master = ModbusIpMaster.CreateIp(tcpClient);
}
catch (Exception)
{
return false;
}
return true;
}
public bool Disconnect()
{
if (tcpClient != null)
{
tcpClient.Close();
return true;
}
else
{
return false;
}
}
#endregion
public byte[] ReadKeepReg(string iAddress, int iLength)
{
ushort[] des = master.ReadHoldingRegisters(Convert.ToUInt16(iAddress), Convert.ToUInt16(iLength));// Interception automatique des bits de données
byte[] res = thinger.cn.DataConvertHelper.ByteArrayLib.GetByteArrayFromUShortArray(des);
return res;
}
}
}
InwinformRéalisation intermédiaire Utiliser le Code de fonction 3Lire,Utilisation concomitanteUnsignedAvecfloat 2 Format de sortie
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using Modbus.Device;
using NmodbusHelper;
using thinger.cn;
namespace NmodbusPractice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string ip = "127.0.0.1";
int port = 502;
Modbus4TCP objTcp = new Modbus4TCP();
private bool IsConnected = false;
//private TcpClient tcpClient;
private void btnConnect_Click(object sender, EventArgs e)
{
IsConnected = objTcp.Connect(ip, port);
//IsConnected = Connect("127.0.0.1", port);
Addinfo(IsConnected ? "Connexion réussie" : "La connexion a échoué");
}
private void btnRead_Click(object sender, EventArgs e)
{
byte[] res = objTcp.ReadKeepReg(this.txtVarAdd.Text, Convert.ToUInt16(this.numCount.Text));
if (res != null)
{
//Produits Entier
//ushort[] Res = thinger.cn.DataConvertHelper.UShortLib.GetUShortArrayFromByteArray(res); //UnsignedProduits
//this.isInfo.Items.Insert(0, string.Join(" ",Res));
//Produits Nombre de points flottants
//float Dres1 = thinger.cn.DataConvertHelper.FloatLib.GetFloatFromByteArray(res);//FloatProduits
//float Dres2 = thinger.cn.DataConvertHelper.FloatLib.GetFloatFromByteArray(res, 4);
//string s1 = Convert.ToDouble(Dres1).ToString("f1");
//string s2 = Convert.ToDouble(Dres2).ToString("f1");
//this.isInfo.Items.Insert(0, s1+" "+s2);
List<float> arrf = new List<float>();
for (int i = 0; i < res.Length; i+=4)
{
arrf.Add(thinger.cn.DataConvertHelper.FloatLib.GetFloatFromByteArray(res,i));
}
foreach (var item in arrf)
{
Console.WriteLine(item);
}
Console.ReadKey();
}
else
{
Addinfo("La connexion a échoué");
}
}
private void btnDisconnect_Click(object sender, EventArgs e)
{
objTcp.Disconnect();
}
private void Addinfo(string info)
{
this.isInfo.Items.Insert(
0, DateTime.Now.ToString("HH:mm:ss") + " " + info + Environment.NewLine);
}
//public string AnalyseData_4xUnsigned(byte[] res)
//{
// ushort[] Res = thinger.cn.DataConvertHelper.UShortLib.GetUShortArrayFromByteArray(res);
// return thinger.cn.DataConvertHelper.StringLib.GetStringFromByteArray()
//}
}
}
版权声明
本文为[La lave de Rodinia]所创,转载请带上原文链接,感谢
https://chowdera.com/2022/01/202201080558307174.html
边栏推荐
- Jimureport building block report v1 Version 4.2 release, free visual low code report
- JVM - automatic memory management - 2 - garbage collector and memory allocation strategy
- Spotmax update: scalable statistics, instance preheating, intelligent storage, providing multiple cost reduction guarantees
- 黑五 圣诞节
- 【TiChoo资讯站】
- 行业分享 | TiChoo数据CEO 陆诗冬展望全球视频电商未来蓝图
- TiChoo数据分析选品 { 资讯站 }
- 【TiChoo资讯站】TikTok及跨境电商周报
- 行业分享 | TiChoo数据即将出席2022年海外短视频行业峰会
- tiktok 数据分析平台
猜你喜欢
随机推荐
- nuget的几个地址
- 使用Text.json解析Json文件
- 常用的SQL语句
- 使用多线程写winform程序
- Modbus协议编写与测试
- 使用多线程,Invoke和Action 访问SQL数据库
- Access数据库练习
- 泛型类, 泛型接口的继承, 委托
- 泛型类,泛型接口
- is 和 as的用法
- 多线程的深入理解
- 异步调用,多线程
- 跨域请求无法携带Cookie的问题
- Jenkins 入门
- Jenkins 分布式架构
- Jenkins 配置中文显示(汉化)
- Jenkins 通过API 执行 grovvy 脚本
- Jenkins API接入指南
- Jenkins 通过API获取从节点的secret
- 浅析npm run serve命令
- I think code is a work of art. She's beautiful
- Push failed Dst refspec V1.0.0 matches more than one.
- 微服务系列--深入理解RPC底层原理与设计实践
- Try the map and slice of the model version in go 1.18
- [highcharts] 04_ wrap
- (highly recommended) mobile audio and video from zero to start
- 微服務系列--深入理解RPC底層原理與設計實踐
- Push failed Dst refspec V1.0.0 matches more than one.
- Série de microservices - compréhension approfondie des principes sous - jacents et des pratiques de conception du CPR
- Push failed DST refspec v1. 0,0 matches more than one.
- Analyse de la commande NPM Run Service
- Jenkins obtient le secret du noeud via l'API
- Jenkins API Access Guide
- Quickly write a vs code plug-in
- Yyds dry goods inventory trunk (I)
- Modify a value to make Scrollview and listview elastic and APK volume optimized
- Jenkins exécute le script grovvy via l'API
- Jenkins configure l'affichage chinois (chinois)
- Jenkins Distributed Architecture
- Introduction à Jenkins