NModbus实例_精品文档.pdf
- 文档编号:3177279
- 上传时间:2022-11-19
- 格式:PDF
- 页数:6
- 大小:85.59KB
NModbus实例_精品文档.pdf
《NModbus实例_精品文档.pdf》由会员分享,可在线阅读,更多相关《NModbus实例_精品文档.pdf(6页珍藏版)》请在冰豆网上搜索。
NModbusSampleCodeSimpleModbusserialRTUmasterwriteholdingregistersexample.using(SerialPortport=newSerialPort(COM1)/configureserialportport.BaudRate=9600;port.DataBits=8;port.Parity=Parity.None;port.StopBits=StopBits.One;port.Open();/createmodbusmasterIModbusSerialMastermaster=ModbusSerialMaster.CreateRtu(port);byteslaveID=1;ushortstartAddress=100;ushortregisters=newushort1,2,3;/writethreeregistersmaster.WriteMultipleRegisters(slaveID,startAddress,registers);SimpleModbusserialASCIImasterreadholdingregistersexample.using(SerialPortport=newSerialPort(COM1)/configureserialportport.BaudRate=9600;port.DataBits=8;port.Parity=Parity.None;port.StopBits=StopBits.One;port.Open();/createmodbusmasterIModbusSerialMastermaster=ModbusSerialMaster.CreateAscii(port);byteslaveID=1;ushortstartAddress=1;ushortnumRegisters=5;/readfiveregistersushortregisters=master.ReadHoldingRegisters(slaveID,startAddress,numRegisters);for(inti=0;inumRegisters;i+)Console.WriteLine(Register0=1,startAddress+i,registersi);/output:
/Register1=0/Register2=0/Register3=0/Register4=0/Register5=0SimpleModbusserialUSBRTUmasterwritemultiplecoilsexample.using(FtdUsbPortport=newFtdUsbPort(0)/configureusbportport.BaudRate=9600;port.DataBits=8;port.Parity=FtdParity.None;port.StopBits=FtdStopBits.One;port.Open();/createmodbusmasterIModbusSerialMastermaster=ModbusSerialMaster.CreateRtu(port);byteslaveID=1;ushortstartAddress=1;/writethreecoilsmaster.WriteMultipleCoils(slaveID,startAddress,newbooltrue,false,true);DonateNModbusisdevelopedandmaintainedonavoluntarybasisandprovidedfreeofcharge.UpcomingFeaturesWPFNModbusMasterMasterConsoleWrapperFTDIUSBSlaveHomeSamplesDownloadsDiscussionIssuesNModbushttp:
/web.archive.org/web/20090601225835/http:
/www.第1页共6页2011/07/069:
49SimpleModbusserialUSBASCIImasterwritemultiplecoilsexample.using(FtdUsbPortport=newFtdUsbPort(0)/configureusbportport.BaudRate=9600;port.DataBits=8;port.Parity=FtdParity.None;port.StopBits=FtdStopBits.One;port.Open();/createmodbusmasterIModbusSerialMastermaster=ModbusSerialMaster.CreateAscii(port);byteslaveID=1;ushortstartAddress=1;/writethreecoilsmaster.WriteMultipleCoils(slaveID,startAddress,newbooltrue,false,true);SimpleModbusTCPmasterreadinputsexample.using(TcpClientclient=newTcpClient(127.0.0.1,502)ModbusIpMastermaster=ModbusIpMaster.CreateTcp(client);/readfiveinputvaluesushortstartAddress=100;ushortnumInputs=5;boolinputs=master.ReadInputs(startAddress,numInputs);for(inti=0;inumInputs;i+)Console.WriteLine(Input0=1,startAddress+i,inputsi?
1:
0);/output:
/Input100=0/Input101=0/Input102=0/Input103=0/Input104=0SimpleModbusUDPmasterwritecoilsexample.using(UdpClientclient=newUdpClient()IPEndPointendPoint=newIPEndPoint(newIPAddress(newbyte127,0,0,1),502);client.Connect(endPoint);ModbusIpMastermaster=ModbusIpMaster.CreateUdp(client);ushortstartAddress=1;/writethreecoilsmaster.WriteMultipleCoils(startAddress,newbooltrue,false,true);SimpleModbusserialRTUslaveexample.using(SerialPortslavePort=newSerialPort(COM2)/configureserialportslavePort.BaudRate=9600;slavePort.DataBits=8;slavePort.Parity=Parity.None;slavePort.StopBits=StopBits.One;slavePort.Open();byteunitID=1;/createmodbusslaveModbusSlaveslave=ModbusSerialSlave.CreateRtu(unitID,slavePort);slave.DataStore=DataStoreFactory.CreateDefaultDataStore();slave.Listen();NModbushttp:
/web.archive.org/web/20090601225835/http:
/www.第2页共6页2011/07/069:
49SimpleModbusSerialASCIIslaveexample.using(SerialPortslavePort=newSerialPort(COM2)/configureserialportslavePort.BaudRate=9600;slavePort.DataBits=8;slavePort.Parity=Parity.None;slavePort.StopBits=StopBits.One;slavePort.Open();byteunitID=1;/createmodbusslaveModbusSlaveslave=ModbusSerialSlave.CreateAscii(unitID,slavePort);slave.DataStore=DataStoreFactory.CreateDefaultDataStore();slave.Listen();SimpleModbusserialRTUslaveexample.using(SerialPortslavePort=newSerialPort(COM2)/configureserialportslavePort.BaudRate=9600;slavePort.DataBits=8;slavePort.Parity=Parity.None;slavePort.StopBits=StopBits.One;slavePort.Open();byteunitID=1;/createmodbusslaveModbusSlaveslave=ModbusSerialSlave.CreateRtu(unitID,slavePort);slave.DataStore=DataStoreFactory.CreateDefaultDataStore();slave.Listen();SimpleModbusTCPslaveexample.byteslaveID=1;intport=502;IPAddressaddress=newIPAddress(newbyte127,0,0,1);/createandstarttheTCPslaveTcpListenerslaveTcpListener=newTcpListener(address,port);slaveTcpListener.Start();ModbusSlaveslave=ModbusTcpSlave.CreateTcp(slaveID,slaveTcpListener);slave.DataStore=DataStoreFactory.CreateDefaultDataStore();slave.Listen();/preventthemainthreadfromexitingThread.Sleep(Timeout.Infinite);SimpleModbusUDPslaveexample.using(UdpClientclient=newUdpClient(502)ModbusUdpSlaveslave=ModbusUdpSlave.CreateUdp(client);slave.DataStore=DataStoreFactory.CreateDefaultDataStore();slave.Listen();/preventthemainthreadfromexiti
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- NModbus 实例 精品 文档
![提示](https://static.bdocx.com/images/bang_tan.gif)