private void testMaster(ModbusMaster master) { for (var i = 0; i < 4; i++) { master.WriteCoil(1, us(3000 + i), false); Thread.Sleep(50); Assert.AreEqual(false, master.ReadCoil(1, us(3000 + i))); master.WriteCoil(1, us(3000 + i), true); Thread.Sleep(50); Assert.AreEqual(true, master.ReadCoil(1, us(3000 + i))); } var s0 = bs(false, false, false, false); var s1 = bs(false, true, true, false); var s2 = bs(true, false, false, true); master.WriteCoils(1, 3000, s1); Thread.Sleep(50); Assert.AreEqual(s1, master.ReadCoils(1, 3000, 4)); Thread.Sleep(50); master.WriteCoils(1, 3000, s2); Thread.Sleep(50); Assert.AreEqual(s2, master.ReadCoils(1, 3000, 4)); Thread.Sleep(50); master.WriteCoils(1, 3000, s0); Thread.Sleep(50); Assert.AreEqual(s0, master.ReadCoils(1, 3000, 4)); }
void Test(ModbusModel model, ModbusMaster master) { master.WriteCoil(1, 2, true); Assert.AreEqual(true, master.ReadCoil(1, 2)); master.WriteCoil(1, 3, false); Assert.AreEqual(false, master.ReadCoil(1, 3)); master.WriteCoils(1, 4, bo(false, true)); Assert.AreEqual(bo(true, false, false, true), master.ReadCoils(1, 2, 4)); model.setDIs(11, 12, bo(true, true, false, false)); Assert.AreEqual(true, master.ReadInput(11, 12)); Assert.AreEqual(true, master.ReadInput(11, 13)); Assert.AreEqual(false, master.ReadInput(11, 14)); Assert.AreEqual(false, master.ReadInput(11, 15)); Assert.AreEqual(bo(true, true, false, false), master.ReadInputs(11, 12, 4)); master.WriteRegister(1, 2, 0xabcd); Assert.AreEqual(0xabcd, master.ReadHoldingRegister(1, 2)); master.WriteRegister(1, 3, 0xcdab); Assert.AreEqual(0xcdab, master.ReadHoldingRegister(1, 3)); master.WriteRegisters(1, 4, us(0xcda1, 0xcda2)); Assert.AreEqual(us(0xabcd, 0xcdab, 0xcda1, 0xcda2), master.ReadHoldingRegisters(1, 2, 4)); model.setWIs(11, 12, us(0xabcd, 0xcdab, 0xcda1, 0xcda2)); Assert.AreEqual(0xabcd, master.ReadInputRegister(11, 12)); Assert.AreEqual(0xcdab, master.ReadInputRegister(11, 13)); Assert.AreEqual(0xcda1, master.ReadInputRegister(11, 14)); Assert.AreEqual(0xcda2, master.ReadInputRegister(11, 15)); Assert.AreEqual(us(0xabcd, 0xcdab, 0xcda1, 0xcda2), master.ReadInputRegisters(11, 12, 4)); }
public static void SharedSlaveTest(ModbusModel model, ModbusMaster master) { master.WriteCoil(1, 2, true); Assert.AreEqual(true, master.ReadCoil(1, 2)); master.WriteCoil(1, 3, false); Assert.AreEqual(false, master.ReadCoil(1, 3)); master.WriteCoils(1, 4, H.bo(false, true)); Assert.AreEqual(H.bo(true, false, false, true), master.ReadCoils(1, 2, 4)); //race condition avoided by access order model.setDIs(11, 12, H.bo(true, true, false, false)); Assert.AreEqual(true, master.ReadInput(11, 12)); Assert.AreEqual(true, master.ReadInput(11, 13)); Assert.AreEqual(false, master.ReadInput(11, 14)); Assert.AreEqual(false, master.ReadInput(11, 15)); Assert.AreEqual(H.bo(true, true, false, false), master.ReadInputs(11, 12, 4)); master.WriteRegister(1, 2, 0xabcd); Assert.AreEqual(0xabcd, master.ReadHoldingRegister(1, 2)); master.WriteRegister(1, 3, 0xcdab); Assert.AreEqual(0xcdab, master.ReadHoldingRegister(1, 3)); master.WriteRegisters(1, 4, H.us(0xcda1, 0xcda2)); Assert.AreEqual(H.us(0xabcd, 0xcdab, 0xcda1, 0xcda2), master.ReadHoldingRegisters(1, 2, 4)); //race condition avoided by access order model.setWIs(11, 12, H.us(0xabcd, 0xcdab, 0xcda1, 0xcda2)); Assert.AreEqual(0xabcd, master.ReadInputRegister(11, 12)); Assert.AreEqual(0xcdab, master.ReadInputRegister(11, 13)); Assert.AreEqual(0xcda1, master.ReadInputRegister(11, 14)); Assert.AreEqual(0xcda2, master.ReadInputRegister(11, 15)); Assert.AreEqual(H.us(0xabcd, 0xcdab, 0xcda1, 0xcda2), master.ReadInputRegisters(11, 12, 4)); }
private static ushort[] TryRead(ModbusMaster modbus, ModbusRegisterType registerType, int address, int register) { switch (registerType) { case ModbusRegisterType.AnalogInput: return(modbus.ReadInputRegisters((byte)address, (ushort)register, 1)); case ModbusRegisterType.Holding: return(modbus.ReadHoldingRegisters((byte)address, (ushort)register, 1)); case ModbusRegisterType.Coil: return(modbus.ReadCoils((byte)address, (ushort)register, 1).Select(c => c ? (ushort)1 : (ushort)0).ToArray()); case ModbusRegisterType.DiscreteInput: return(modbus.ReadInputs((byte)address, (ushort)register, 1).Select(c => c ? (ushort)1 : (ushort)0).ToArray()); } return(null); }
public bool Read() { if (_master == null) { throw new NullReferenceException("_master"); } var minReg = Registers.Min(); var maxReg = Registers.Max(); var count = Convert.ToUInt16(maxReg - minReg + 1); try { Thread.Sleep(3000); //锁一下Transport,避免和其它的调用者发生冲突 lock (Transport) { var inputsResult = _master.ReadCoils(minReg, count); if (inputsResult == null) { return(false); } Values.Clear(); if (inputsResult.Any()) { Values = inputsResult.ToList(); } } return(true); } catch (ObjectDisposedException odx) { throw odx; } catch (Exception ex) { XTrace.WriteException(ex); return(false); } }
public void ModportSweepTest() { //m0 - MD-DIDC8 8 digital input //m1 - MD-DOSO8 8 digital output //all outputs wired to corresponding inputs var serial = new SerialPort("COM10"); serial.BaudRate = 57600; serial.Open(); var stream = new ModbusSerialStream(serial, 400); var protocol = new ModbusRTUProtocol(); var master = new ModbusMaster(stream, protocol); master.WriteCoil(1, 3000, false); master.WriteCoil(1, 3001, true); master.WriteCoil(1, 3002, true); master.WriteCoil(1, 3003, false); Thread.Sleep(50); Assert.AreEqual(false, master.ReadCoil(1, 3000)); Assert.AreEqual(true, master.ReadCoil(1, 3001)); Thread.Sleep(50); Assert.AreEqual(new bool[] { false, true, true, false }, master.ReadCoils(1, 3000, 4)); }
public void RackSweepTest() { //http://www.opto22.com/documents/1678_Modbus_TCP_Protocol_Guide.pdf // required otg included in SharpOpto22 // m0.0 = 24V // m0.1 = 0V // m0.2 = m1.2 // m0.3 = m1.3 // m2.1 = m3.1 // m2.2 = m3.2 var socket = new TcpClient("10.77.0.2", 502); var stream = new ModbusSocketStream(socket, 400); var protocol = new ModbusTCPProtocol(); var master = new ModbusMaster(stream, protocol); master.WriteCoils(1, 4, Bools(4, false)); //clear module Thread.Sleep(50); master.WriteCoils(1, 64, Bools(4, true)); //clear on latches master.WriteCoils(1, 128, Bools(4, true)); //clear off latches Assert.AreEqual(true, master.ReadCoil(1, 0)); Assert.AreEqual(false, master.ReadCoil(1, 1)); Assert.AreEqual(false, master.ReadCoil(1, 2)); Assert.AreEqual(false, master.ReadCoil(1, 3)); master.WriteCoil(1, 6, false); master.WriteCoil(1, 7, false); Thread.Sleep(50); Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 64 + 2, 2)); Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 128 + 2, 2)); Assert.AreEqual(new bool[] { false, false }, master.ReadCoils(1, 6, 2)); Assert.AreEqual(new bool[] { true, false, false, false }, master.ReadCoils(1, 0, 4)); master.WriteCoil(1, 6, true); master.WriteCoil(1, 7, false); Thread.Sleep(50); Assert.AreEqual(true, master.ReadCoil(1, 0)); Assert.AreEqual(false, master.ReadCoil(1, 1)); Assert.AreEqual(true, master.ReadCoil(1, 2)); Assert.AreEqual(false, master.ReadCoil(1, 3)); Assert.AreEqual(new bool[] { true, false }, master.ReadInputs(1, 64 + 2, 2)); Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 128 + 2, 2)); Assert.AreEqual(new bool[] { true, false }, master.ReadCoils(1, 6, 2)); Assert.AreEqual(new bool[] { true, false, true, false }, master.ReadCoils(1, 0, 4)); master.WriteCoil(1, 6, false); master.WriteCoil(1, 7, true); Thread.Sleep(50); Assert.AreEqual(true, master.ReadCoil(1, 0)); Assert.AreEqual(false, master.ReadCoil(1, 1)); Assert.AreEqual(false, master.ReadCoil(1, 2)); Assert.AreEqual(true, master.ReadCoil(1, 3)); Assert.AreEqual(new bool[] { true, true }, master.ReadInputs(1, 64 + 2, 2)); Assert.AreEqual(new bool[] { true, false }, master.ReadInputs(1, 128 + 2, 2)); Assert.AreEqual(new bool[] { false, true }, master.ReadCoils(1, 6, 2)); Assert.AreEqual(new bool[] { true, false, false, true }, master.ReadCoils(1, 0, 4)); master.WriteCoils(1, 64, Bools(4, true)); //clear on latches master.WriteCoils(1, 128, Bools(4, true)); //clear off latches Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 64 + 2, 2)); Assert.AreEqual(new bool[] { false, false }, master.ReadInputs(1, 128 + 2, 2)); //analog SetAnalog(master, 12, 0); SetAnalog(master, 13, 0); Assert.AreEqual(0, GetAnalog(master, 12)); Assert.AreEqual(0, GetAnalog(master, 13)); Thread.Sleep(50); Assert.That(0f, Is.EqualTo(GetAnalog2(master, 8)).Within(0.1)); Assert.That(0f, Is.EqualTo(GetAnalog2(master, 9)).Within(0.1)); SetAnalog(master, 12, 5); SetAnalog(master, 13, 10); Assert.AreEqual(5, GetAnalog(master, 12)); Assert.AreEqual(10, GetAnalog(master, 13)); Thread.Sleep(50); Assert.That(5f, Is.EqualTo(GetAnalog2(master, 8)).Within(0.1)); Assert.That(10f, Is.EqualTo(GetAnalog2(master, 9)).Within(0.1)); SetAnalog2(master, 12, -5); SetAnalog2(master, 13, -10); Assert.AreEqual(-5, GetAnalog(master, 12)); Assert.AreEqual(-10, GetAnalog(master, 13)); Thread.Sleep(50); Assert.That(-5f, Is.EqualTo(GetAnalog2(master, 8)).Within(0.1)); Assert.That(-10f, Is.EqualTo(GetAnalog2(master, 9)).Within(0.1)); }
public virtual void ReadCoils() { bool[] coils = Master.ReadCoils(SlaveAddress, 2048, 8); Assert.AreEqual(new bool[] { false, false, false, false, false, false, false, false }, coils); }
private void Sequencer_Worker() { while (seqThreadStop == false) { if (master != null) { byte slaveId = (byte)(StartAddr + CurrentSlave); ushort startAddress = (ushort)Register; ushort numInputs = (ushort)Quantity; //datagridview was resized if (dataGrid.Rows.Count - 1 < CurrentSlave) { CurrentSlave = 0; } int requests = (int)dataGrid.Rows[CurrentSlave].Cells[1].Value; int errors = (int)dataGrid.Rows[CurrentSlave].Cells[2].Value; requests++; try { // write three registers ushort[] inputs = null; bool[] bools = null; switch (Function) { case 0: bools = master.ReadCoils(slaveId, startAddress, numInputs); break; case 1: bools = master.ReadInputs(slaveId, startAddress, numInputs); break; case 2: inputs = master.ReadHoldingRegisters(slaveId, startAddress, numInputs); break; case 3: inputs = master.ReadInputRegisters(slaveId, startAddress, numInputs); break; } if (bools != null) { for (int i = 0; i < numInputs; i++) { dataGrid.Rows[CurrentSlave].Cells[i + 3].Value = bools[i] ? "1" : "0"; } } else if (inputs != null) { for (int i = 0; i < numInputs; i++) { dataGrid.Rows[CurrentSlave].Cells[i + 3].Value = (short)(inputs[i]); } } } catch (Exception ex) { errors++; } dataGrid.Rows[CurrentSlave].Cells[1].Value = requests; dataGrid.Rows[CurrentSlave].Cells[2].Value = errors; CurrentSlave++; if ((CurrentSlave + StartAddr) > StopAddr) { CurrentSlave = 0; } Thread.Sleep(ScanRate); } while (seqThreadPause) { Thread.Sleep(100); } } }