Exemplo n.º 1
0
        public TransferReply TransferAndRead(byte[] data, int timeoutMS)
        {
            // send the first command
            // 0 to 60 inclusively is the maximum length of data (64bytes SPI data minus 4bytes header)
            if (data == null || data.Length > 60)
            {
                throw new ArgumentException("Spi data transfer must be non-null and should not exceed 60 bytes size.");
            }

            // create the packet
            byte[] packet = new byte[Constants.PacketsSize];
            packet[0] = CommandCodes.TransferSpiData;
            packet[1] = (byte)data.Length;

            for (int i = 0; i < data.Length; i++)
            {
                packet[i + 4] = data[i];
            }

            // write on device and read the reply
            byte[] reply = _hidHandler.WriteData(packet);

            // check for errors
            if (reply[0] != CommandCodes.TransferSpiData)
            {
                throw new PacketReplyFormatException();
            }

            // continuously read the SPI bus status and wait till the data is ready
            DateTime timeout = DateTime.Now.AddMilliseconds(timeoutMS);

            while (reply[3] != ReplyStatusCodes.SPITransferFinished)
            {
                // check for timeout
                if (timeout <= DateTime.Now)
                {
                    throw new TimeoutException("SPI device is not replying.");
                }

                reply = _hidHandler.WriteData(packet);
            }

            // create the reply data
            TransferReply transfReply = new TransferReply();

            switch (reply[3])
            {
            case ReplyStatusCodes.SPITransferStarted:
                transfReply.TransferStatus = SpiTransferStatus.Started;
                break;

            case ReplyStatusCodes.SPITransferNotFinished:
                transfReply.TransferStatus = SpiTransferStatus.Waiting;
                break;

            case ReplyStatusCodes.SPITransferFinished:
                transfReply.TransferStatus = SpiTransferStatus.Ended;
                break;

            default:
                throw new NotImplementedException();
            }

            // read the SPI return data
            int dataLength = reply[2];

            transfReply.Data = new byte[dataLength];
            for (int i = 0; i < dataLength; i++)
            {
                transfReply.Data[i] = reply[i + 4];
            }

            return(transfReply);
        }
Exemplo n.º 2
0
        public TransferReply Transfer(byte[] data)
        {
            // 0 to 60 inclusively is the maximum length of data (64bytes SPI data minus 4bytes header)
            if (data == null || data.Length > 60)
            {
                throw new ArgumentException("Spi data transfer must be non-null and should not exceed 60 bytes size.");
            }

            // create the packet
            byte[] packet = new byte[Constants.PacketsSize];
            packet[0] = CommandCodes.TransferSpiData;
            packet[1] = (byte)data.Length;

            for (int i = 0; i < data.Length; i++)
            {
                packet[i + 4] = data[i];
            }

            // write on device and read the reply
            byte[] reply = _hidHandler.WriteData(packet);

            // check for errors
            if (reply[0] != CommandCodes.TransferSpiData)
            {
                throw new PacketReplyFormatException();
            }

            switch (reply[1])
            {
            case ReplyStatusCodes.CompletedSuccessfully:
                break;

            case ReplyStatusCodes.SpiBusNotAvailable:
                throw new SpiBusNotAvailableException();

            case ReplyStatusCodes.SpiTransferInProgress:
                throw new SpiTransferInProgressException();

            default:
                throw new NotImplementedException();
            }

            // create the reply data
            TransferReply transfReply = new TransferReply();

            switch (reply[3])
            {
            case ReplyStatusCodes.SPITransferStarted:
                transfReply.TransferStatus = SpiTransferStatus.Started;
                break;

            case ReplyStatusCodes.SPITransferNotFinished:
                transfReply.TransferStatus = SpiTransferStatus.Waiting;
                break;

            case ReplyStatusCodes.SPITransferFinished:
                transfReply.TransferStatus = SpiTransferStatus.Ended;
                break;

            default:
                throw new NotImplementedException();
            }

            // read the SPI return data
            int dataLength = reply[2];

            transfReply.Data = new byte[dataLength];
            for (int i = 0; i < dataLength; i++)
            {
                transfReply.Data[i] = reply[i + 4];
            }

            return(transfReply);
        }