Click here to Skip to main content
15,891,204 members
Home / Discussions / C#
   

C#

 
GeneralRe: ASP or WPF? Pin
jeramyRR4-Jun-13 16:08
jeramyRR4-Jun-13 16:08 
AnswerRe: ASP or WPF? Pin
V.5-Jun-13 2:32
professionalV.5-Jun-13 2:32 
GeneralRe: ASP or WPF? Pin
jeramyRR5-Jun-13 5:39
jeramyRR5-Jun-13 5:39 
AnswerRe: ASP or WPF? Pin
Jasmine25015-Jun-13 12:07
Jasmine25015-Jun-13 12:07 
AnswerRe: ASP or WPF? Pin
_Maxxx_5-Jun-13 20:05
professional_Maxxx_5-Jun-13 20:05 
QuestionUSB Device search Pin
Blubbo4-Jun-13 7:19
Blubbo4-Jun-13 7:19 
AnswerRe: USB Device search Pin
Dave Kreskowiak4-Jun-13 10:59
mveDave Kreskowiak4-Jun-13 10:59 
GeneralRe: USB Device search Pin
Blubbo5-Jun-13 2:11
Blubbo5-Jun-13 2:11 
What I was trying to do is to have my software to connect up to 4 USB Communication Box. This box would communicate with RFID tag. In some cases, the USB box would somehow loses connection and the software would detect the loss of communication to the box. Also that when the USB box is reconnected, the software would re-connect and re-communicate again. This software would ensure that the "address" would be unique from other 3 USB devices.

here's my sample code
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using HID_API_Library;
using System.Security.Permissions;

namespace USB_Device_Connect_Disconnect_Test
{
    public partial class Form1 : Form
    {
        /// <summary>
        /// Windows Messages
        /// Defined in winuser.h from Windows SDK v6.1
        /// Documentation pulled from MSDN.
        /// For more look at: http://www.pinvoke.net/default.aspx/Enums/WindowsMessages.html
        /// </summary>
        public enum WM : uint
        {
            /// <summary>
            /// Notifies an application of a change to the hardware configuration of a device or the computer.
            /// </summary>
            WM_DEVICECHANGE = 0x0219,
            WM_PARENTNOTIFY = 0x0210,
            DBT_DEVTYP_DEVICEINTERFACE = 5,
            DBT_DEVTYP_HANDLE = 6,
            DBT_DEVICEARRIVAL = 0x8000,         // system detected a new device
            DBT_DEVICEQUERYREMOVE = 0x8001,     // Preparing to remove (any program can disable the removal)
            DBT_DEVICEREMOVECOMPLETE = 0x8004,  // removed 
            DBT_DEVTYP_VOLUME = 0x00000002,     // drive type is logical volume
            DBT_DEVNODES_CHANGED = 7,           // device changed
            BROADCAST_QUERY_DENY = 0x424D5144,

        }
        
        // initilized bytes had to be removed for public post.
        byte[] Read_Input = new byte[9];
        byte[] DeviceFirmwareVersion = new byte[9];
        byte[] inputReport = new byte[9];

        HID_API[] hid = new HID_API[4];

        GroupBox[] gbDevice = new GroupBox[4];
        List<string> usbDevices;

        public Form1()
        {
            InitializeComponent();
            MonitorUSBDevices();
        }

        int ReadPort(int deviceID, ref byte data)
        {
            int status = hid[deviceID].ExchangeInputAndOutputReports(ref Read_Input, ref inputReport, deviceID);

            if (status == 1)
            {
                data = inputReport[1];
            }
            else
                status = -530;

            return status;
        }

        int GetDeviceFirmwareVersion(int deviceID, ref byte versionDeviceFirmware)
        {
            byte[] result = new byte[9];

            int status = hid[deviceID].ExchangeInputAndOutputReports(ref DeviceFirmwareVersion, ref result, deviceID);

            if (status == 1)
                versionDeviceFirmware = result[0];
            else
            {
                versionDeviceFirmware = 0x00;
                status = -510;
            }

            return status;
        }

        // This method acquires the Box ID from the pre-setted slider switch
        int GetBoxID(int deviceID, ref byte boxId)
        {
            int status = ReadPort(deviceID, ref boxId);

            if (status == 1)
            {
                boxId = (byte)~boxId;
                boxId &= 0x0F;

                if ((boxId & 0x08) == 0x08)
                {
                    boxId &= 0x06;
                    boxId >>= 1;
                    boxId += 1;
                }
                else
                {
                    boxId = 0;
                    status = -520;
                }
            }
            else
                status = -521;

            return status;
        }

        void CreateGroupBoxes()
        {
            int yLoc = 12;

            for (int i = 0; i < 4; i++)
            {
                gbDevice[i] = gb_Device0;
                gbDevice[i].Location = new Point(218, yLoc);
                yLoc += 106;
            }

            this.Controls.AddRange(gbDevice);
        }

        List<string> SeekHIDDevices()
        {
            HID_API hid_api = new HID_API();
            return hid_api.EnumerateUSBDevices(0x0003, 0x0483);
        }

        private void MonitorUSBDevices()
        {
            usbDevices = SeekHIDDevices();

            byte boxID = 0x00;

            for (int i = 0; i < usbDevices.Count; i++)
            {
                hid[i] = new HID_API();
                hid[i].OpenUSBDrive(usbDevices[i]);
                hid[i].RegisterHidNotification(this.Handle);
            }

            if (usbDevices.Count >= 1)
            {
                GetBoxID(0, ref boxID);
                lbl_BoxID0.Text = boxID.ToString("X2");

                if (usbDevices.Count >= 2)
                {
                    GetBoxID(1, ref boxID);
                    lbl_BoxID1.Text = boxID.ToString("X2");

                    if (usbDevices.Count >= 3)
                    {
                        GetBoxID(2, ref boxID);
                        lbl_BoxID2.Text = boxID.ToString("X2");

                        if (usbDevices.Count == 4)
                        {
                            GetBoxID(3, ref boxID);
                            lbl_BoxID3.Text = boxID.ToString("X2");
                        }
                    }
                }
            }
        }

        [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
        protected override void WndProc(ref Message m)
        {
            int devType = 0;

            switch ((WM)m.Msg)
            {
                case WM.WM_DEVICECHANGE:
                    {
                        //MessageBox.Show("USB device is unplugged/plugged");

                        switch ((WM)m.WParam)
                        {
                            case WM.DBT_DEVICEARRIVAL:
                                {
                                    //MessageBox.Show(string.Format("USB device is plugged: {0}", devType));
                                    break;
                                }
                            case WM.DBT_DEVICEREMOVECOMPLETE:
                                {
                                    //MessageBox.Show(string.Format("USB device is unplugged: {0}", devType));
                                    break;
                                }
                            case WM.DBT_DEVNODES_CHANGED:
                                {
                                   // MessageBox.Show(string.Format("USB device is plugged: {0}", devType));
                                    HID_API hid_api = new HID_API();
                                    List<string> usbPath = hid_api.EnumerateUSBDevices(0x0003, 0x0483);

                                    //foreach (string path in usbPath)
                                    //{
                                    //    foreach (string existPath in usbDevices)
                                    //    {
                                    //    }
                                    //}
                                    break;
                                }
                        }
                        break;
                    }
            }

            base.WndProc(ref m);
        }
    }
}

QuestionXLOCK in Sqlserver Pin
GauravSatpute3-Jun-13 23:53
GauravSatpute3-Jun-13 23:53 
AnswerRe: XLOCK in Sqlserver Pin
Eddy Vluggen4-Jun-13 0:29
professionalEddy Vluggen4-Jun-13 0:29 
AnswerRe: XLOCK in Sqlserver Pin
Nicholas Marty4-Jun-13 3:09
professionalNicholas Marty4-Jun-13 3:09 
AnswerRe: XLOCK in Sqlserver Pin
jschell4-Jun-13 8:47
jschell4-Jun-13 8:47 
QuestionAny one help me about the Recursion Concept Diagramatically. Pin
sgunasekhar3-Jun-13 23:09
sgunasekhar3-Jun-13 23:09 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
OriginalGriff3-Jun-13 23:24
mveOriginalGriff3-Jun-13 23:24 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
Keith Barrow4-Jun-13 0:03
professionalKeith Barrow4-Jun-13 0:03 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
uspatel4-Jun-13 2:18
professionaluspatel4-Jun-13 2:18 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
Abhinav S4-Jun-13 3:22
Abhinav S4-Jun-13 3:22 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
Alan Balkany4-Jun-13 4:51
Alan Balkany4-Jun-13 4:51 
GeneralRe: Any one help me about the Recursion Concept Diagramatically. Pin
Keith Barrow4-Jun-13 6:14
professionalKeith Barrow4-Jun-13 6:14 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
Amarnath S4-Jun-13 20:24
professionalAmarnath S4-Jun-13 20:24 
AnswerRe: Any one help me about the Recursion Concept Diagramatically. Pin
_Maxxx_5-Jun-13 20:06
professional_Maxxx_5-Jun-13 20:06 
AnswerAny One Can Help me to Upload xml file into database Pin
Member 97011655-Jun-13 23:56
Member 97011655-Jun-13 23:56 
GeneralSearchDirectory Pin
HubSnippets3-Jun-13 0:46
HubSnippets3-Jun-13 0:46 
GeneralRe: SearchDirectory Pin
Richard MacCutchan3-Jun-13 1:24
mveRichard MacCutchan3-Jun-13 1:24 
GeneralRe: SearchDirectory Pin
Jasmine25013-Jun-13 12:30
Jasmine25013-Jun-13 12:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.