Exemple #1
0
 /// <summary>
 /// Sends and Receives data.
 /// </summary>
 /// <param name="address"></param>
 /// <param name="dataToWrite"></param>
 /// <param name="numBytesToRead"></param>
 /// <returns>Nothing. When data is received, an event will be generated</returns>
 public byte[] SendReceive(byte[] dataToWrite)
 {
     if (dataToWrite.Length > 255)
     {
         throw new Exception("Maximum packet length is 255 bytes");
     }
     byte[] returnedData = new byte[dataToWrite.Length];
     byte[] dataToSend   = new byte[2 + dataToWrite.Length];
     dataToSend[0] = (byte)DeviceCommands.SPITransaction;
     dataToSend[1] = (byte)dataToWrite.Length;
     dataToWrite.CopyTo(dataToSend, 2);
     if (ChipSelect != null)
     {
         if (ChipSelectPolarity == PinPolarity.ActiveLow)
         {
             ChipSelect.DigitalValue = false;
         }
         else
         {
             ChipSelect.DigitalValue = true;
         }
     }
     device.sendCommsConfigPacket(dataToSend);
     //Thread.Sleep(1);
     byte[] receivedData = device.receiveCommsResponsePacket();
     if (ChipSelect != null)
     {
         ChipSelect.ToggleOutput();
     }
     return(receivedData);
 }
Exemple #2
0
        /// <summary>
        /// Sends and Receives data. This is a blocking call that won't return until I2C communication is complete.
        /// </summary>
        /// <param name="address">The address of the device. This address should not include the read/write bit.</param>
        /// <param name="dataToWrite">Array of one or more bytes to write to the device.</param>
        /// <param name="numBytesToRead">Number of bytes to receive from the device.</param>
        /// <returns>Data read from the device.</returns>
        public byte[] SendReceive(byte address, byte[] dataToWrite, byte numBytesToRead)
        {
            byte[] returnedData = new byte[numBytesToRead];
            byte[] dataToSend   = new byte[4 + dataToWrite.Length];
            dataToSend[0] = (byte)DeviceCommands.I2CTransaction;
            dataToSend[1] = address;
            dataToSend[2] = (byte)dataToWrite.Length;
            dataToSend[3] = numBytesToRead;
            dataToWrite.CopyTo(dataToSend, 4);
            lock (lockObject)
            {
                device.sendCommsConfigPacket(dataToSend);
                Thread.Sleep(1);
                byte[] response = device.receiveCommsResponsePacket();
                if (numBytesToRead > 0)
                {
                    Array.Copy(response, 1, returnedData, 0, numBytesToRead);
                }
            }

            return(returnedData);
        }