public void ModbusRtuProtocol_calculateCrc() { byte[] data1 = new byte[] { 0x01, 0x01, 0x00, 0x00, 0x00, 0x0A, 0xBC, 0x0D }; UInt16 crc = ModbusRtuProtocol.calculateCrc(data1, 6); Console.WriteLine(crc.ToString()); Assert.IsTrue(crc == (data1[6] << 8 | data1[7])); byte[] data2 = new byte[] { 0x01, 0x02, 0x00, 0x00, 0x00, 0x0A, 0xF8, 0x0D }; crc = ModbusRtuProtocol.calculateCrc(data2, 6); Assert.IsTrue(crc == (data2[6] << 8 | data2[7])); byte[] data3 = new byte[] { 0x01, 0x03, 0x00, 0x00, 0x00, 0x0A, 0xC5, 0xCD }; crc = ModbusRtuProtocol.calculateCrc(data3, 6); Assert.IsTrue(crc == (data3[6] << 8 | data3[7])); byte[] data4 = new byte[] { 0x01, 0x04, 0x00, 0x00, 0x00, 0x0A, 0x70, 0x0D }; crc = ModbusRtuProtocol.calculateCrc(data4, 6); Assert.IsTrue(crc == (data4[6] << 8 | data4[7])); byte[] data5 = new byte[] { 0x01, 0x05, 0x00, 0x00, 0x00, 0x0A, 0x4D, 0xCD }; crc = ModbusRtuProtocol.calculateCrc(data5, 6); Assert.IsTrue(crc == (data5[6] << 8 | data5[7])); byte[] data6 = new byte[] { 0x01, 0x06, 0x00, 0x00, 0x00, 0x0A, 0x09, 0xCD }; crc = ModbusRtuProtocol.calculateCrc(data6, 6); Assert.IsTrue(crc == (data6[6] << 8 | data6[7])); byte[] data7 = new byte[] { 0x01, 0x0F, 0x00, 0x00, 0x00, 0x0A, 0xD5, 0xCC }; crc = ModbusRtuProtocol.calculateCrc(data7, 6); Assert.IsTrue(crc == (data7[6] << 8 | data7[7])); byte[] data8 = new byte[] { 0x01, 0x10, 0x00, 0x00, 0x00, 0x0A, 0x40, 0x0E }; crc = ModbusRtuProtocol.calculateCrc(data8, 6); Assert.IsTrue(crc == (data8[6] << 8 | data8[7])); }
public void ModbusRtuProtocol_modbusRtuCrc() { byte[] data1 = new byte[] { 0x01, 0x02, 0x00, 0x00, 0x00, 0x0A, 0xF8, 0x0D }; //correct crc Assert.IsTrue(ModbusRtuProtocol.modbusRtuCrc(data1) == ProtocolError.OK); byte[] data2 = new byte[] { 0x01, 0x02, 0x00, 0x00, 0x00, 0x0A, 0xF8, 0x0C }; //wrong crc Assert.IsTrue(ModbusRtuProtocol.modbusRtuCrc(data2) == ProtocolError.CRCError); byte[] data3 = new byte[] { 0x01 }; Assert.IsTrue(ModbusRtuProtocol.modbusRtuCrc(data3) == ProtocolError.MsgLengthError); }
public void ModbusRtuProtocol_validateResponse() { /* * GlobalObject go = new GlobalObject(); * go.varList = new System.Collections.Generic.LinkedList<Var>(); * go.varList.AddLast(new Var("var1", VarType.Int16, new ModbusAddress(Space.Coils, 0x00))); * go.varList.AddLast(new Var("var2", VarType.Int16, new ModbusAddress(Space.Coils, 0x01))); * PrivateObject auxPrivateObject = new PrivateObject(go); * auxPrivateObject.Invoke("buildAddrVarMap"); */ Query query = new Query(); //query.data = new UInt16[] { 0x01, 0x01, 0x00, 0x00, 0x00, 0x0A, 0xBC, 0x0D }; query.function = 1; query.startTag = 0; query.tagNum = 10; //query.globalObject = go; byte[] respMsg1 = new byte[] { 0x01, 0x01, 0x02, 0x55, 0x40, 0x00, 0x00 }; UInt16 crc = ModbusRtuProtocol.calculateCrc(respMsg1, 5); respMsg1[5] = (byte)(crc >> 8); respMsg1[6] = (byte)crc; ModbusRtuProtocol modbusRtuProtocol = new ModbusRtuProtocol(); modbusRtuProtocol.deviceAddress = 1; modbusRtuProtocol.query = query; PrivateObject po = new PrivateObject(modbusRtuProtocol); ProtocolError pe = (ProtocolError)po.Invoke("validateResponse", new Object[] { respMsg1 }); Assert.IsTrue(pe == ProtocolError.OK); // Assert.IsTrue(modbusRtuProtocol.query.globalObject.addrVarMap[new ModbusAddress(Space.Coils, 0x00)].UInt16Value == 0 ); // Assert.IsTrue(modbusRtuProtocol.query.globalObject.addrVarMap[new ModbusAddress(Space.Coils, 0x01)].UInt16Value == 1); }
public void ModbusRtuProtocol_buildResponse_Test() { byte[] msg; byte[] resp; byte[] ans; UInt16 crc; bool[] coils, d_inputs; UInt16[] a_inputs, holdings; GlobalObject go = new GlobalObject(); ModbusRtuProtocol mrp = new ModbusRtuProtocol(); mrp.deviceAddress = 1; mrp.globalObject = go; mrp.globalObject.tagStorage.setTag(new ModbusAddress(Space.Coils, 0), 1); //--------------- // function 1 coils = new bool[] { true, false, true, true, false, false, true, true, true, false, false, false, true, true, true, true }; for (int i = 0; i < 16; i++) { go.tagStorage.setTag(new ModbusAddress(Space.Coils, (UInt16)i), (UInt16)((coils[i]) ? 0xFF00: 0)); } ; msg = new byte[] { 01, 01, 00, 00, 00, 16, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(msg, 8); msg[6] = (byte)(crc >> 8); msg[7] = (byte)crc; resp = mrp.buildResponse(msg); ans = new byte[] { 01, 01, 02, 0xCD, 0xF1, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(ans, 5); ans[5] = (byte)(crc >> 8); ans[6] = (byte)crc; Assert.IsTrue(resp.Length == 7); for (int i = 0; i < resp.Length; i++) { Assert.IsTrue(resp[i] == ans[i]); } //---------------------------- // function 2 d_inputs = new bool[] { true, false, true, true, false, false, true, true, true, false, false, false, true, true, true, true }; for (int i = 0; i < 16; i++) { go.tagStorage.setTag(new ModbusAddress(Space.DiscreteInputs, (UInt16)i), (UInt16)((d_inputs[i]) ? 0xFF00 : 0)); } ; msg = new byte[] { 01, 02, 00, 00, 00, 16, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(msg, 8); msg[6] = (byte)(crc >> 8); msg[7] = (byte)crc; resp = mrp.buildResponse(msg); ans = new byte[] { 01, 02, 02, 0xCD, 0xF1, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(ans, 5); ans[5] = (byte)(crc >> 8); ans[6] = (byte)crc; Assert.IsTrue(resp.Length == 7); for (int i = 0; i < resp.Length; i++) { Assert.IsTrue(resp[i] == ans[i]); } //----------------- //function 3 holdings = new UInt16[] { 0x0102, 0x0304, 0x0506 }; for (int i = 0; i < 3; i++) { go.tagStorage.setTag(new ModbusAddress(Space.Holdings, (UInt16)i), holdings[i]); } ; msg = new byte[] { 01, 03, 00, 00, 00, 03, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(msg, 8); msg[6] = (byte)(crc >> 8); msg[7] = (byte)crc; resp = mrp.buildResponse(msg); ans = new byte[] { 01, 03, 06, 01, 02, 03, 04, 05, 06, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(ans, 9); ans[9] = (byte)(crc >> 8); ans[10] = (byte)crc; Assert.IsTrue(resp.Length == 11); for (int i = 0; i < resp.Length; i++) { Assert.IsTrue(resp[i] == ans[i]); } //----------------- //function 4 a_inputs = new UInt16[] { 0x0102, 0x0304, 0x0506 }; for (int i = 0; i < 3; i++) { go.tagStorage.setTag(new ModbusAddress(Space.AnalogInputs, (UInt16)i), a_inputs[i]); } ; msg = new byte[] { 01, 04, 00, 00, 00, 03, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(msg, 6); msg[6] = (byte)(crc >> 8); msg[7] = (byte)crc; resp = mrp.buildResponse(msg); ans = new byte[] { 01, 04, 06, 01, 02, 03, 04, 05, 06, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(ans, 9); ans[9] = (byte)(crc >> 8); ans[10] = (byte)crc; Assert.IsTrue(resp.Length == 11); for (int i = 0; i < resp.Length; i++) { Assert.IsTrue(resp[i] == ans[i]); } //----------------- // function 5 msg = new byte[] { 01, 05, 200, 100, 0xFF, 0x00, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(msg, 6); msg[6] = (byte)(crc >> 8); msg[7] = (byte)crc; resp = mrp.buildResponse(msg); ans = new byte[] { 01, 05, 200, 100, 0xFF, 0x00, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(ans, 6); ans[6] = (byte)(crc >> 8); ans[7] = (byte)crc; Assert.IsTrue(resp.Length == 8); for (int i = 0; i < resp.Length; i++) { Assert.IsTrue(resp[i] == ans[i]); } //----------------- // function 6 msg = new byte[] { 01, 06, 200, 100, 0xAA, 0x55, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(msg, 6); msg[6] = (byte)(crc >> 8); msg[7] = (byte)crc; resp = mrp.buildResponse(msg); ans = new byte[] { 01, 06, 200, 100, 0xAA, 0x55, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(ans, 6); ans[6] = (byte)(crc >> 8); ans[7] = (byte)crc; Assert.IsTrue(resp.Length == 8); for (int i = 0; i < resp.Length; i++) { Assert.IsTrue(resp[i] == ans[i]); } Assert.IsTrue(go.tagStorage.getTag(new ModbusAddress(Space.Holdings, (UInt16)(200 * 256 + 100))) != 0); //--------------- // function 15 msg = new byte[] { 01, 15, 00, 00, 00, 08, 01, 55, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(msg, 8); msg[8] = (byte)(crc >> 8); msg[9] = (byte)crc; resp = mrp.buildResponse(msg); ans = new byte[] { 01, 15, 00, 00, 00, 08, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(ans, 6); ans[6] = (byte)(crc >> 8); ans[7] = (byte)crc; Assert.IsTrue(resp.Length == 8); for (int i = 0; i < resp.Length; i++) { Assert.IsTrue(resp[i] == ans[i]); } coils = new bool[] { true, true, true, false, true, true, false, false }; for (int i = 0; i < 8; i++) { Assert.IsTrue((go.tagStorage.getTag(Space.Coils, i) != 0) == coils[i]);// coils[i] == coils[i]); } //--------------- // function 16 msg = new byte[] { 01, 16, 00, 00, 00, 03, 06, 00, 01, 02, 03, 04, 05, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(msg, 12); msg[13] = (byte)(crc >> 8); msg[14] = (byte)crc; resp = mrp.buildResponse(msg); ans = new byte[] { 01, 16, 00, 00, 00, 03, 00, 00 }; crc = ModbusRtuProtocol.calculateCrc(ans, 6); ans[6] = (byte)(crc >> 8); ans[7] = (byte)crc; Assert.IsTrue(resp.Length == 8); for (int i = 0; i < resp.Length; i++) { Assert.IsTrue(resp[i] == ans[i]); } holdings = new UInt16[] { 0x0001, 0x0203, 0x0405 }; for (int i = 0; i < 3; i++) { Assert.IsTrue(go.tagStorage.getTag(Space.Holdings, i) == holdings[i]); } }
public void ModbusRtuProtocol_decode() { GlobalObject go = new GlobalObject(); go.varList = new System.Collections.Generic.LinkedList <Var>(); go.varList.AddLast(new Var("var1-1", VarType.Int16, new ModbusAddress(Space.Coils, 0x00))); go.varList.AddLast(new Var("var1-2", VarType.Int16, new ModbusAddress(Space.Coils, 0x01))); go.varList.AddLast(new Var("var1-3", VarType.Int16, new ModbusAddress(Space.Coils, 0x02))); go.varList.AddLast(new Var("var1-4", VarType.Int16, new ModbusAddress(Space.Coils, 0x03))); go.varList.AddLast(new Var("var2-1", VarType.Int16, new ModbusAddress(Space.DiscreteInputs, 0x00))); go.varList.AddLast(new Var("var2-2", VarType.Int16, new ModbusAddress(Space.DiscreteInputs, 0x01))); go.varList.AddLast(new Var("var2-3", VarType.Int16, new ModbusAddress(Space.DiscreteInputs, 0x02))); go.varList.AddLast(new Var("var2-4", VarType.Int16, new ModbusAddress(Space.DiscreteInputs, 0x03))); go.varList.AddLast(new Var("var3-1", VarType.Int16, new ModbusAddress(Space.AnalogInputs, 0x00))); go.varList.AddLast(new Var("var3-2", VarType.Int16, new ModbusAddress(Space.AnalogInputs, 0x01))); go.varList.AddLast(new Var("var3-3", VarType.Int16, new ModbusAddress(Space.AnalogInputs, 0x02))); go.varList.AddLast(new Var("var3-4", VarType.Int16, new ModbusAddress(Space.AnalogInputs, 0x03))); go.varList.AddLast(new Var("var4-1", VarType.Int16, new ModbusAddress(Space.Holdings, 0x00))); go.varList.AddLast(new Var("var4-2", VarType.Int16, new ModbusAddress(Space.Holdings, 0x01))); go.varList.AddLast(new Var("var4-3", VarType.Int16, new ModbusAddress(Space.Holdings, 0x02))); go.varList.AddLast(new Var("var4-4", VarType.Int16, new ModbusAddress(Space.Holdings, 0x03))); PrivateObject auxPrivateObject = new PrivateObject(go); auxPrivateObject.Invoke("buildAddrVarMap"); ModbusRtuProtocol modbusRtuProtocol = new ModbusRtuProtocol(); Query query = new Query(); // query.data = new UInt16[] { 0x01, 0x01, 0x00, 0x00, 0x00, 0x0A, 0xBC, 0x0D }; query.function = 1; query.startTag = 0; query.tagNum = 10; query.globalObject = go; byte[] respMsg1 = new byte[] { 0x01, 0x01, 0x02, 0x55, 0x40, 0x00, 0x00 }; UInt16 crc = ModbusRtuProtocol.calculateCrc(respMsg1, 5); respMsg1[5] = (byte)(crc >> 8); respMsg1[6] = (byte)crc; modbusRtuProtocol.deviceAddress = 1; modbusRtuProtocol.query = query; modbusRtuProtocol.globalObject = go; Response response = new Response(query); ProtocolError pe = modbusRtuProtocol.decode(respMsg1, query, response); Assert.IsTrue(pe == ProtocolError.OK); Assert.IsTrue(modbusRtuProtocol.query.globalObject.addrVarMap[new ModbusAddress(Space.Coils, 0x00)].uInt16Value == 1); Assert.IsTrue(modbusRtuProtocol.query.globalObject.addrVarMap[new ModbusAddress(Space.Coils, 0x01)].uInt16Value == 0); Assert.IsTrue(modbusRtuProtocol.query.globalObject.addrVarMap[new ModbusAddress(Space.Coils, 0x02)].uInt16Value == 1); Assert.IsTrue(modbusRtuProtocol.query.globalObject.addrVarMap[new ModbusAddress(Space.Coils, 0x03)].uInt16Value == 0); respMsg1[6] ^= respMsg1[6]; //make the crc wrong pe = modbusRtuProtocol.decode(respMsg1, query, response); Assert.IsTrue(pe == ProtocolError.CRCError); //дописать тесты для других функций }