hbnas.blogg.se

Serial port datareceived event not firing
Serial port datareceived event not firing











serial port datareceived event not firing
  1. #Serial port datareceived event not firing serial#
  2. #Serial port datareceived event not firing code#
  3. #Serial port datareceived event not firing windows#

A very pragmatic one is to simply not close the port, Windows will take care of it when your process terminates. Then sleep for a while, a second or two, to ensure that any threadpool threads in-flight have completed running.

#Serial port datareceived event not firing code#

But not unlikely to happen when you are debugging your code since you don't actually care about the data.Ī counter-measure is to turn off handshaking so the device cannot send anything anymore. That's not great, it is guaranteed to cause data loss. Unsubscribing doesn't fix it, the threadpool thread already got its target method.ĭo realize what you are doing to trigger this problem, you are closing the port while the device is sending data. So it is fairly inevitable that, if the device is actively sending data, that it can race with your Close() call and run after you closed it. And yes, they've got the awkward habit of running your code at an unpredictable time, it is not instant. Your DataReceived event handler is called on a threadpool thread. I wish there was a more effective way to remove the callback rather than inserting sleep periods in the program. It seems to work now but I'm not really that happy. Private void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)īyte b = Convert.ToByte(comPort.ReadByte()) ĮDIT: following the suggestions, I changed StopStreaming code to something like this: public override void StopStreaming() Here´s my Vb code : Public Class Form1 Public WithEvents SerialP As System.IO.Ports.SerialPort. private SerialPort comPort = new SerialPort() ĬomPort.DataReceived += comPort_DataReceived ĬomPort.DataReceived -= comPort_DataReceived Hello everyone, recently bought an Arduino Leonardo, however I am not able to receive data on VB.net side, I can send data to Arduino but can´t receive, DataReceived event doesn´t fire on VB.NET, can someone help me, already googled and I can´t see no reason why event DataReceived isn´t working. So this is not a race condition that happens randomly but rather a systematic problem indicating a completely broken code! However, I fail to see how. What am I missing? What is the correct way to close the port and stop the events?ĮDIT: I get this error every time I run my code. As a result, in my event handler code I get an InvalidOperationException with the message that "The port is closed". The problem is that the DataReceived event doesnt.

#Serial port datareceived event not firing serial#

I'm experiencing a weird behavior while trying to stop a SerialPort: the DataReceived event continues to fire after unsubscribing and after calling close! (see StopStreaming in the following code). I have a WPF test app for evaluating event-based serial port communication (vs.













Serial port datareceived event not firing