Exemplo n.º 1
0
        /// <summary>
        /// WriteData
        /// </summary>
        /// <param name="command">command data to send</param>
        /// <param name="ack">true if ack is epected</param>
        /// <param name="retries">number of retries</param>
        /// <returns></returns>
        private byte[] WriteData(byte[] command, bool ack = true, int retries = 5)
        {
            byte[] ackBuffer = new byte[2];
            bool   done      = false;

            while ((!done) && (retries-- > 0))
            {
                SendCommandAsync(command).Wait();

                if (ack)
                {
                    LoadResponseAsync(2).Wait();
                    DataReaderObject.ReadBytes(ackBuffer);
                    done = 0xEE07 != (ackBuffer[0] << 8 | ackBuffer[1]);
                }
                else
                {
                    done = true;
                }
            }

            if (!done)
            {
                throw new Exception("WriteData retries exceeded");
            }
            return(ackBuffer);
        }
Exemplo n.º 2
0
        /// <summary>
        /// ReadData
        /// </summary>
        /// <param name="data">Array of data read from device</param>
        /// <returns></returns>
        private byte[] ReadData(byte[] command, uint readLength)
        {
            byte[] headerBuffer = new byte[2];
            byte[] returnBuffer = new byte[readLength];

            SendCommandAsync(command).Wait();

            LoadResponseAsync(2).Wait();
            DataReaderObject.ReadBytes(headerBuffer);

            if (headerBuffer[0] != 0xBB)
            {
                throw new Exception(string.Format("ReadData error 0x{0:x2}{0:x2}", headerBuffer[0], headerBuffer[1]));
            }
            if (headerBuffer[1] != readLength)
            {
                throw new Exception(string.Format("ReadData error: failed to read {0} bytes.  Read {1}", readLength, headerBuffer[1]));
            }

            LoadResponseAsync(readLength).Wait();
            DataReaderObject.ReadBytes(returnBuffer);

            return(returnBuffer);
        }