Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# waiting for the data on serial port

i am trying to get data from fingerprint scanner through c# application, but before the fingerprint can send, a my whole code executes.

I tried using delay function and System.Threading.Thread.Sleep(1000), so it can get data before the next step executes, but it all seems futile.

Could any one please provide any other option?

I am using "SerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)" to get data.

like image 321
sami manchnda Avatar asked Mar 23 '23 01:03

sami manchnda


1 Answers

This code works perfectly for me:

port = new SerialPort(port, 9600, Parity.None, 8, StopBits.One);
port.Open();
port.DiscardOutBuffer();
port.DiscardInBuffer();
port.DataReceived += OnScan;

void OnScan(object sender, SerialDataReceivedEventArgs args)
{
    SerialPort port = sender as SerialPort;

    string line = port.ReadExisting();
// etc
}
like image 192
Serve Laurijssen Avatar answered Apr 10 '23 05:04

Serve Laurijssen