示例#1
0
        private static void WritePacket(params byte[][] data)
        {
            var buff     = new byte[1024];
            var len      = 0;
            var buffUsed = 0;

            buff[len++] = STX;
            buff[len++] = STX;

            Action <byte> put = b => {
                if (b == STX || b == ETX || b == DLE)
                {
                    buff[len++] = DLE;
                }
                buff[len++] = b;
                buffUsed++;
            };

            var crc = new Crc16Ccitt();

            foreach (var b in data.SelectMany(arr => arr))
            {
                put(b);
                crc.Update(b);
            }
            put((byte)(crc.Value >> 8));
            put((byte)crc.Value);

            if (buffUsed > 255)
            {
                throw new Exception();
            }

            buff[len++] = ETX;

#if LOOPBACK
            port.DiscardInBuffer();
            port.Write(buff, 0, len);
            while (len > port.BytesToRead)
            {
                Thread.Sleep(1);
            }
            port.DiscardInBuffer();
#else
            port.Write(buff, 0, len);
            Thread.Sleep(300);
#endif
        }
示例#2
0
        private static void WritePacket(params byte[][] data)
        {
            var buff = new byte[1024];
            var len = 0;
            var buffUsed = 0;

            buff[len++] = STX;
            buff[len++] = STX;

            Action<byte> put = b => {
                if (b == STX || b == ETX || b == DLE)
                    buff[len++] = DLE;
                buff[len++] = b;
                buffUsed++;
            };

            var crc = new Crc16Ccitt();
            foreach (var b in data.SelectMany(arr => arr)) {
                put(b);
                crc.Update(b);
            }
            put((byte)(crc.Value >> 8));
            put((byte)crc.Value);

            if (buffUsed > 255) {
                throw new Exception();
            }

            buff[len++] = ETX;

#if LOOPBACK
            port.DiscardInBuffer();
            port.Write(buff, 0, len);
            while (len > port.BytesToRead) {
                Thread.Sleep(1);
            }
            port.DiscardInBuffer();
#else
            port.Write(buff, 0, len);
            Thread.Sleep(300);
#endif
        }