Exemplo n.º 1
0
        public static void Scan()
        {
            Communication.USBHost usbHost = new Communication.USBHost("/dev/ttyUSB0");

            usbHost.Open();

            if (usbHost.IsOpen)
            {
                FileStream fileStream = usbHost.GetStream();

                try
                {
                    int length;

                    while (true)
                    {
                        Byte[] bytes = new Byte[256];

                        if ((length = fileStream.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            scannedBarcode = Encoding.ASCII.GetString(bytes, 0, length).Trim();

                            barcodeIsSet = true;

                            CountSetter.SetCountScreen();
                            break;
                        }
                        else
                        {
                            Thread.Sleep(1000);
                        }
                    }
                }
                catch (Exception ex)
                {
                }

                usbHost.Close();
            }

            usbHost.Dispose();
        }
Exemplo n.º 2
0
        public static int Test(string[] args)
        {
            Log.d("CommunicationUSBHost");

            // Get list of USB Host connected devices
            string[] usbPortNames = Communication.USBHost.GetPortNames();
            foreach (string usbPortName in usbPortNames)
            {
                Log.d("Found: " + usbPortName);
            }

            if (usbPortNames.Length == 0)
            {
                Log.d("No usb device!");
                return(-2);
            }
            // Open the first USB host device and read any data sent
            Communication.USBHost usbHost =
                new Communication.USBHost("/dev/ttyUSB0");

            usbHost.Open();

            if (usbHost.IsOpen)
            {
                Log.d(String.Format("Opened /dev/ttyUSB0 ({0})",
                                    usbHost.HIDName));

                FileStream fileStream = usbHost.GetStream();

                try
                {
                    int timeout = 0;
                    int i;

                    while (timeout < 10)
                    {
                        Log.d(String.Format("Exiting in {0} secs", 10 - timeout));
                        Byte[] bytes = new Byte[256];

                        if ((i = fileStream.Read(bytes, 0, bytes.Length)) > 0)
                        {
                            string data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                            Log.d(String.Format("read {0} bytes: {1}", data.Length, data));
                            timeout = 0;
                        }
                        else
                        {
                            Thread.Sleep(1000);
                            timeout++;
                        }
                    }
                }
                catch (Exception ex)
                {
                    Log.d(String.Format("Exception reading /dev/ttyUSB0: {0}", ex.Message));
                }

                usbHost.Close();
            }

            // Wait a short while
            Thread.Sleep(1000);

            // Clean up
            usbHost.Dispose();

            return(0);
        }