Example #1
0
        /// <summary>
        /// Open the connection
        /// </summary>
        public override void Open()
        {
            bool hasError = false;

            try
            {
                device = HidApiNative.hid_open(this.vendorId, this.productId, IntPtr.Zero);
                if (device != IntPtr.Zero)
                {
                    HidApiNative.hid_set_nonblocking(device, 0);
                }
                else
                {
                    hasError = true;
                }
            }
            catch (Exception e) {
                throw new ConnectionException(ConnectionError.OpenError, e);
            }
            if (hasError)
            {
                throw new ConnectionException(ConnectionError.OpenError);
            }
            isConnected = true;
            ConnectionWasOpened();
        }
Example #2
0
        /// <summary>
        /// Receive a reply
        /// </summary>
        public override TBrickReply Receive()
        {
            byte[] reply          = null;
            IntPtr pnt            = IntPtr.Zero;
            int    bytesRead      = 0;
            int    expectedlength = 0;
            bool   hasError       = false;

            try{
                byte[] temp = new byte[1024];
                pnt       = Marshal.AllocHGlobal(Marshal.SizeOf(temp[0]) * 1024);
                bytesRead = HidApiNative.hid_read_timeout(device, pnt, 1024, Timeout);
                if (bytesRead > 2)
                {
                    Marshal.Copy(pnt, temp, 0, 1024);
                    expectedlength = (ushort)(0x0000 | temp[0] | (temp[1] << 2));
                    Marshal.FreeHGlobal(pnt);
                    pnt   = IntPtr.Zero;
                    reply = new byte[expectedlength];
                    Array.Copy(temp, 2, reply, 0, expectedlength);
                }
                else
                {
                    if (pnt != IntPtr.Zero)
                    {
                        Marshal.FreeHGlobal(pnt);
                        pnt = IntPtr.Zero;
                    }
                    hasError = true;
                }
            }
            catch (Exception e) {
                throw new ConnectionException(ConnectionError.ReadError, e);
            }
            finally{
                if (pnt != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pnt);
                }
            }
            if (hasError)
            {
                if (typeof(TBrickCommand) == typeof(NXT.Command))
                {
                    throw new NXT.BrickException(NXT.BrickError.WrongNumberOfBytes);
                }
                else
                {
                    throw new EV3.BrickException(EV3.BrickError.WrongNumberOfBytes);
                }
            }
            var brickReply = new TBrickReply();

            brickReply.SetData(reply);
            ReplyWasReceived(brickReply);
            return(brickReply);
        }
Example #3
0
 /// <summary>
 /// Close the connection
 /// </summary>
 public override void Close()
 {
     try{
         if (device != IntPtr.Zero)
         {
             HidApiNative.hid_close(device);
         }
     }
     catch {}
     isConnected = false;
     ConnectionWasClosed();
 }
Example #4
0
        /// <summary>
        /// Send the specified command.
        /// </summary>
        /// <param name='command'>
        /// Command to send
        /// </param>
        public override void Send(TBrickCommand command)
        {
            Exception innerException = null;
            int       bytesWritten;

            byte[] data   = null;
            ushort length = (ushort)command.Length;

            if (addReportId)
            {
                data    = new byte[length + 3];
                data[0] = 0x00;
                data[1] = (byte)(length & 0x00ff);
                data[2] = (byte)((length & 0xff00) >> 2);
                Array.Copy(command.Data, 0, data, 3, command.Length);
            }
            else
            {
                data    = new byte[length + 2];
                data[0] = (byte)(length & 0x00ff);
                data[1] = (byte)((length & 0xff00) >> 2);
                Array.Copy(command.Data, 0, data, 2, command.Length);
            }
            CommandWasSend(command);
            bool   hasError = false;
            IntPtr pnt      = IntPtr.Zero;

            try{
                int size = Marshal.SizeOf(data[0]) * data.Length;
                pnt = Marshal.AllocHGlobal(size);
                // Copy the array to unmanaged memory.
                Marshal.Copy(data, 0, pnt, data.Length);
                bytesWritten = HidApiNative.hid_write(device, pnt, data.Length);
                if (!addReportId)
                {
                    if (bytesWritten != data.Length)
                    {
                        hasError = true;
                    }
                }
                else
                {
                    if (bytesWritten == -1)
                    {
                        hasError = true;
                    }
                }
            }
            catch (Exception e) {
                innerException = e;
                hasError       = true;
            }
            finally
            {
                if (pnt != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pnt);
                }
            }
            if (hasError)
            {
                if (innerException != null)
                {
                    throw new ConnectionException(ConnectionError.WriteError, innerException);
                }
                throw new ConnectionException(ConnectionError.WriteError);
            }
        }