通过串口进行通信 :
1、将 NMobus 类库导入工程中,添加引用、命名空间。工程属性必须配置 为 .NET 4.0。
2、创建 SerialPort 类的一个实例,配置参数,打开串口,如:
public SerialPort port = new SerialPort(“ COM1” )//创建串口
port.BaudRate = 9600;//配置
port.DataBits = 8;
port.Parity = Parity.None;
port.StopBits = StopBits.One;
port.Open();//打开串口
3、调用相应方法创建 Modbus 主站,如:
IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port); //创建 Rtu 传输模式、通过串口 port 通信的主站 master IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port); //ASCII传输模式
4、配置参数,如:
master.Transport.ReadTimeout = 1000;//读取串口数据超时为 1000ms master.Transport.WriteTimeout = 1000;//写入串口数据超时
master.Transport.Retries = 3;//重试次数
master.Transport.WaitToRetryMilliSeconds = 250;//重试间隔
5、调用相应方法执行功能,如:
try
{
master.WriteMultipleRegisters(slaveId, startAddress, registers); }
有以下几种功能:
//读线圈,参数:从站地址 (8位 ) 、起始地址 (16位 ) 、数量 (16位 ) ;返回布尔型数 组
bool[] ReadCoils(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
//读输入离散量,参数:从站地址 (8位 ) 、起始地址 (16位 ) 、数量 (16位 ) ;返回布 尔型数组
bool[] ReadInputs(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
//读保持寄存器,参数:从站地址 (8位 ) 、起始地址 (16位 ) 、数量 (16位 ) ;返回 16位整型数组
ushort[] ReadHoldingRegisters(byte slaveAddress, ushort startAddress, ushort
numberOfPoints);
//读输入寄存器,参数:从站地址 (8位 ) 、起始地址 (16位 ) 、数量 (16位 ) ;返回 16位整型数组
ushort[] ReadInputRegisters(byte slaveAddress, ushort startAddress, ushort numberOfPoints);
//写单个线圈,参数:从站地址 (8位 ) ,线圈地址 (16位 ) ,线圈值 (布尔型 ) void WriteSingleCoil(byte slaveAddress, ushort coilAddress, bool value);
//写单个寄存器,参数:从站地址 (8位 ) ,寄存器地址 (16位 ) ,寄存器值 (16位 ) void WriteSingleRegister(byte slaveAddress, ushort registerAddress, ushort value);
//写多个寄存器,参数:从站地址 (8位 ) ,起始地址 (16位 ) ,寄存器值 (16位整型 数组 )
void WriteMultipleRegisters(byte slaveAddress, ushort startAddress, ushort[] data);
//写多个线圈,参数:从站地址 (8位 ) ,起始地址 (16位 ) ,线圈值 (布尔型数组 ) void WriteMultipleCoils(byte slaveAddress, ushort startAddress, bool[] data);
//读写多个寄存器,参数:从站地址 (8位 ) ,读起始地址 (16位 ) ,数量 (16位 ) ,写 起始地址 (16位 ) ,写入值 (16位整型数组 ) ;返回 16位整型数组
ushort[] ReadWriteMultipleRegisters(byte slaveAddress, ushort startReadAddress, ushort numberOfPointsToRead, ushort startWriteAddress, ushort[] writeData);
6、使用 catch 语句捕捉异常:
如果执行没有出错,则不抛出异常,如果是执行读操作则能得到相应的 返回值。
响应超时会抛出 TimeoutException 类型的异常 ;
接收到从站的异常响应时会抛出 SlaveException 类型的异常, 这一类异常 包含 SlaveExceptionCode 属性,即异常码,通过判断异常码能得知出错的原因。
NModbus Sample Code
Simple Modbus serial RTU master write holding registers example.
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();
// 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);
}
Simple Modbus serial ASCII master read holding registers example.
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();
// create modbus master
IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
byte slaveID = 1;
ushort startAddress = 1;
ushort numRegisters = 5;
// read five registers
ushort[] registers = master.ReadHoldingRegisters(slaveID, startAddress, numRegisters);
for (int i = 0; i < numRegisters; i++)
Console.WriteLine("Register {0}={1}", startAddress + i, registers[i]);
}
// output:
// Register 1=0
// Register 2=0
// Register 3=0
// Register 4=0
// Register 5=0
Simple Modbus serial USB RTU master write multiple coils example.
using (FtdUsbPort port = new FtdUsbPort(0))
{
// configure usb port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = FtdParity.None;
port.StopBits = FtdStopBits.One;
port.Open();
// create modbus master
IModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port);
byte slaveID = 1;
ushort startAddress = 1;
// write three coils
master.WriteMultipleCoils(slaveID, startAddress, new bool[] { true, false, true });
}
Donate
NModbus is developed and
maintained on a voluntary basis
and provided free of charge.
Upcoming Features
WPF NModbus Master
Master Console Wrapper
FTDI USB Slave
Home
Samples
Downloads
Discussion
Issues
NModbus
http://web.archive.org/web/20090601225835/http://www...
第1页?共6页
2011/07/06?9:49
Simple Modbus serial USB ASCII master write multiple coils example.
using (FtdUsbPort port = new FtdUsbPort(0))
{
// configure usb port
port.BaudRate = 9600;
port.DataBits = 8;
port.Parity = FtdParity.None;
port.StopBits = FtdStopBits.One;
port.Open();
// create modbus master
IModbusSerialMaster master = ModbusSerialMaster.CreateAscii(port);
byte slaveID = 1;
ushort startAddress = 1;
// write three coils
master.WriteMultipleCoils(slaveID, startAddress, new bool[] { true, false, true });
}
Simple Modbus TCP master read inputs example.
using (TcpClient client = new TcpClient("127.0.0.1", 502))
{
ModbusIpMaster master = ModbusIpMaster.CreateTcp(client);
// read five input values
ushort startAddress = 100;
ushort numInputs = 5;
bool[] inputs = master.ReadInputs(startAddress, numInputs);
for (int i = 0; i < numInputs; i++)
Console.WriteLine("Input {0}={1}", startAddress + i, inputs[i] ? 1 : 0);
}
// output:
// Input 100=0
// Input 101=0
// Input 102=0
// Input 103=0
// Input 104=0
Simple Modbus UDP master write coils example.
using (UdpClient client = new UdpClient())
{
IPEndPoint endPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 502);
client.Connect(endPoint);
ModbusIpMaster master = ModbusIpMaster.CreateUdp(client);
ushort startAddress = 1;
// write three coils
master.WriteMultipleCoils(startAddress, new bool[] { true, false, true });
}
Simple Modbus serial RTU slave example.
using (SerialPort slavePort = new SerialPort("COM2"))
{
// configure serial port
slavePort.BaudRate = 9600;
slavePort.DataBits = 8;
slavePort.Parity = Parity.None;
slavePort.StopBits = StopBits.One;
slavePort.Open();
byte unitID = 1;
// create modbus slave
ModbusSlave slave = ModbusSerialSlave.CreateRtu(unitID, slavePort);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
}
NModbus
http://web.archive.org/web/20090601225835/http://www...
第2页?共6页
2011/07/06?9:49
Simple Modbus Serial ASCII slave example.
using (SerialPort slavePort = new SerialPort("COM2"))
{
// configure serial port
slavePort.BaudRate = 9600;
slavePort.DataBits = 8;
slavePort.Parity = Parity.None;
slavePort.StopBits = StopBits.One;
slavePort.Open();
byte unitID = 1;
// create modbus slave
ModbusSlave slave = ModbusSerialSlave.CreateAscii(unitID, slavePort);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
}
Simple Modbus serial RTU slave example.
using (SerialPort slavePort = new SerialPort("COM2"))
{
// configure serial port
slavePort.BaudRate = 9600;
slavePort.DataBits = 8;
slavePort.Parity = Parity.None;
slavePort.StopBits = StopBits.One;
slavePort.Open();
byte unitID = 1;
// create modbus slave
ModbusSlave slave = ModbusSerialSlave.CreateRtu(unitID, slavePort);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
}
Simple Modbus TCP slave example.
byte slaveID = 1;
int port = 502;
IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
// create and start the TCP slave
TcpListener slaveTcpListener = new TcpListener(address, port);
slaveTcpListener.Start();
ModbusSlave slave = ModbusTcpSlave.CreateTcp(slaveID, slaveTcpListener);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
// prevent the main thread from exiting
Thread.Sleep(Timeout.Infinite);
Simple Modbus UDP slave example.
using (UdpClient client = new UdpClient(502))
{
ModbusUdpSlave slave = ModbusUdpSlave.CreateUdp(client);
slave.DataStore = DataStoreFactory.CreateDefaultDataStore();
slave.Listen();
// prevent the main thread from exiting
Thread.Sleep(Timeout.Infinite);
}
Modbus TCP master and slave example.
byte slaveID = 1;
int port = 502;
IPAddress address = new IPAddress(new byte[] { 127, 0, 0, 1 });
NModbus
http://web.archive.org/web/20090601225835/http://www...
第3页?共6页
2011/07/06?9:49