Example #1
0
        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;
        }
Example #2
0
 MBMessage NewWriteCoil(ushort addr, bool onOff)
 {
     MBMessage req = new MBMessage();
     req.TID = 0xFF;
     req.UID = 0x01;
     req.PID = 0;
     req.FC = 0x05;
     req.SetBodySize(4);
     req.SetWord(0, addr); // 单樘门开门控制信号 - 读/写	0x11
     req.SetByte(2, onOff ? (byte)0xFF : (byte)0x00); // 读取16*6个字节
     req.SetByte(3, 0x00);
     return req;
 }