本文主要是介绍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 portport.BaudRate = 9600;port.DataBits = 8;port.Parity = Parity.None;port.StopBits = StopBits.One;port.Open();//var adapter = new SerialPortAdapter(port);// create modbus masterIModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);byte slaveId = 1;ushort startAddress = 100;ushort[] registers = new ushort[] { 1, 2, 3 };// write three registersmaster.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);}}}
}
安装完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源为本地,也就是你下载的包的路径,并且需要 放在最上面,否则找不到,如下
包名一般是如下:
红框内是安装方法,
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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!