コード例 #1
0
        internal override IEnumerable <byte> OnSerialize(IModbusMessage message)
        {
            if (message is ModbusRequest request)
            {
                request.TransactionID = transactionID++;
            }

            var messageArray  = message.Serialize().ToArray();
            int messageLength = messageArray.Length;

            yield return((byte)((message.TransactionID >> 8) & 0xff));

            yield return((byte)(message.TransactionID & 0xff));

            yield return(0);

            yield return(0);

            yield return((byte)((messageLength >> 8) & 0xff));

            yield return((byte)(messageLength & 0xff));

            foreach (var b in messageArray)
            {
                yield return(b);
            }
        }
コード例 #2
0
        internal override IEnumerable <byte> OnSerialize(IModbusMessage message)
        {
            ushort crc = ushort.MaxValue;

            foreach (var b in message.Serialize())
            {
                byte tableIndex = (byte)(crc ^ b);
                crc >>= 8;
                crc  ^= crcTable[tableIndex];
                yield return(b);
            }

            foreach (var b in BitConverter.GetBytes(crc))
            {
                yield return(b);
            }
        }
コード例 #3
0
        internal override IEnumerable <byte> OnSerialize(IModbusMessage message)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append(":");

            byte lrc = 0;

            foreach (var b in message.Serialize())
            {
                stringBuilder.AppendFormat("{0:X2}", b);
                lrc += b;
            }

            lrc = (byte)(-lrc & 0xff);
            stringBuilder.AppendFormat("{0:X2}", lrc);
            stringBuilder.Append("\r\n");

            return(Encoding.ASCII.GetBytes(stringBuilder.ToString()));
        }