Beispiel #1
0
        //---#+************************************************************************
        //---NOTATION:
        //-  bool writeData(char[] cDataToWrite)
        //-
        //--- DESCRIPTION:
        //--  writes data to the device and returns true if no error occured
        //                                                             Autor:      F.L.
        //-*************************************************************************+#*
        /// <summary>
        /// Writes the data.
        /// </summary>
        /// <param name="bDataToWrite">The b data to write.</param>
        /// <returns></returns>
        public bool writeData(byte[] bDataToWrite)
        {
            bool success = false;

            if (getConnectionState())
            {
                try
                {
                    //get output report length
                    int myPtrToPreparsedData = -1;

                    myUSB.CT_HidD_GetPreparsedData(myUSB.HidHandle, ref myPtrToPreparsedData);
                    int code = myUSB.CT_HidP_GetCaps(myPtrToPreparsedData);

                    // int outputReportByteLength = 65;

                    int bytesSend = 0;

                    //if bWriteData is bigger then one report diveide into sevral reports
                    while (bytesSend < bDataToWrite.Length)
                    {
                        // Set the size of the Output report buffer.
                        byte[] OutputReportBuffer = new byte[myUSB.myHIDP_CAPS.OutputReportByteLength - 1 + 1];
                        // byte[] OutputReportBuffer = new byte[outputReportByteLength - 1 + 1];

                        // Store the report ID in the first byte of the buffer:
                        OutputReportBuffer[0] = 0;

                        // Store the report data following the report ID.
                        for (int i = 1; i < OutputReportBuffer.Length; i++)
                        {
                            if (bytesSend < bDataToWrite.Length)
                            {
                                OutputReportBuffer[i] = bDataToWrite[bytesSend];
                                bytesSend++;
                            }
                            else
                            {
                                OutputReportBuffer[i] = 0;
                            }
                        }

                        OutputReport myOutputReport = new OutputReport();
                        success = myOutputReport.Write(OutputReportBuffer, myUSB.HidHandle);
                    }
                }
                catch (AccessViolationException)
                {
                    success = false;
                }
            }
            else
            {
                success = false;
            }
            return(success);
        }
Beispiel #2
0
        //---#+************************************************************************
        //---NOTATION:
        //-  readDataThread()
        //-
        //--- DESCRIPTION:
        //--  ThreadMethod for reading Data
        //                                                             Autor:      F.L.
        //-*************************************************************************+#*
        /// <summary>
        ///  ThreadMethod for reading Data
        /// </summary>
        public void readDataThread()
        {
            int receivedNull = 0;

            while (true)
            {
                int myPtrToPreparsedData = -1;
                if (myUSB.CT_HidD_GetPreparsedData(myUSB.HidHandle, ref myPtrToPreparsedData) != 0)
                {
                    int code         = myUSB.CT_HidP_GetCaps(myPtrToPreparsedData);
                    int reportLength = myUSB.myHIDP_CAPS.InputReportByteLength;

                    while (true)
                    {//read until thread is stopped
                        byte[] myRead = myUSB.CT_ReadFile(myUSB.myHIDP_CAPS.InputReportByteLength);
                        if (myRead != null)
                        {
                            //ByteCount + bytes received
                            byteCount += myRead.Length;

                            //Store received bytes

                            /*  lock (recieveBuffer.SyncRoot)
                             * {
                             *    recieveBuffer.Add(myRead);
                             * }*/
                            lock (USBHIDDRIVER.USBInterface.usbBuffer.SyncRoot)
                            {
                                USBHIDDRIVER.USBInterface.usbBuffer.Add(myRead);
                            }
                        }
                        else
                        {
                            //Recieved a lot of null bytes!
                            //mybe device disconnected?
                            if (receivedNull > 100)
                            {
                                receivedNull = 0;
                                Thread.Sleep(1);
                            }
                            receivedNull++;
                        }
                    }
                }
            }
        }
Beispiel #3
0
        bool SearchDevice()
        {
            this.DevicePath = string.Empty;

            myUSB.CT_HidGuid();             // Get the GUID of the HID device Class

            myUSB.CT_SetupDiGetClassDevs(); // Get the device information set


            bool?result       = null;
            bool resultb      = false;
            int  device_count = 0;
            int  size         = 0;
            int  requiredSize = 0;

            // Reset the IsConnected to false
            this.IsConnected = false;

            //search the device until you have found it or no more devices in list
            while (!result.HasValue || result.Value == true)
            {
                //
                //if (result == false)
                //    break;

                //open the device
                result = myUSB.CT_SetupDiEnumDeviceInterfaces(device_count);

                //get size of device path
                resultb = myUSB.CT_SetupDiGetDeviceInterfaceBuffer(ref requiredSize, 0);

                size = requiredSize;

                //get device path
                resultb = myUSB.CT_SetupDiGetDeviceInterfaceDetail(ref requiredSize, size);

                if (resultb == false)
                {
                    int err = Marshal.GetLastWin32Error();
                }

                this.DevicePath = myUSB.DevicePathName;

                //is this the device i want?
                string deviceID = this.VID + "&" + this.PID;

                if (this.DevicePath.ToLower().IndexOf(deviceID.ToLower()) > 0)
                {
                    //create HID Device Handel
                    resultb = myUSB.CT_CreateFile(this.DevicePath);

                    // Check the serial Number
                    myUSB.CT_HidD_GetHIDSerialNumber(out string device_sn);

                    if (this.SerialNumber == null || this.SerialNumber == string.Empty || device_sn == this.SerialNumber)
                    {
                        IntPtr myPtrToPreparsedData = default(IntPtr);

                        if (myUSB.CT_HidD_GetPreparsedData(myUSB.hHidFile, ref myPtrToPreparsedData))
                        {
                            bool code         = myUSB.CT_HidP_GetCaps(myPtrToPreparsedData);
                            int  reportLength = myUSB.myHIDP_CAPS.InputReportByteLength;

                            //we have found our device so stop searching
                            this.IsConnected  = true;
                            this.SerialNumber = device_sn;
                        }

                        break;
                    }
                    else
                    {
                        myUSB.CT_CloseFile();
                    }
                }

                device_count++;
            }

            myUSB.CT_SetupDiDestroyDeviceInfoList();

            //return state
            return(this.IsConnected);
        }