//模拟器初始化 public override void Simulator(int times, bool IsSystem) { SimulatorClose(); if (_simularorserialPort == null) { InitSmSerialPort(ref _simularorserialPort); } SimulatorUpdateCycle = times; for (int i = 0; i < this.IODevices.Count; i++) { try { ModbusSlave DeviceSlave = null; if (Serial_PARA.ModbusType == ModbusType.ASCII) { DeviceSlave = ModbusSerialSlave.CreateAscii(byte.Parse(this.IODevices[i].IO_DEVICE_ADDRESS), _simularorserialPort); } else { DeviceSlave = ModbusSerialSlave.CreateRtu(byte.Parse(this.IODevices[i].IO_DEVICE_ADDRESS), _simularorserialPort); } slaves.Add(DeviceSlave); } catch (Exception emx) { this.SimulatorAppendLog("创建设备模拟器失败 " + this.IODevices[i].IO_DEVICE_NAME + " " + emx.Message); } } }
public bool EstablishConnection(int address, MasterConnectionData connectionData) { ResetConnection(); _serialPort = new SerialPort(); _serialPort.BaudRate = (int)connectionData.BaudRate; _serialPort.DataBits = (int)connectionData.DataBits; _serialPort.Parity = connectionData.Parity; _serialPort.StopBits = connectionData.StopBits; _serialPort.PortName = connectionData.PortName; try { _serialPort.Open(); _slave = ModbusSerialSlave.CreateAscii((byte)address, _serialPort); _slave.Transport.ReadTimeout = connectionData.TimeoutInMs; _slave.Transport.Retries = connectionData.RetransmissionCount; _slave.Transport.SlaveBusyUsesRetryCount = true; //_slave.ModbusSlaveRequestReceived += _receivedRequestEventHandler; _slave.WriteComplete += _prepareAnswerEventHandler; _slaveListenerThread = new Thread(_slave.Listen); } catch (Exception ex) { MessageBox.Show(ex.Message, "Could not establish connection.", MessageBoxButtons.OK, MessageBoxIcon.Error); return(false); } return(true); }
public NModbusSerialAsciiMasterNModbusSerialAsciiSlaveFixture() { MasterSerialPort = CreateAndOpenSerialPort(DefaultMasterSerialPortName); Master = ModbusSerialMaster.CreateAscii(MasterSerialPort); SetupSlaveSerialPort(); Slave = ModbusSerialSlave.CreateAscii(SlaveAddress, SlaveSerialPort); StartSlave(); }
public override void Init() { base.Init(); MasterSerialPort = CreateAndOpenSerialPort(DefaultMasterSerialPortName); Master = ModbusSerialMaster.CreateAscii(MasterSerialPort); SetupSlaveSerialPort(); Slave = ModbusSerialSlave.CreateAscii(SlaveAddress, SlaveSerialPort); StartSlave(); }
public void ApplyRequest_VerifyModbusRequestReceivedEventIsFired() { bool eventFired = false; ModbusSlave slave = ModbusSerialSlave.CreateAscii(1, new SerialPort()); WriteSingleRegisterRequestResponse request = new WriteSingleRegisterRequestResponse(1, 1, 1); slave.ModbusSlaveRequestReceived += (obj, args) => { eventFired = true; Assert.AreEqual(request, args.Message); }; slave.ApplyRequest(request); Assert.IsTrue(eventFired); }
/// <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(); var slaveAdapter = new SerialPortAdapter(slavePort); // create modbus slave on seperate thread byte slaveId = 1; ModbusSlave slave = ModbusSerialSlave.CreateAscii(slaveId, slaveAdapter); var listenTask = slave.ListenAsync(); var masterAdapter = new SerialPortAdapter(masterPort); // create modbus master ModbusSerialMaster master = ModbusSerialMaster.CreateAscii(masterAdapter); 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> /// 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(); } }
public static ModbusSlave CreateSlave(byte id, SerialPortAdapter adapter, SerialModBusMode type) { ModbusSlave slave = null; if (adapter != null) { switch (type) { case SerialModBusMode.Rtu: slave = ModbusSerialSlave.CreateRtu(id, adapter); break; case SerialModBusMode.Ascii: slave = ModbusSerialSlave.CreateAscii(id, adapter); break; default: break; } } return(slave); }
private void ButtonOpenRTUASCII_Click(object sender, EventArgs e) { SerPort = new SerialPort(); if (!string.IsNullOrWhiteSpace(tbManualCOM.Text)) { SerPort.PortName = tbManualCOM.Text; } else { SerPort.PortName = cbPort.SelectedItem.ToString(); } SerPort.BaudRate = Convert.ToInt32(cbBaud.SelectedItem.ToString()); SerPort.Parity = (Parity)cbParity.SelectedIndex; SerPort.DataBits = Convert.ToInt32(cbDataBits.SelectedItem.ToString()); SerPort.StopBits = (StopBits)cbStopBits.SelectedIndex + 1; SerPort.Handshake = Handshake.None; SerPort.DtrEnable = true; SerPort.RtsEnable = true; try { SerPort.Open(); } catch (Exception ex) { MessageBox.Show(ex.ToString()); return; } if (cbCommMode.SelectedIndex == 0) //RTU { slave = ModbusSerialSlave.CreateRtu(unitID, SerPort); } else if (cbCommMode.SelectedIndex == 3) //ASCIIoverRTU { slave = ModbusSerialSlave.CreateAscii(unitID, SerPort); } slave.ModbusSlaveRequestReceived += MasterRequests; slave.DataStore = dataStore; btnOpenRTUASCII.Enabled = false; btnOpenRTUASCII.BackColor = Color.Gainsboro; btnCloseRTUASCII.Enabled = true; btnCloseRTUASCII.BackColor = Color.LightSteelBlue; btnRefresh.Enabled = false; btnRefresh.BackColor = Color.Gainsboro; cbPort.Enabled = false; cbBaud.Enabled = false; cbDataBits.Enabled = false; cbParity.Enabled = false; cbStopBits.Enabled = false; cbCommMode.Enabled = false; cbRowCount.Enabled = false; tbManualCOM.Enabled = false; bckgndThread = new Thread(BckgndThreadTask) { IsBackground = true }; bckgndThread.Start(); }