返回首页
当前位置: 主页>开发编程语言>

RS232 编程实例源码(Modbus serial)

时间:2018-06-17 23:37来源:USB开发群 作者:USB开发频道 点击:

  

from clipboard

内容来自dedecms

本文来自织梦

【核心代码】

内容来自dedecms

using System; using System.IO.Ports; using System.Net; using System.Net.Sockets; using System.Threading; using Modbus.Data; using Modbus.Device; using Modbus.Utility; namespace MySample { /// <summary> /// Demonstration of NModbus /// </summary> public class Driver { private static void Main(string[] args) { log4net.Config.XmlConfigurator.Configure(); try { //ModbusTcpMasterReadInputs(); //SimplePerfTest(); //ModbusSerialRtuMasterWriteRegisters(); //ModbusSerialAsciiMasterReadRegisters(); //ModbusTcpMasterReadInputs(); //StartModbusAsciiSlave(); //ModbusTcpMasterReadInputsFromModbusSlave(); //ModbusSerialAsciiMasterReadRegistersFromModbusSlave(); //StartModbusTcpSlave(); //StartModbusUdpSlave(); //StartModbusAsciiSlave(); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadKey(); } /// <summary> /// Simple Modbus serial RTU master write holding registers example. /// </summary> public static void ModbusSerialRtuMasterWriteRegisters() { 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); } } /// <summary> /// Simple Modbus serial ASCII master read holding registers example. /// </summary> public static void ModbusSerialAsciiMasterReadRegisters() { 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 {startAddress i}={registers[i]}"); } } // output: // Register 1=0 // Register 2=0 // Register 3=0 // Register 4=0 // Register 5=0 } /// <summary> /// Simple Modbus TCP master read inputs example. /// </summary> public static void ModbusTcpMasterReadInputs() { using (TcpClient client = new TcpClient("127.0.0.1", 502)) { ModbusIpMaster master = ModbusIpMaster.CreateIp(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 {(startAddress i)}={(inputs[i] ? 1 : 0)}"); } } // output: // Input 100=0 // Input 101=0 // Input 102=0 // Input 103=0 // Input 104=0 } /// <summary> /// Simple Modbus UDP master write coils example. /// </summary> public static void ModbusUdpMasterWriteCoils() { using (UdpClient client = new UdpClient()) { IPEndPoint endPoint = new IPEndPoint(new IPAddress(new byte[] { 127, 0, 0, 1 }), 502); client.Connect(endPoint); ModbusIpMaster master = ModbusIpMaster.CreateIp(client); ushort startAddress = 1; // write three coils master.WriteMultipleCoils(startAddress, new bool[] { true, false, true }); } } /// <summary> /// Simple Modbus serial ASCII slave example. /// </summary> public static void StartModbusSerialAsciiSlave() { 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(); } } /// <summary> /// Simple Modbus serial RTU slave example. /// </summary> public static void StartModbusSerialRtuSlave() { 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(); } } /// <summary> /// Simple Modbus serial USB ASCII slave example. /// </summary> public static void StartModbusSerialUsbAsciiSlave() { // TODO } /// <summary> /// Simple Modbus serial USB RTU slave example. /// </summary> public static void StartModbusSerialUsbRtuSlave() { // TODO } /// <summary> /// Simple Modbus TCP slave example. /// </summary> public static void StartModbusTcpSlave() { 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); } /// <summary> /// Simple Modbus UDP slave example. /// </summary> public static void StartModbusUdpSlave() { 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); } } /// <summary> /// Modbus TCP master and slave example. /// </summary> public static void ModbusTcpMasterReadInputsFromModbusSlave() { 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); Thread slaveThread = new Thread(slave.Listen); slaveThread.Start(); // create the master TcpClient masterTcpClient = new TcpClient(address.ToString(), port); ModbusIpMaster master = ModbusIpMaster.CreateIp(masterTcpClient); ushort numInputs = 5; ushort startAddress = 100; // read five register values ushort[] inputs = master.ReadInputRegisters(startAddress, numInputs); for (int i = 0; i < numInputs; i ) { Console.WriteLine($"Register {(startAddress i)}={(inputs[i])}"); } // clean up masterTcpClient.Close(); slaveTcpListener.Stop(); // output // Register 100=0 // Register 101=0 // Register 102=0 // Register 103=0 // Register 104=0 } /// <summary> /// Modbus serial ASCII master and slave example. /// </summary> public static void ModbusSerialAsciiMasterReadRegistersFromModbusSlave() { using (SerialPort masterPort = new SerialPort("COM1")) using (SerialPort slavePort = new SerialPort("COM2")) { // configure serial ports masterPort.BaudRate = slavePort.BaudRate = 9600; masterPort.DataBits = slavePort.DataBits = 8; masterPort.Parity = slavePort.Parity = Parity.None; masterPort.StopBits = slavePort.StopBits = StopBits.One; masterPort.Open(); slavePort.Open(); // create modbus slave on seperate thread byte slaveId = 1; ModbusSlave slave = ModbusSerialSlave.CreateAscii(slaveId, slavePort); Thread slaveThread = new Thread(new ThreadStart(slave.Listen)); slaveThread.Start(); // create modbus master ModbusSerialMaster master = ModbusSerialMaster.CreateAscii(masterPort); master.Transport.Retries = 5; ushort startAddress = 100; ushort numRegisters = 5; // read five register values ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, numRegisters); for (int i = 0; i < numRegisters; i ) { Console.WriteLine($"Register {(startAddress i)}={registers[i]}"); } } // output // Register 100=0 // Register 101=0 // Register 102=0 // Register 103=0 // Register 104=0 } /// <summary> /// Write a 32 bit value. /// </summary> public static void ReadWrite32BitValue() { 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 ModbusSerialMaster master = ModbusSerialMaster.CreateRtu(port); byte slaveId = 1; ushort startAddress = 1008; uint largeValue = UInt16.MaxValue 5; ushort lowOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(largeValue), 0); ushort highOrderValue = BitConverter.ToUInt16(BitConverter.GetBytes(largeValue), 2); // write large value in two 16 bit chunks master.WriteMultipleRegisters(slaveId, startAddress, new ushort[] { lowOrderValue, highOrderValue }); // read large value in two 16 bit chunks and perform conversion ushort[] registers = master.ReadHoldingRegisters(slaveId, startAddress, 2); uint value = ModbusUtility.GetUInt32(registers[1], registers[0]); } } } }

织梦内容管理系统






秦华 『上海买保险』上海地区人寿保险资深寿险顾问 上海外籍人士买保险-高端人士医疗办理
服务项目:儿童医疗/子女教育金/养老保险/重大疾病/意外伤害/员工团险/公众责任
免费热线『上海』:13761630213, QQ:107242375, Email:victor.qin@qq.com
选择保险事业 为了爱,为了一份坚定的承诺!MSN:
上海地区请直接拨打本人电话,应约免费上门提供免费保险方案。
Baoxian.ws>>           旅行签证保险、上门办理>>        重大疾病网>>     上海重大疾病一览    
友情链接: 走在上海人寿保险的前端    和讯保险博客    上海投保在线    Shanghai Insurance    秦华的个人简历


顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
图文并茂 我的链接
最新评论 查看所有评论
发表评论 查看所有评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 密码: 验证码:
上海高端医疗/住院医疗保险信息
上海保险规划师信息
上海子女教育理财保险信息
上海保险规划师信息
上海女性高端生育保险方案-等待期
上海保险规划师信息
上海三口之家家庭保障方案
上海保险规划师信息
上海女性特色重大疾病保险方案
上海保险规划师信息
上海女性白领理财保险方案
上海保险规划师信息
上海养老理财规划保险方案
上海保险规划师信息
通过网络取得初步咨询
  •    
上海办理-免费咨询送计划书
上海买保险
上海办理-申根签证 出国旅行保险
上海买保险
上海买保险,海外基金,买保险,友邦保险,中国人民健康保险,养老保险,儿童保险,儿童医疗,子女教育金,太平洋保险,医疗保险,工伤保险条例,中国人寿保险,保险公司,中国平安保险,医疗险,进口药,自费药,外籍人士医疗,泰康人寿,英国保诚集团,上海综合保险,国际医疗保险,上海医疗保险,上海社会保险,上海养老保险,上海市综合保险,上海平安保险,上海太平洋保险,和睦家医院,上海综合保险卡,买保险,高端医疗,重大疾病,意外伤害,定期寿险,家庭理财,友邦保险,金茂大厦,少儿互助基金,上海社保,太平洋集团,港安医院