private void InitializeFiscalPrinter() { var device = Program.UserDevices.FirstOrDefault(x => x.DeviceType == DeviceType.FiscalPrinter); if (device != null && !device.IsConnected) { return; } try { if (_fiscalPrinter == null) { if (device == null) { device = new Device(); device.DeviceType = DeviceType.FiscalPrinter; device.IsConnected = true; Program.UserDevices.Add(device); } HardwareConfigurationSection config; HardwareConfigurationElementCollection hardwareIdsConfig; List <KeyValuePair <string, string> > hardware; config = HardwareConfigurationSection.GetConfiguration(); hardwareIdsConfig = config.HardwareIds; hardware = new List <KeyValuePair <string, string> >(); foreach (HardwareConfigurationElement hardwareId in hardwareIdsConfig) { hardware.Add(new KeyValuePair <string, string>(hardwareId.Name, hardwareId.Id)); } string VID = hardware.FirstOrDefault(x => x.Key == "SerialDevice").Value.Split('&')[0].Replace("VID_", ""); string PID = hardware.FirstOrDefault(x => x.Key == "SerialDevice").Value.Split('&')[1].Replace("PID_", ""); var ports = Common.Helpers.DeviceHelper.GetPortByVPid(VID, PID).Distinct(); //("067B", "2303") var portName = SerialPort.GetPortNames().Intersect(ports).FirstOrDefault(x => !Program.UserDevices.Any(y => y.PortName == x)); this.CheckPort(portName); _fiscalPrinter = new SY50(portName); device.PortName = portName; //MessageBox.Show("Успешно поврзување со касата, на port :: " + portName); } } catch (Exception ex) { device.IsConnected = false; MessageBox.Show(this, "Неуспешно поврзување со Фискалната каса, проверете дали е приклучена!\n\nOpening serial port result :: " + ex.Message, "Информација!"); } }
private void InitializeFiscalPrinterDeviceHandles() { var device = Program.UserDevices.FirstOrDefault(x => x.DeviceType == DeviceType.FiscalPrinter); if (device != null && !device.IsConnected) { return; } try { if (_serialPort == null) { if (device == null) { device = new Device(); device.DeviceType = DeviceType.FiscalPrinter; device.IsConnected = true; Program.UserDevices.Add(device); } HardwareConfigurationSection config; HardwareConfigurationElementCollection hardwareIdsConfig; //List<string> hardwareIds; List <KeyValuePair <string, string> > hardware; config = HardwareConfigurationSection.GetConfiguration(); hardwareIdsConfig = config.HardwareIds; //hardwareIds = new List<string>(); hardware = new List <KeyValuePair <string, string> >(); foreach (HardwareConfigurationElement hardwareId in hardwareIdsConfig) { //hardwareIds.Add(hardwareId.Id); hardware.Add(new KeyValuePair <string, string>(hardwareId.Name, hardwareId.Id)); } string VID = hardware.FirstOrDefault(x => x.Key == "BarcodeScanner").Value.Split('&')[0].Replace("VID_", ""); string PID = hardware.FirstOrDefault(x => x.Key == "BarcodeScanner").Value.Split('&')[1].Replace("PID_", ""); var ports = Common.Helpers.DeviceHelper.GetPortByVPid(VID, PID).Distinct(); //("067B", "2303") var com_port = SerialPort.GetPortNames().Intersect(ports).FirstOrDefault(); _serialPort = new SerialPort(com_port); //give your barcode serial port _serialPort.BaudRate = 9600; _serialPort.Parity = Parity.None; _serialPort.StopBits = StopBits.One; _serialPort.DataBits = 8; _serialPort.Handshake = Handshake.None; _serialPort.ReadTimeout = 500; _serialPort.WriteTimeout = 500; device.PortName = com_port; device.SerialPort = _serialPort; } //_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); AddDataReceivedHandler(); // Makes sure serial port is open before trying to write if (!_serialPort.IsOpen) { _serialPort.Open(); //MessageBox.Show("Успешно вклучен баркод читач, на port :: " + _serialPort.PortName); } //_serialPort.Write("SI\r\n"); } catch (Exception ex) { device.IsConnected = false; SplashScreen.SplashScreen.CloseForm(); MessageBox.Show(this, "Баркод читачот не е успешно активиран или не е приклучен!\n\nOpening serial port result :: " + ex.Message, "Информација!"); } }
/// <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() { var device = Program.UserDevices.FirstOrDefault(x => x.DeviceType == DeviceType.BarcodeScanner); if (device != null && !device.IsConnected) { return; } try { if (_serialPort == null) { if (device == null) { device = new Device(); device.DeviceType = DeviceType.BarcodeScanner; device.IsConnected = true; Program.UserDevices.Add(device); } HardwareConfigurationSection config; HardwareConfigurationElementCollection hardwareIdsConfig; //List<string> hardwareIds; List <KeyValuePair <string, string> > hardware; config = HardwareConfigurationSection.GetConfiguration(); hardwareIdsConfig = config.HardwareIds; //hardwareIds = new List<string>(); hardware = new List <KeyValuePair <string, string> >(); foreach (HardwareConfigurationElement hardwareId in hardwareIdsConfig) { //hardwareIds.Add(hardwareId.Id); hardware.Add(new KeyValuePair <string, string>(hardwareId.Name, hardwareId.Id)); } string VID = hardware.FirstOrDefault(x => x.Key == "SerialDevice").Value.Split('&')[0].Replace("VID_", ""); string PID = hardware.FirstOrDefault(x => x.Key == "SerialDevice").Value.Split('&')[1].Replace("PID_", ""); var ports = Common.Helpers.DeviceHelper.GetPortByVPid(VID, PID).Distinct(); //("067B", "2303") /// Prviot port koj ne e veke dodelen var com_port = SerialPort.GetPortNames().Intersect(ports).FirstOrDefault(x => !Program.UserDevices.Any(y => y.PortName == x)); _serialPort = new SerialPort(com_port); //give your barcode serial port _serialPort.BaudRate = 9600; _serialPort.Parity = Parity.None; _serialPort.StopBits = StopBits.One; _serialPort.DataBits = 8; _serialPort.Handshake = Handshake.None; _serialPort.ReadTimeout = 500; _serialPort.WriteTimeout = 500; device.PortName = com_port; device.SerialPort = _serialPort; } //_serialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); this.AddDataReceivedHandler(); // Makes sure serial port is open before trying to write if (!_serialPort.IsOpen) { _serialPort.Open(); //MessageBox.Show("Успешно вклучен баркод читач, на port :: " + _serialPort.PortName); } //_serialPort.Write("SI\r\n"); } catch (Exception ex) { device.IsConnected = false; SplashScreen.SplashScreen.CloseForm(); MessageBox.Show(this, "Баркод читачот не е успешно активиран или не е приклучен!\n\nOpening serial port result :: " + ex.Message, "Информација!"); } /* * 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); * } */ #region Zakomentiran /* * // 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, * 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, * NativeMethods.RawInputDeviceInfoCommand.RIDI_DEVICENAME, * data, * ref pcbSize) >= 0) * { * deviceName = (string)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.dwType), * rid.hDevice, * friendlyName); * * this.devices.Add(rid.hDevice, info); * } * } * * Marshal.FreeHGlobal(data); * } * } * } * } * * Marshal.FreeHGlobal(rawInputDeviceList); * } */ #endregion }