public void TestMBMessage() { byte[] bs = ValueHelper.StrToToHexByte("00 86 00 00 00 06 01 01 01 01 00 02"); MBMessage mb = new MBMessage(bs); Assert.AreEqual(0x86, mb.TID); Assert.AreEqual(0, mb.PID); Assert.AreEqual(6, mb.Length); Assert.AreEqual(1, mb.UID); Assert.AreEqual(1, mb.FC); Assert.AreEqual(0x101, mb.GetWord(0)); Assert.AreEqual(0x2, mb.GetByte(3)); Assert.AreEqual(0x2, mb.GetWord(2)); }
private MBMessage AckReadCoil(MBMessage req) { MBMessage resp = new MBMessage(req); /** * req: * Byte 1-2:Reference number * Byte 3-4:Bit count (1-2000) * resp: * Response * Byte 1: Byte count of response (B=(bit count+7)/8) * Byte 2-(B+1):Bit values (least significant bit is first coil!)" */ ushort refNum = req.GetWord(0); ushort bitCount = req.GetWord(2); if (_cms.IsValidRegAddress((byte)refNum)) { byte[] byteValues = _cms.MB_ReadCoils(refNum, bitCount); // 写入. var bcnt = byteValues.Length; resp.SetBodySize((ushort)(bcnt + 1)); resp.SetByte(0, (byte)bcnt); for (byte i = 0; i < bcnt; i++) { resp.SetByte(i + 1, byteValues[i]); } } else { // Error Response. // Byte 0:FC = 81 (hex) // Byte 1:exception code = 01 or 02 resp.FC = 0x82; resp.SetBody(new byte[] { MBException.E02_ILLEGAL_DATA_ADDRESS }); } return resp; }
/* WriteCoil REQ: Byte 0:FC = 05 Byte 1-2: Reference number Byte 3:= FF to turn coil ON, =00 to turn coil OFF Byte 4:= 00 Resp: Byte 1-2:Reference number Byte 3:= FF to turn coil ON, =00 to turn coil OFF (echoed) Byte 4:= 00" */ private MBMessage AckWriteCoil(MBMessage req) { MBMessage resp = new MBMessage(req); // copy header ushort refNum = req.GetWord(0); // byte val = req.GetByte(2); byte result = _cms.MB_WriteCoil(refNum, val); // exception if (MBException.MB_SUCCESS == result) { // OK, return resp } else { // ERROR resp.FC = 0x85; resp.SetBody(new byte[] { result }); } return resp; }