Ejemplo n.º 1
0
        /// <summary>
        /// Puts the address in the front and the correct checksum at the end of the supplied
        /// data array.
        /// </summary>
        /// <param name="data">Data array.</param>
        /// <param name="address">Target device address.</param>
        /// <returns>Complete data frame.</returns>
        private protected byte[] AddAddressAndChecksum(byte[] data, int address)
        {
            byte[] transmitBuffer = new byte[data.Length + 2];
            Array.Copy(data, 0, transmitBuffer, 1, data.Length);

            transmitBuffer[0] = (byte)address;
            transmitBuffer[transmitBuffer.Length - 1] = CRC8.Calculate(transmitBuffer, 0, transmitBuffer.Length - 1);

            return(transmitBuffer);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Read bytes from the USB device
        /// </summary>
        /// <param name="offset">Location into the file (address in device)</param>
        /// <param name="length">Number of bytes to read</param>
        /// <param name="ok">Boolean indicating whether the read was successfull</param>
        /// <returns>An array of bytes read from the device</returns>
        private static byte[] ReadUsb(int offset, int length, out bool ok)
        {
            var response = new byte[length];
            var answer   = new byte[133];

            bool nak = false;

            var loop = length / 128;

            if ((length % 128) > 0)
            {
                loop++;
            }

            int  read = 0;
            byte nb   = 128;
            int  badDataRetryCount = 0;
            bool badData           = false;

            do /* Each 128 byte (max) frame */
            {
                badDataRetryCount = 0;
                do /* Retry up to BAD_DATA_RETRIES times when bad data is read */
                {
                    numRequests++;
                    badData = false;

                    if ((length - read) < 128)
                    {
                        nb = (byte)(length - read);
                    }

                    var offsetBytes = BitConverter.GetBytes(offset);
                    request[4] = offsetBytes[0];
                    request[5] = offsetBytes[1];
                    request[6] = offsetBytes[2];
                    request[7] = offsetBytes[3];
                    request[8] = nb;
                    request[9] = crc.Calculate(request.Skip(3).Take(6).ToArray());
                    winUsbDevice.Write(request, 11);

                    DateTime timeout = DateTime.Now;

                    int r = 0;
                    try
                    {
                        while ((r += winUsbDevice.Read(answer, r, nb + 5 - r)) < nb + 5)
                        {
                            /* Read into answer until full chunk is recieved */
                            System.Threading.Thread.Sleep(0);

                            /*
                             * XXX: TODO:
                             * On ZModem that does not support 0x02 log message
                             * When operating via USB this returns a NAK
                             * then ends up in an infinate loop.
                             * WONT affect commercial products (ZModem is serial not USB)
                             */
                            if ((DateTime.Now - timeout) > TimeSpan.FromMilliseconds(2000))
                            {
                                System.Diagnostics.Debug.WriteLine(string.Format("Error : Timeout - {0}.", offset));
                                break;
                            }
                        }
                    }
                    catch
                    {
                        /* Typically timeout */
                        nak     = true;
                        badData = true;
                    }

                    if (r > 0)
                    {
                        /* Error checking */

                        /* Device said NAK */
                        if (answer[r - 1] == NAK)
                        {
                            badData = true; /* nak = true; */
                            System.Diagnostics.Debug.WriteLine(string.Format("Error : NAK - {0}.", offset));
                        }

                        /* Check for device ACK */
                        if (answer[nb + 4] != ACK)
                        {
                            badData = true;
                            System.Diagnostics.Debug.WriteLine(string.Format("Error : Not ACK - {0}.", offset));
                        }

                        /* STX header */
                        if (answer[0] != STX)
                        {
                            badSTXCount++;
                            System.Diagnostics.Debug.WriteLine(string.Format("Error : Not STX - {0}.", offset));
                            badData = true;
                        }

                        /* MsgID header */
                        if (answer[1] != MSG_ID_GET_LOG)
                        {
                            badMsgCount++;
                            System.Diagnostics.Debug.WriteLine(string.Format("Error : Wrong Msg ID - {0}.", offset));
                            badData = true;
                        }

                        /* Number of bytes read matches request */
                        if (answer[2] != nb)
                        {
                            badBytesReadCount++;
                            System.Diagnostics.Debug.WriteLine(string.Format("Error : Wrong number of bytes - {0}.", offset));
                            badData = true;
                        }

                        /* CRC */
                        CRC8 frameCrc = new CRC8(0x8c);
                        byte crcVal   = frameCrc.Calculate(answer.Skip(3).Take(nb).ToArray());
                        if (answer[nb + 3] != crcVal)
                        {
                            badCRCCount++;
                            System.Diagnostics.Debug.WriteLine(string.Format("Error : Wrong CRC - {0}.", offset));
                            badData = true;
                        }
                    }

                    if (nak == true)
                    {
                        break;
                    }

                    if (badData)
                    {
                        badDataRetryCount++;
                        var byteToRead = winUsbDevice.BytesToRead;
                        if (byteToRead > 0)
                        {
                            System.Diagnostics.Debug.WriteLine(string.Format("Error : Flush the buffer of {1} bytes - {0}.", offset, byteToRead));
                            winUsbDevice.Read(new byte[byteToRead]);
                        }
                    }
                    else
                    {
                        if (r > 0)
                        {
                            Array.Copy(answer, 3, response, read, nb);
                            offset += nb;
                            read   += nb;
                        }
                        else
                        {
                            badData = true;
                        }
                    }
                }while (badData && (badDataRetryCount < BAD_DATA_RETRIES));

                if (badData)
                {
                    /*
                     * If we still had bad data after BAD_DATA_RETRIES then give up with NAK
                     * Read() function will be called once more externally on NAK (ok = false)
                     */
                    nak = true;
                    break;
                }
            }while (--loop > 0);  /* Next 128 byte frame */

            ok = !nak;
            return(response);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Read bytes from the device
        /// </summary>
        /// <param name="offset">Location or address in the device</param>
        /// <param name="length">Number of bytes to read</param>
        /// <param name="ok">A ref to a bool that indicates success</param>
        /// <returns>An array of bytes read from the device</returns>
        private static byte[] Read(int offset, int length, out bool ok)
        {
            if (!isSerial)
            {
                return(ReadUsb(offset, length, out ok));
            }

            var response = new byte[length];
            var answer   = new byte[133];

            bool nak = false;

            var loop = length / 128;

            if ((length % 128) > 0)
            {
                loop++;
            }

            int  read = 0;
            byte nb   = 128;
            int  badDataRetryCount = 0;
            bool badData           = false;

            do     /* Each 128 byte (max) frame */
            {
                do /* Retry up to BAD_DATA_RETRIES times when bad data is read */
                {
                    badData = false;

                    if ((length - read) < 128)
                    {
                        nb = (byte)(length - read);
                    }

                    var offsetBytes = BitConverter.GetBytes(offset);
                    request[4] = offsetBytes[0];
                    request[5] = offsetBytes[1];
                    request[6] = offsetBytes[2];
                    request[7] = offsetBytes[3];
                    request[8] = nb;
                    request[9] = crc.Calculate(request.Skip(3).Take(6).ToArray());
                    vcp.Write(request, 0, 11);

                    int r = 0;
                    try
                    {
                        while ((r += vcp.Read(answer, r, nb + 5 - r)) < nb + 5)
                        {
                            /* Read into answer until full chunk is recieved */
                        }
                    }
                    catch
                    {
                        /* Typically timeout*/
                        nak = true;
                    }

                    if (r > 0)
                    {
                        /* Error checking */

                        /* Device said NAK */
                        if (answer[r - 1] == NAK)
                        {
                            nak = true;
                            break;
                        }

                        /* Check for device ACK */
                        if (answer[nb + 4] != ACK)
                        {
                            badData = true;
                        }

                        /* STX header */
                        if (answer[0] != STX)
                        {
                            badData = true;
                        }

                        /* MsgID header */
                        if (answer[1] != MSG_ID_GET_LOG)
                        {
                            badData = true;
                        }

                        /* Number of bytes read matches request */
                        if (answer[2] != nb)
                        {
                            badData = true;
                        }

                        /* CRC */
                        CRC8 frameCrc = new CRC8(0x8c);
                        byte crcVal   = frameCrc.Calculate(answer.Skip(3).Take(nb).ToArray());
                        if (answer[nb + 3] != crcVal)
                        {
                            badData = true;
                        }
                    }

                    if (nak == true)
                    {
                        break;
                    }

                    if (badData)
                    {
                        badDataRetryCount++;
                    }
                    else
                    {
                        Array.Copy(answer, 3, response, read, nb);
                        offset += nb;
                        read   += nb;
                    }
                }while (badData && badDataRetryCount < BAD_DATA_RETRIES);

                if (badData)
                {
                    /* If we still had bad data after BAD_DATA_RETRIES then give up with NAK
                     * Read() function will be called once more externally on NAK (ok = false)
                     */
                    nak = true;
                    break;
                }
            }while (--loop > 0);  /* Next 128 byte frame */

            ok = !nak;
            return(response);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Syncronously reads data from the BioHarness.
        /// Handles the maximum message size constraints, splitting the
        /// request into manageable chunks, and reassembling the response.
        /// </summary>
        /// <param name="offset">The byte to start read at</param>
        /// <param name="length">The number of bytes to read</param>
        /// <returns>A byte array conaining the requested data</returns>
        private static byte[] SyncRead(int offset, int length)
        {
            CRC8 crc      = new CRC8(0x8c);
            var  response = new byte[length];
            var  answer   = new byte[133];

            /* request = STX, MSG_ID_LOG,  DLC, ... ETX */
            byte[] request = new byte[] { STX, MSG_ID_GET_LOG, DLC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ETX };

            var loop = length / 128;

            if ((length % 128) > 0)
            {
                loop++;
            }

            int  read = 0;
            byte nb   = 128;

            do
            {
                if ((length - read) < 128)
                {
                    nb = (byte)(length - read);
                }

                var offsetBytes = BitConverter.GetBytes(offset);
                request[4] = offsetBytes[0];
                request[5] = offsetBytes[1];
                request[6] = offsetBytes[2];
                request[7] = offsetBytes[3];
                request[8] = nb;
                request[9] = crc.Calculate(request.Skip(3).Take(6).ToArray());
                if (isSerial)
                {
                    vcp.Write(request, 0, 11);
                }
                else
                {
                    winUsbDevice.Write(request, 11);
                }

                int r = 0;
                if (isSerial)
                {
                    while ((r += vcp.Read(answer, r, nb + 5 - r)) < nb + 5)
                    {
                    }
                }
                else
                {
                    while ((r += winUsbDevice.Read(answer, r, nb + 5 - r)) < nb + 5)
                    {
                    }
                }

                Array.Copy(answer, 3, response, read, nb);
                offset += nb;
                read   += nb;
            }while (--loop > 0);

            return(response);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Syncronously reads data from the BioHarness.
        /// Handles the maximum message size constraints, splitting the
        /// request into manageable chunks, and reassembling the response.
        /// </summary>
        /// <param name="offset">The byte to start read at</param>
        /// <param name="length">The number of bytes to read</param>        
        /// <returns>A byte array conaining the requested data</returns>
        private static byte[] SyncRead(int offset, int length)
        {
            CRC8 crc = new CRC8(0x8c);
            var response = new byte[length];
            var answer = new byte[133];
            /* request = STX, MSG_ID_LOG,  DLC, ... ETX */
            byte[] request = new byte[] { STX, MSG_ID_GET_LOG, DLC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ETX };

            var loop = length / 128;
            if ((length % 128) > 0)
            {
                loop++;
            }

            int read = 0;
            byte nb = 128;
            do
            {
                if ((length - read) < 128)
                {
                    nb = (byte)(length - read);
                }

                var offsetBytes = BitConverter.GetBytes(offset);
                request[4] = offsetBytes[0];
                request[5] = offsetBytes[1];
                request[6] = offsetBytes[2];
                request[7] = offsetBytes[3];
                request[8] = nb;
                request[9] = crc.Calculate(request.Skip(3).Take(6).ToArray());

                vcp.Write(request, 0, 11);

                int r = 0;
                while ((r += vcp.Read(answer, r, nb + 5 - r)) < nb + 5)
                {
                }

                Array.Copy(answer, 3, response, read, nb);
                offset += nb;
                read += nb;
            }
            while (--loop > 0);

            return response;
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Read bytes from the device
        /// </summary>
        /// <param name="offset">Location or address in the device</param>
        /// <param name="length">Number of bytes to read</param>
        /// <param name="ok">A ref to a bool that indicates success</param>
        /// <returns>An array of bytes read from the device</returns>
        private static byte[] Read(int offset, int length, out bool ok)
        {
            var response = new byte[length];
            var answer = new byte[133];

            bool nak = false;

            var loop = length / 128;
            if ((length % 128) > 0)
            {
                loop++;
            }

            int read = 0;
            byte nb = 128;
            int badDataRetryCount = 0;
            bool badData = false;

            do /* Each 128 byte (max) frame */
            {
                do /* Retry up to BAD_DATA_RETRIES times when bad data is read */
                {
                    badData = false;

                    if ((length - read) < 128)
                    {
                        nb = (byte)(length - read);
                    }

                    var offsetBytes = BitConverter.GetBytes(offset);
                    request[4] = offsetBytes[0];
                    request[5] = offsetBytes[1];
                    request[6] = offsetBytes[2];
                    request[7] = offsetBytes[3];
                    request[8] = nb;
                    request[9] = crc.Calculate(request.Skip(3).Take(6).ToArray());
                    vcp.Write(request, 0, 11);

                    int r = 0;
                    try
                    {
                        while ((r += vcp.Read(answer, r, nb + 5 - r)) < nb + 5)
                        {
                            /* Read into answer until full chunk is recieved */
                        }
                    }
                    catch
                    {
                        /* Typically timeout*/
                        nak = true;
                    }

                    if (r > 0)
                    {
                        /* Error checking */

                        /* Device said NAK */
                        if (answer[r - 1] == NAK)
                        {
                            nak = true;
                            break;
                        }

                        /* Check for device ACK */
                        if (answer[nb + 4] != ACK)
                        {
                            badData = true;
                        }

                        /* STX header */
                        if (answer[0] != STX)
                        {
                            badData = true;
                        }

                        /* MsgID header */
                        if (answer[1] != MSG_ID_GET_LOG)
                        {
                            badData = true;
                        }

                        /* Number of bytes read matches request */
                        if (answer[2] != nb)
                        {
                            badData = true;
                        }

                        /* CRC */
                        CRC8 frameCrc = new CRC8(0x8c);
                        byte crcVal = frameCrc.Calculate(answer.Skip(3).Take(nb).ToArray());
                        if (answer[nb + 3] != crcVal)
                        {
                            badData = true;
                        }
                    }

                    if (nak == true)
                    {
                        break;
                    }

                    if (badData)
                    {
                        badDataRetryCount++;
                    }
                    else
                    {
                        Array.Copy(answer, 3, response, read, nb);
                        offset += nb;
                        read += nb;
                    }
                }
                while (badData && badDataRetryCount < BAD_DATA_RETRIES);

                if (badData)
                {
                    /* If we still had bad data after BAD_DATA_RETRIES then give up with NAK
                     * Read() function will be called once more externally on NAK (ok = false)
                     */
                    nak = true;
                    break;
                }
            }
            while (--loop > 0);  /* Next 128 byte frame */

            ok = !nak;
            return response;
        }