ndevriendt
Programmer
Hello,
I've a barcode scanner where I can communicate in a two way direction.
I'm listening on a serial port to know if an user have scanned a barcode. When data is entered in the serial port by scanning a code, I send a message back to the serial port that will be displayed at the barcode scanner.
My problem is that the event DataReceived is triggerd when I sent data to the serial port (by using a write command) and I get at the command readline() again the data from de barcode that the user has scanned.
Is there a way to clear everything in de serial port ?
I have already tried to use DiscardInBuffer but this is not helping.
See here some code:
_sp = new SerialPort(port);
_sp.DataReceived += new SerialDataReceivedEventHandler(BarCodeScanner_DataReceived);
_sp.Open();
void BarCodeScanner_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
barCode = _sp.ReadLine();
_sp.DiscardInBuffer();
// do some actions with the barcode
....
// Send a message
if (!(_sp.IsOpen)) _sp.Open();
_sp.Write(myMessage);
// After this write I comming back in this method and
// at ReadLine I'm getting back the barcode that the
// user has read. I'm looping...
}
I hope you can understand my problem.
Thans for your time and answer.
Devriendt Nico
I've a barcode scanner where I can communicate in a two way direction.
I'm listening on a serial port to know if an user have scanned a barcode. When data is entered in the serial port by scanning a code, I send a message back to the serial port that will be displayed at the barcode scanner.
My problem is that the event DataReceived is triggerd when I sent data to the serial port (by using a write command) and I get at the command readline() again the data from de barcode that the user has scanned.
Is there a way to clear everything in de serial port ?
I have already tried to use DiscardInBuffer but this is not helping.
See here some code:
_sp = new SerialPort(port);
_sp.DataReceived += new SerialDataReceivedEventHandler(BarCodeScanner_DataReceived);
_sp.Open();
void BarCodeScanner_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
barCode = _sp.ReadLine();
_sp.DiscardInBuffer();
// do some actions with the barcode
....
// Send a message
if (!(_sp.IsOpen)) _sp.Open();
_sp.Write(myMessage);
// After this write I comming back in this method and
// at ReadLine I'm getting back the barcode that the
// user has read. I'm looping...
}
I hope you can understand my problem.
Thans for your time and answer.
Devriendt Nico