Beispiel #1
0
        /// <summary>
        /// Fires the barcode scanned event.
        /// </summary>
        /// <param name="deviceInfo">information about the device that generated
        /// the barcode</param>
        private void FireBarcodeScanned(BarcodeScannerDeviceInfo deviceInfo, string barcode)
        {
            //string barcode;
            EventHandler handler;

            //barcode = this.keystrokeBuffer.ToString();
            handler = this._barcodeScanned;

            //this.keystrokeBuffer = new StringBuilder();

            if (handler != null)
            {
                handler(this, new BarcodeScannedEventArgs(barcode, deviceInfo));
            }
        }
Beispiel #2
0
        /// <summary>
        /// Fires the barcode scanned event.
        /// </summary>
        /// <param name="deviceInfo">information about the device that generated
        /// the barcode</param>
        private void FireBarcodeScanned(BarcodeScannerDeviceInfo deviceInfo)
        {
            string       barcode;
            EventHandler handler;

            barcode = this.keystrokeBuffer.ToString();
            if (barcode != null && barcode.Length > 0)
            {
                handler = this.BarcodeScanned;
                this.keystrokeBuffer = new StringBuilder();
                if (handler != null)
                {
                    handler(this, new BarcodeScannedEventArgs(barcode, deviceInfo));
                }
            }
        }
Beispiel #3
0
 private void DataReceivedHandler(object sender, SerialDataReceivedEventArgs e)
 {
     try
     {
         //Thread.Sleep(100);
         SerialPort sp     = (SerialPort)sender;
         string     indata = sp.ReadLine();
         indata = RegExReplace(indata, String.Empty).PadLeft(13, '0');
         string deviceName = "MS9520";
         BarcodeScannerDeviceType deviceType = BarcodeScannerDeviceType.HumanInterfaceDevice;
         IntPtr deviceHandle = new IntPtr();
         string friendlyName = "Voyager Barcode Scanner";
         BarcodeScannerDeviceInfo deviceInfo = new BarcodeScannerDeviceInfo(deviceName, deviceType, deviceHandle, friendlyName);
         this.FireBarcodeScanned(deviceInfo, indata);
         return;
     }
     catch (Exception ex)
     {
         var i = 1;
         //throw;
     }
 }
        /// <summary>
        /// Enumerates devices provided by GetRawInputDeviceList. We'll only listen
        /// to these devices.
        /// </summary>
        /// <exception cref="ConfigurationErrorsException">if an error occurs
        /// during configuration</exception>
        private void InitializeBarcodeScannerDeviceHandles()
        {
            BarcodeScannerListenerConfigurationSection           config;
            BarcodeScannerListenerConfigurationElementCollection hardwareIdsConfig;
            List <string> hardwareIds;
            uint          numDevices;
            uint          size;

            config            = BarcodeScannerListenerConfigurationSection.GetConfiguration();
            hardwareIdsConfig = config.HardwareIds;
            hardwareIds       = new List <string>();
            numDevices        = 0;
            size = (uint)Marshal.SizeOf(typeof(NativeMethods.RAWINPUTDEVICELIST));

            foreach (BarcodeScannerListenerConfigurationElement hardwareId in hardwareIdsConfig)
            {
                hardwareIds.Add(hardwareId.Id);
            }

            // First, we get the number of raw input devices in the list by passing
            // in IntPtr.Zero. Then we allocate sufficient memory and retrieve the
            // entire list.
            if (NativeMethods.GetRawInputDeviceList(IntPtr.Zero, ref numDevices, size) == 0)
            {
                IntPtr rawInputDeviceList;

                rawInputDeviceList = Marshal.AllocHGlobal((int)(size * numDevices));
                if (NativeMethods.GetRawInputDeviceList(
                        rawInputDeviceList,
                        ref numDevices,
                        size) != uint.MaxValue)
                {
                    // Next, we iterate through the list, discarding undesired items
                    // and retrieving further information on the barcode scanner devices
                    for (int i = 0; i < numDevices; ++i)
                    {
                        uint pcbSize;
                        NativeMethods.RAWINPUTDEVICELIST rid;

                        pcbSize = 0;
                        rid     = (NativeMethods.RAWINPUTDEVICELIST)Marshal.PtrToStructure(
                            new IntPtr((rawInputDeviceList.ToInt32() + (size * i))),
                            typeof(NativeMethods.RAWINPUTDEVICELIST));

                        if (NativeMethods.GetRawInputDeviceInfo(
                                rid.hDevice,
                                (uint)NativeMethods.RawInputDeviceInfoCommand.RIDI_DEVICENAME,
                                IntPtr.Zero,
                                ref pcbSize) >= 0)
                        {
                            if (pcbSize > 0)
                            {
                                string deviceName;
                                string friendlyName;
                                BarcodeScannerDeviceInfo info;
                                IntPtr data;

                                data = Marshal.AllocHGlobal((int)pcbSize);
                                if (NativeMethods.GetRawInputDeviceInfo(
                                        rid.hDevice,
                                        (uint)NativeMethods.RawInputDeviceInfoCommand.RIDI_DEVICENAME,
                                        data,
                                        ref pcbSize) >= 0)
                                {
                                    deviceName = Marshal.PtrToStringAnsi(data);

                                    if ((from hardwareId in hardwareIds
                                         where deviceName.Contains(hardwareId)
                                         select hardwareId).Count() > 0)
                                    {
                                        friendlyName = GetDeviceFriendlyName(deviceName);

                                        info = new BarcodeScannerDeviceInfo(
                                            deviceName,
                                            GetBarcodeScannerDeviceType(rid.Type),
                                            rid.hDevice,
                                            friendlyName);

                                        devices.Add(rid.hDevice, info);
                                    }
                                }

                                Marshal.FreeHGlobal(data);
                            }
                        }
                    }
                }

                Marshal.FreeHGlobal(rawInputDeviceList);
            }
        }