C#安装NuGet、安装NModbus4

C#安装:

https://jingyan.baidu.com/article/642c9d34242718644a46f7b3.html

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
//using System.Net;
//using System.Net.Sockets;
using Modbus.Data;
using Modbus.Device;
using Modbus.Utility;
//using Modbus.Serial;

namespace Modbus
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             using (SerialPort port = new SerialPort("COM1"))
             {
                 // configure serial port
                 port.BaudRate = 9600;
                 port.DataBits = 8;
                 port.Parity = Parity.None;
                 port.StopBits = StopBits.One;
                 port.Open();

                 //var adapter = new SerialPortAdapter(port);
                 // create modbus master
                 IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);

                 byte slaveId = 1;
                 ushort startAddress = 100;
                 ushort[] registers = new ushort[] { 1, 2, 3 };

                 // write three registers
                 master.WriteMultipleRegisters(slaveId, startAddress, registers);

                 //读取数据函数:
                 slaveId = 1;
                 startAddress = 0;
                 ushort numRegisters = 10;             
                 registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters);
                 //读取寄存器数据到register数组中
                 //需要处理数据的话 后面可能就需要数据的转化
                   //写数据函数:
                 byte slaveID = 1;
                 ushort registerAddress = 0;
                 ushort value = 100;//你要写的值
                 master.WriteSingleRegister(slaveID, registerAddress, value);
             }
        }
    }
}

C#安装NuGet、安装NModbus4_第1张图片

安装完NuGet,重启VS,在Tool里面找到Nuget下得console,然后输入:

PM > Install-Package NModbus4

 

RTU

https://github.com/NModbus4/NModbus4/

 

可参考

https://blog.csdn.net/sudaobo/article/details/7698117

不通过nuget在线下载,比较慢

可以在https://www.nuget.org/packages/NModbus4/2.1.0下,下载需要的包,

需要设置VS的nuget源为本地,也就是你下载的包的路径,并且需要 放在最上面,否则找不到,如下

C#安装NuGet、安装NModbus4_第2张图片

包名一般是如下:

红框内是安装方法,

C#安装NuGet、安装NModbus4_第3张图片

PM> Install-Package NModbus4 -Version 2.1.0
Installing 'NModbus4 2.1.0'.
Successfully installed 'NModbus4 2.1.0'.
Adding 'NModbus4 2.1.0' to WindowsFormsApplication2.
Successfully added 'NModbus4 2.1.0' to WindowsFormsApplication2.

C#安装NuGet、安装NModbus4_第4张图片

 

你可能感兴趣的:(C#,C#,安装,nuget)