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)); }
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 DefaultTest() { var socket = Tools.ConnectWithTimeout("127.0.0.1", 9005, 400); var stream = new ModbusSocketStream(socket, 400, StreamLog); var protocol = new ModbusTCPProtocol(); protocol.TransactionId = 0xFA; using (var master = new ModbusMaster(stream, protocol)) { var count = 10; while (--count > 0) { master.ReadCoil(1, 4096); } } }
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 static void SharedExceptionTest(ModbusMaster master) { master.ReadCoil(1, 2); //should except on empty model }