Nmodbus4Core类库:

我.NET6上用的包:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <Content Remove="VariableNode.json" />
  </ItemGroup>

  <ItemGroup>
    <None Include="VariableNode.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
	  <PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="6.0.4" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.3" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Abstractions" Version="6.0.3" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="6.0.3" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.3" />
	  <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.3">
		  <PrivateAssets>all</PrivateAssets>
		  <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
	  </PackageReference>
	  <PackageReference Include="Microsoft.Extensions.Configuration" Version="6.0.1" />
	  <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
	  <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
	  <PackageReference Include="Microsoft.Extensions.Options" Version="6.0.0" />
	  <PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
	  <PackageReference Include="NModbus4.NetCore" Version="2.0.1" />
	  <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="6.0.1" />
  </ItemGroup>

</Project>

using Modbus.Device;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Sockets;
using System.Text;
using thinger.DataConvertLib;

namespace DotNet6Demon
{
    public class NModBusHelper
    {
        private TcpClient tcpClient = null;
        private ModbusIpMaster master;

        public bool Connect(string ip, string port)
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(IPAddress.Parse(ip), int.Parse(port));
                master = ModbusIpMaster.CreateIp(tcpClient);
            }
            catch (Exception)
            {
                return false;
            }
            
            return tcpClient.Connected;
        }

        public bool Disconnect()
        {
            if (tcpClient != null)
            {
                tcpClient.Close();
                return true;
            }
            else
            {
                return false;
            }
        }
        
        public byte[] ReadKeepRegByteArr(string iAddress, string iLength)//偏移量,寄存器数量
        {
            try
            {
                ushort[] des = master.ReadHoldingRegisters(ushort.Parse(iAddress), ushort.Parse(iLength));
                byte[] res = ByteArrayLib.GetByteArrayFromUShortArray(des);
                return res;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public ushort[] ReadKeepRegUshort(string iAddress, string iLength)//偏移量,寄存器数量
        {
            try
            {
                ushort[] des = master.ReadHoldingRegisters(ushort.Parse(iAddress), ushort.Parse(iLength));
                //byte[] res = ByteArrayLib.GetByteArrayFromUShortArray(des);
                return des;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public void CommonWriteSingleRegister(string iAddress, string value)
        {
            //ushort valueTemp = Convert.ToUInt16(Math.Floor(double.Parse(value)));
            //master.WriteSingleRegister(ushort.Parse(iAddress), valueTemp);
            master.WriteSingleRegister(ushort.Parse(iAddress), ushort.Parse(value));
        }

        public List<float> AnalyseData_4x(ushort[] des, string iAddress)
        {
            int StartByte;
            StartByte = int.Parse(iAddress) * 2;
            List<float> floatArray = new List<float>();
            byte[] byteArray = ByteArrayLib.GetByteArrayFromUShortArray(des);

            for (int i = StartByte; i < byteArray.Length; i += 4)
            {
                floatArray.Add(FloatLib.GetFloatFromByteArray(byteArray, i));
            }
            return floatArray;
        }


    }
}

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潘诺西亚的火山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值