-1

I have a C# Winform app that reads supplier barcodes into a textbox and automatically goes to another textbox by handling Keydown event and looking for "ENTER" key press. The "ENTER" key character is automatically appended by the barcode scanner upon reading all characters of the barcode. However, I am having problems with barcodes that embed CR/LF characters which causes the Keydown event to erroneously detect it as completion of scanning and triggers transfer to the next textbox. Is it possible to ignore the CR/LF characters until all barcodes characters are already read? Or maybe just determine if all characters have been read so that this can be incorporated to the Keydown event handler to correctly determine when to trigger transfer to the other textboxes?

  • scan into a multiple hidden text boxes – jsotola Mar 01 '22 at 03:20
  • It's not clear what make/model barcode scanner that you're using or why you haven't set your scanner as a USB Serial Device. You may consider [taking the tour](https://stackoverflow.com/tour) and provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) which others can use to re-create the issue that you're facing. – Tu deschizi eu inchid Mar 01 '22 at 03:25
  • If you're able to set your barcode scanner to a USB Serial device, the following may be helpful: https://stackoverflow.com/questions/65957066/serial-to-usb-cable-from-a-scale-to-pc-some-values-are-just-question-marks/65971845#65971845 – Tu deschizi eu inchid Mar 01 '22 at 03:32

1 Answers1

1

We use this below to capture UPS/USPS/FedEx barcodes. It is instantiated as a member variable on the Form, and then the Form.KeyPreview sends the char(s) to the KeyPress on it. It uses the hueristic that a human can't type those chars that fast, so it must be a scan. When it receives the correct length and a pattern matching one of the shipper's barcode format, it raises an event of a barcode scanned. You could probably adapt this to clean the spurious CRLF out of a scan as well.

using System;
using System.Diagnostics;

namespace YadaYada.LabelMaker.Library;

public class KeyReader
{
    public delegate void ScannedEventHandler(string trackingNumber);
    public static event ScannedEventHandler Scanned;

private static readonly object CurrentValueLock = new object();

private enum CurrentlyTrackingEnum
{
    None = 0,
    Ups = 1,
    Usps = 2,
}

private static CurrentlyTrackingEnum _currentlyTracking;
private static DateTime _lastPress = DateTime.MinValue;

public static void KeyPress(char pressed)
{

    lock (CurrentValueLock)
    {
        Debug.WriteLine($"Start:{CurrentValue}");
        if (DateTime.Now.Subtract(_lastPress).Ticks > 1000000) CurrentValue = string.Empty;
        _lastPress = DateTime.Now;
        CurrentValue += pressed.ToString().ToUpperInvariant();

        if (pressed == ']')
        {
            CurrentValue = string.Empty;
            _currentlyTracking = CurrentlyTrackingEnum.Usps;
        }
        else if (CurrentValue == "1Z")
        {
            _currentlyTracking = CurrentlyTrackingEnum.Ups;
        }

        if (_currentlyTracking == CurrentlyTrackingEnum.Ups)
        {
            if (CurrentValue.Length == "1Z52895E0392843135".Length)
            {
                OnScanned(CurrentValue);
                _currentlyTracking = CurrentlyTrackingEnum.None;
            }
        }
        else if (_currentlyTracking == CurrentlyTrackingEnum.Usps)
        {
            if (CurrentValue.Length == "9400110298370442324568".Length)
            {
                OnScanned(CurrentValue);
                _currentlyTracking = CurrentlyTrackingEnum.None;

            }
        }
        Debug.WriteLine($"End:{CurrentValue}");
    }
}

public static string CurrentValue { get; private set; } = String.Empty;

private static void OnScanned(string trackingnumber)
{
    Scanned?.Invoke(trackingnumber);
    CurrentValue = string.Empty;
}

}

Tim Bassett
  • 1,325
  • 1
  • 12
  • 23