首页 > 其他分享 >NMobus

NMobus

时间:2023-11-20 16:11:06浏览次数:29  
标签:NMobus 16 ushort 地址 startAddress new byte

Modbus注意事项:

西门子处理:

对于一般的支持 Modbus TCP 设备,可能无法在其设备手册中查找到诸如 40001、30001 这种数据地址,而是以功能码 + 十六进制数形式的变量地址,这样有两种处理办法:

方法 1:将功能码 + 100 作为 MB_MODE,十六进制地址数转化为十进制数作为 MB_DATA_ADDR。例如 0x03 功能码,地址 0x00FF,这样就是 MB_MODE = 103, MB_DATA_ADDR = 255。

方法 2:通过判断功能码决定是读是写,决定 MB_MODE 为 0 读还是 1 写(2 写比较特殊,只用于不支持 5、6 号功能码的设备),然后将十六进制地址数转化为十进制数,然后对于不同的功能码,十进制数增加不同的偏移量,对于功能码 1、5、15 偏移量为 1,对于功能码 2 偏移量为 10001,对于功能码 3、6、16 偏移量为 40001(对于超过 9999 的地址,偏移量为 400001),对于功能码 4 偏移量为 30001。同样以 0x03 功能码,地址 0x00FF 为例,因为是读,所以 MB_MODE = 0, 0x00FF 转换为十进制为 255,加上偏移量 40001 就是 40256,这样 MB_DATA_ADDR = 40256。

Modbus功能码:

01 READ COIL STATUS
02 READ INPUT STATUS
03 READ HOLDING REGISTER
04 READ INPUT REGISTER
05 WRITE SINGLE COIL
06 WRITE SINGLE REGISTER
15 WRITE MULTIPLE COIL
16 WRITE MULTIPLE REGISTER

NModbus指令方法:

方法名 作用 所需参数 返回值 对应功能码
ReadCoils 读取DO的状态

从站地址(8位)

byte slaveAddress

起始地址(16位)

 

ushort startAddress

读取数量(16位)

ushort numberOfPoints

bool[] 01
ReadInputs 读取DI的状态

从站地址(8位)

byte slaveAddress

起始地址(16位)

 

ushort startAddress

读取数量(16位)

ushort numberOfPoints

bool[] 02
ReadHoldingRegisters 读取AO的值

从站地址(8位)

byte slaveAddress

起始地址(16位) 

ushort startAddress

读取数量(16位)

ushort numberOfPoints

ushort[] 03
ReadInputRegisters 读取AI的值

从站地址(8 位)

byte slaveAddress

起始地址(16位)

 

ushort startAddress

读取数量(16位)

ushort numberOfPoints

ushort[] 04
WriteSingleCoil 写入值到DO

从站地址(8位)

byte slaveAddress

线圈地址(16位)

ushort coilAddress

写入值(布尔型)

bool value

无返回值 05
WriteSingleRegister 写入值到AO

从站地址(8位)

byte slaveAddress

寄存器地址(16位)

ushort registerAddress

写入值(16位)

ushort value

无返回值 06
WriteMultipleCoils 写多线圈寄存器

从站地址(8位)

byte slaveAddress

起始地址(16位)

ushort startAddress

写入值(布尔型数组)

bool[] data

无返回值 15
WriteMultipleRegisters 写多个保持寄存器

从站地址(8位)

byte slaveAddress

 

起始地址(16位)

ushort startAddress,

寄存器值(16位整型数组)

ushort[] data

无返回值 16
ReadWriteMultipleRegisters 读写多个保持寄存器

从站地址(8位)

byte slaveAddress

读起始地址(16位)

ushort startReadAddress

 

读取数量(16位)

ushort numberOfPointsToRead,

写入起始地址(16位)

ushort startWriteAddress,

写入值(16位整型数组)

ushort[] writeData

ushort[] 23

ModbusTCP

构建TcpClient

 1 TcpClient client = new TcpClient("10.16.12.50", 502)//使用Using,来自动释放资源连接资源 

实例化ModbusFactory

 1 var factory = new ModbusFactory(); 

创建TCP主站

 1 IModbusMaster master = factory.CreateMaster(client); 

读DI状态

 1         public static void ModbusTcpMasterReadInputs()
 2         {
 3             using (TcpClient client = new TcpClient("127.0.0.1", 502))
 4             {
 5                 var factory = new ModbusFactory();
 6                 IModbusMaster master = factory.CreateMaster(client);
 7 
 8                 // read five input values
 9                 ushort startAddress = 100;
10                 ushort numInputs = 5;
11                 bool[] inputs = master.ReadInputs(0, startAddress, numInputs);
12 
13                 for (int i = 0; i < numInputs; i++)
14                 {
15                     Console.WriteLine($"Input {(startAddress + i)}={(inputs[i] ? 1 : 0)}");
16                 }
17             }
18 
19             // output: 
20             // Input 100=0
21             // Input 101=0
22             // Input 102=0
23             // Input 103=0
24             // Input 104=0
25         }

 读写保持寄存器

 1         public static void ModbusTcpMasterReadHoldingRegisters32()
 2         {
 3             using (TcpClient client = new TcpClient("10.16.12.50", 502))
 4             {
 5                 var factory = new ModbusFactory();
 6                 IModbusMaster master = factory.CreateMaster(client);
 7 
 8 
 9                 byte slaveId = 1;
10                 ushort startAddress = 7165;
11                 ushort numInputs = 5;
12                 UInt32 www = 0x42c80083;
13                 //写值到保存寄存器
14                 master.WriteSingleRegister32(slaveId, startAddress, www);
15                 //读取保存寄存器
16                 uint[] registers = master.ReadHoldingRegisters32(slaveId, startAddress, numInputs);
17 
18                 for (int i = 0; i < numInputs; i++)
19                 {
20                     Console.WriteLine($"Input {(startAddress + i)}={registers[i]}");
21                 }
22             }
23         }

主站读取从站

 1         public static void ModbusTcpMasterReadInputsFromModbusSlave()
 2         {
 3             byte slaveId = 1;
 4             int port = 502;
 5             IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
 6 
 7             // create and start the TCP slave
 8             TcpListener slaveTcpListener = new TcpListener(address, port);
 9             slaveTcpListener.Start();
10 
11             var factory = new ModbusFactory();
12             var network = factory.CreateSlaveNetwork(slaveTcpListener);
13 
14             IModbusSlave slave = factory.CreateSlave(slaveId);
15 
16             network.AddSlave(slave);
17 
18             var listenTask = network.ListenAsync();
19 
20             // create the master
21             TcpClient masterTcpClient = new TcpClient(address.ToString(), port);
22             IModbusMaster master = factory.CreateMaster(masterTcpClient);
23 
24             ushort numInputs = 5;
25             ushort startAddress = 100;
26 
27             // read five register values
28             ushort[] inputs = master.ReadInputRegisters(0, startAddress, numInputs);
29 
30             for (int i = 0; i < numInputs; i++)
31             {
32                 Console.WriteLine($"Register {(startAddress + i)}={(inputs[i])}");
33             }
34 
35             // clean up
36             masterTcpClient.Close();
37             slaveTcpListener.Stop();
38 
39             // output
40             // Register 100=0
41             // Register 101=0
42             // Register 102=0
43             // Register 103=0
44             // Register 104=0
45         }

 

Modbus RTU

Modbus RTU写寄存器

 

1         //声明两个常量串口号
2         private const string PrimarySerialPortName = "COM4";
3         private const string SecondarySerialPortName = "COM2";    
 1         public static void ModbusSerialRtuMasterWriteRegisters()
 2         {
 3             using (SerialPort port = new SerialPort(PrimarySerialPortName))
 4             {
 5                 // configure serial port
 6                 port.BaudRate = 9600;
 7                 port.DataBits = 8;
 8                 port.Parity = Parity.None;
 9                 port.StopBits = StopBits.One;
10                 port.Open();
11 
12                 var factory = new ModbusFactory();
13                 IModbusMaster master = factory.CreateRtuMaster(port);
14 
15                 byte slaveId = 1;
16                 ushort startAddress = 100;
17                 ushort[] registers = new ushort[] { 1, 2, 3 };
18 
19                 // write three registers
20                 master.WriteMultipleRegisters(slaveId, startAddress, registers);
21             }
22         }

 

标签:NMobus,16,ushort,地址,startAddress,new,byte
From: https://www.cnblogs.com/davisdabing/p/17837721.html

相关文章