Exemple #1
0
        /// <summary>
        /// Setups up the ports
        /// SetupPort( Port#, Initvalue, Input/Output, Pullups, Interrupts
        /// </summary>
        public UInt32 SetupPort(UInt32 PortNum, UInt32 InitValue, UInt32 Direction, UInt32 Pullups, UInt32 Interrutps)
        {
            try
            {
                myTxHidPacket.MajorCmd = 101;
                myTxHidPacket.MinorCmd = 130;
                myTxHidPacket.LSBData  = Convert.ToByte(PortNum);
                myTxHidPacket.HidData0 = Convert.ToByte(InitValue);
                myTxHidPacket.HidData1 = Convert.ToByte(Direction);
                myTxHidPacket.HidData2 = Convert.ToByte(Pullups);
                myTxHidPacket.HidData3 = Convert.ToByte(Interrutps);
                if (Hid.HidD_SetFeature(myDeviceHandle, StructureToByteArray(myTxHidPacket), 8) == false)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }

            catch (Exception ex)
            {
                DisplayException(MODULE_NAME, ex);
                //throw;
                return(2);
            }
        }
Exemple #2
0
        /// <summary>
        /// Writes the ports values
        /// returns zero on sucess, else non-zero erro
        /// </summary>
        public UInt32 WritePorts(UInt32 Port0, UInt32 Port1)
        {
            try
            {
                myTxHidPacket.MajorCmd = 101;
                myTxHidPacket.MinorCmd = 10;
                myTxHidPacket.LSBData  = Convert.ToByte(Port0);
                myTxHidPacket.MSBData  = Convert.ToByte(Port1);
                if (Hid.HidD_SetFeature(myDeviceHandle, StructureToByteArray(myTxHidPacket), 8) == false)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }

            catch (Exception ex)
            {
                DisplayException(MODULE_NAME, ex);
                //throw;
                return(2);
            }
        }
Exemple #3
0
        /// <summary>
        /// Writes the ports values
        /// returns zero on sucess, else non-zero erro
        /// </summary>
        public UInt32 SendCommand(HidTxPacketStruct TxCmd)
        {
            try
            {
                int Length;
                if (TxCmd.MajorCmd == 102)
                {
                    Length = 16;
                }
                else
                {
                    Length = 8;
                }
                if (Hid.HidD_SetFeature(myDeviceHandle, StructureToByteArray(TxCmd), Length) == false)
                {
                    return(1);
                }
                else
                {
                    return(0);
                }
            }

            catch (Exception ex)
            {
                DisplayException(MODULE_NAME, ex);
                //throw;
                return(2);
            }
        }
Exemple #4
0
        /// <summary>
        /// Reads the serial Number
        /// returns zero on sucess, else non-zero erro
        /// </summary>
        public UInt32 GetDeviceInfo(ref UInt32 FamilyCode, ref UInt32 SerialNumber, ref UInt32 Version, ref UInt32 Date, ref UInt32 Month, ref UInt32 Year, ref UInt32 SecurityNumber, ref UInt32 ChipType)
        {
            try
            {
                Byte[] Ans = new Byte[16];
                //Ans[0] = 10;
                //if (Hid.HidD_GetFeature(myDeviceHandle, Ans, 8) == false) return (1);
                //SerialNumber = Convert.ToUInt32(Ans[0]);
                //SerialNumber += Convert.ToUInt32(Ans[1] <<8);
                //SerialNumber += Convert.ToUInt32(Ans[2] << 16);
                //SerialNumber += Convert.ToUInt32(Ans[3] << 24);
                //Version = Convert.ToUInt32(Convert.ToByte(Ans[4]));
                //Date = Convert.ToUInt32(Convert.ToByte(Ans[5]));
                //Month = Convert.ToUInt32(Convert.ToByte(Ans[6]));
                //Year = Convert.ToUInt32(Convert.ToByte(Ans[7]));

                Ans[0] = 104;
                if (Hid.HidD_GetFeature(myDeviceHandle, Ans, 16) == false)
                {
                    return(1);
                }
                FamilyCode      = Convert.ToUInt32(Ans[0]);
                FamilyCode     += Convert.ToUInt32(Ans[1] << 8);
                SecurityNumber  = Convert.ToUInt32(Ans[2]);
                SecurityNumber += Convert.ToUInt32(Ans[3] << 8);

                Version       = Convert.ToUInt32(Convert.ToByte(Ans[4]));
                Date          = Convert.ToUInt32(Convert.ToByte(Ans[5]));
                Month         = Convert.ToUInt32(Convert.ToByte(Ans[6]));
                Year          = Convert.ToUInt32(Convert.ToByte(Ans[7]));
                SerialNumber  = Convert.ToUInt32(Ans[8]);
                SerialNumber += Convert.ToUInt32(Ans[9] << 8);
                SerialNumber += Convert.ToUInt32(Ans[10] << 16);
                SerialNumber += Convert.ToUInt32(Ans[11] << 24);

                ChipType = Convert.ToUInt32(Ans[12]);

                return(0);
            }

            catch (Exception ex)
            {
                DisplayException(MODULE_NAME, ex);
                //throw;
                return(2);
            }
        }
Exemple #5
0
        /// <summary>
        /// Reads the currenly value at port zero
        /// returns zero on sucess, else non-zero erro
        /// </summary>
        public UInt32 ReadPort0(ref UInt32 Port0)
        {
            try
            {
                Byte[] Ans = new Byte[16];
                Ans[0] = 100;
                if (Hid.HidD_GetFeature(myDeviceHandle, Ans, 8) == false)
                {
                    return(1);
                }
                Port0 = Convert.ToUInt32(Ans[0]);
                return(0);
            }

            catch (Exception ex)
            {
                DisplayException(MODULE_NAME, ex);
                //throw;
                return(2);
            }
        }
Exemple #6
0
        ///  <summary>
        ///  Uses a series of API calls to locate a HID-class device
        ///  by its Vendor ID, Product ID and by the Nth number on the list.
        ///  NthDevice: 0=none, used to determine how many matching device are currently
        ///  installed on the system. 1=Find the first matching device, 2=the second matching device,
        ///  and so on...
        ///  </summary>
        ///
        ///  <returns>
        ///   True if the device is detected, False if not detected.
        ///  </returns>
        private Boolean FindTheHid(UInt32 NthDevice)
        {
            Boolean deviceFound = false;

            String[]       devicePathName = new String[512]; // Apr 20, 2009 - Increase size from 128 to 512.
            SafeFileHandle hidHandle;
            Guid           hidGuid = Guid.Empty;

            Int32   memberIndex     = 0;
            UInt16  myProductID     = 0xB080;
            UInt16  myVendorID      = 0x0FC5;
            Boolean success         = false;
            UInt32  MatchingDevices = 0;

            try
            {
                myDeviceDetected = false;
                Hid.HidD_GetHidGuid(ref hidGuid);       //Retrieves the interface class GUID for the HID class.

                //  Fill an array with the device path names of all attached HIDs.
                deviceFound = MyDeviceManagement.FindDeviceFromGuid(hidGuid, ref devicePathName);

                //  If there is at least one HID, attempt to read the Vendor ID and Product ID
                //  of each device until there is a match or all devices have been examined.
                if (deviceFound)
                {
                    memberIndex = 0;
                    do
                    {
                        //  Open the device
                        hidHandle = FileIO.CreateFile(devicePathName[memberIndex], 0, FileIO.FILE_SHARE_READ | FileIO.FILE_SHARE_WRITE, IntPtr.Zero, FileIO.OPEN_EXISTING, 0, 0);

                        if (!hidHandle.IsInvalid)
                        {   //  Device openned, now find out if it's the device we want.
                            MyHid.DeviceAttributes.Size = Marshal.SizeOf(MyHid.DeviceAttributes);
                            //  Retrieves a HIDD_ATTRIBUTES structure containing the Vendor ID,
                            //  Product ID, and Product Version Number for a device.
                            success = Hid.HidD_GetAttributes(hidHandle, ref MyHid.DeviceAttributes);
                            if (success)
                            {
                                //  Find out if the device matches the one we're looking for.
                                if ((MyHid.DeviceAttributes.VendorID == myVendorID) & (MyHid.DeviceAttributes.ProductID == myProductID))
                                {
                                    MatchingDevices++;
                                    myDeviceDetected = true;
                                }

                                if (myDeviceDetected && (MatchingDevices == NthDevice))
                                {
                                    // Device found
                                    //  Save the DevicePathName
                                    myDevicePathName = devicePathName[memberIndex];
                                    hidHandle.Close();
                                }
                                else
                                {
                                    //  It's not a match, so close the handle. try the next one
                                    myDeviceDetected = false;
                                    hidHandle.Close();
                                }
                            }
                            else
                            {
                                //  There was a problem in retrieving the information.
                                myDeviceDetected = false;
                                hidHandle.Close();
                            }
                        }

                        //  Keep looking until we find the device or there are no devices left to examine.
                        memberIndex = memberIndex + 1;
                    }while (!((myDeviceDetected | (memberIndex == devicePathName.Length))));
                }

                MatchingDevicesFound = MatchingDevices; // save the device found count

                return(myDeviceDetected);
            }
            catch (Exception ex)
            {
                DisplayException(MODULE_NAME, ex);
                throw;
            }
        }