Barcode Scanner - Serial Port
I just newbie, and I was having task - recieve data from BarCode scaner by serial port... I spent a lot of time... and I have next result
using System.IO.Ports;
using System.Timers;
namespace BarCode_manager
{
public partial class MainWindow : Window
{
private static SerialPort currentPort = new SerialPort();
private static System.Timers.Timer aTimer;
private delegate void updateDelegate(string txt);
public MainWindow()
{
InitializeComponent();
currentPort.PortName = "COM6";
currentPort.BaudRate = 9600;
currentPort.ReadTimeout = 1000;
aTimer = new System.Timers.Timer(1000);
aTimer.Elapsed += OnTimedEvent;
aTimer.AutoReset = true;
aTimer.Enabled = true;
}
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
if (!currentPort.IsOpen)
{
currentPort.Open();
System.Threading.Thread.Sleep(100); /// for recieve all data from scaner to buffer
currentPort.DiscardInBuffer(); /// clear buffer
}
try
{
string strFromPort = currentPort.ReadExisting();
lblPortData.Dispatcher.BeginInvoke(new updateDelegate(updateTextBox), strFromPort);
}
catch { }
}
private void updateTextBox(string txt)
{
if (txt.Length != 0)
{
aTimer.Stop();
aTimer.Dispose();
txtReceive.Text = Convert.ToString(aTimer.Enabled);
currentPort.Close();
}
lblPortData.Text = txt;
lblCount.Content = txt.Length;
txtReceive.Text = Convert.ToString(aTimer.Enabled);
}
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (currentPort.IsOpen)
currentPort.Close();
}
}
}
User45362
Updated on June 04, 2022Comments
-
User45362 over 1 year
So, I'm trying to use my Barcode Scanner as a 'Serial' device as opposed to a Keyboard emulator but it is not creating the com port. I have scanned the set-up codes from the manual that set it as a Serial device, that seems to configure the scanner correctly (it stops sending scanned codes to text-box\text editor) but because there is no COM port, I cannot capture the data when I scan a barcode......
Windows installed the driver when it was first plugged in, there wasn't a disk\driver supplied... wondered if anyone else has experienced the same issue.....
Here is my code....
class Program { // Create the serial port with basic settings private SerialPort port = new SerialPort("com1", 9600, Parity.None, 8, StopBits.One); [STAThread] static void Main(string[] args) { new Program(); } private Program() { string[] ports = System.IO.Ports.SerialPort.GetPortNames(); Console.WriteLine("Incoming Data:"); // Attach a method to be called when there // is data waiting in the port's buffer port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived); // Begin communications port.Open(); // Enter an application loop to keep this thread alive Application.Run(); } private void port_DataReceived(object sender, SerialDataReceivedEventArgs e) { // Show all the incoming data in the port's buffer Console.WriteLine(port.ReadExisting()); } }
I get the error message..... 'The port 'com1' does not exist'..... when I try to open the Port.
When I create a virtual Port (using 3rd party app) the code runs BUT I still don't get the data from the Scanner....