Connecting to barcode scanner

Hello, I am having some trouble working out a functional one way connection with barcode scanner. It's supposed to be a really simple connection, when the scanner scans a barcode, event activates in main form and after that its all internal logic. Problem is, due to reasons we are unable to use OPOS connection, so we decided to use serial port connection, but that brought even more problems. The device randomly disconnects, causing the port to stay open, unable to reconnect to it, requiring whole restart of PC, sometimes even several times. Or in other instances, while it appears to be connected, port is open and everything seems fine, the SerialPort.DataReceived event doesn't fire. After months of trying to troubleshoot this, I kind of gave up on the serial port connection, is there any other way how to do a simplistic connection to barcode scanner?
5 Replies
Pobiega
Pobiega11mo ago
Depends entirely on the scanner. The ones I've worked with were all USB and just acted like keyboards. They literally typed the characters it decoded then sent an "enter" keypress
Lord Iori, The Cursed
Yeah, thats not really viable, as the scanned barcodes arent really text to be written somewhere but more like instructions for the app what to do
Pobiega
Pobiega11mo ago
Still, it depends entirely on the scanner itself.
Lord Iori, The Cursed
Gryphon 4500 but it doesnt really matter, i am currently looking into options of connecting to it as USB HID device, but accepting the input only in the background One would think connecting to scanner would be easier than this..
Sir Rufo
Sir Rufo11mo ago
Indeed it is very easy Most scanners are acting like a keyboard and that makes it very easy to handle
c#
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

PreviewTextInput += OnPreviewTextInput;
}

// configure scanner to send prefix and suffix
const string PREFIX = "\u001d";
const string SUFFIX = "\r";

private bool _isScanning;
private string _barcode = string.Empty;

private void OnPreviewTextInput( object sender, TextCompositionEventArgs e )
{
if ( !_isScanning && e.Text == PREFIX )
{
_isScanning = true;
e.Handled = true;
}
else if ( _isScanning && e.Text == SUFFIX )
{
_isScanning = false;
e.Handled = true;
if ( !string.IsNullOrEmpty( _barcode ) )
{
(_barcode, var barcode) = (string.Empty, _barcode);
ProcessBarcode( barcode );
}
}
else if ( _isScanning )
{
e.Handled = true;
_barcode += e.Text;
}
}
private void ProcessBarcode( string barcode )
{
// handle barcode
var json = JsonSerializer.Serialize( barcode );
BarcodeListBox.Items.Add( json );
}
}
c#
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();

PreviewTextInput += OnPreviewTextInput;
}

// configure scanner to send prefix and suffix
const string PREFIX = "\u001d";
const string SUFFIX = "\r";

private bool _isScanning;
private string _barcode = string.Empty;

private void OnPreviewTextInput( object sender, TextCompositionEventArgs e )
{
if ( !_isScanning && e.Text == PREFIX )
{
_isScanning = true;
e.Handled = true;
}
else if ( _isScanning && e.Text == SUFFIX )
{
_isScanning = false;
e.Handled = true;
if ( !string.IsNullOrEmpty( _barcode ) )
{
(_barcode, var barcode) = (string.Empty, _barcode);
ProcessBarcode( barcode );
}
}
else if ( _isScanning )
{
e.Handled = true;
_barcode += e.Text;
}
}
private void ProcessBarcode( string barcode )
{
// handle barcode
var json = JsonSerializer.Serialize( barcode );
BarcodeListBox.Items.Add( json );
}
}

Did you find this page helpful?