Discussion:
Serial port in thread
(too old to reply)
Sonnich Jensen
2016-01-08 15:59:13 UTC
Permalink
Hi all

I have a small problem - I have a thread, which will scan comports for a specific item (by starting by the most obvious). My problem is that I rely on an answer from the device I am looking for.
However, I open a port, send a "ping", wait for "pong". But while my thread is in Thread.Sleep I dont get the receive event from the port, and thereby not any "pong" before I have been through all ports.
In Delphi I would have made an empty thread-class, which would synchronize to the search thread.
How do I make this work in C#?

foreach(port...)
{
open port
if in use then continue;
send ping
wait for pong (w/timeout)
if pong then break
}

In case you ask, I have a USB-COM adapater which will get any port name - so I go by the highest port, as it is most probably there. Replugged and I cannot be sure where it is.
Arne Vajhøj
2016-01-10 01:04:48 UTC
Permalink
Post by Sonnich Jensen
I have a small problem - I have a thread, which will scan comports
for a specific item (by starting by the most obvious). My problem is
that I rely on an answer from the device I am looking for. However, I
open a port, send a "ping", wait for "pong". But while my thread is
in Thread.Sleep I dont get the receive event from the port, and
thereby not any "pong" before I have been through all ports. In
Delphi I would have made an empty thread-class, which would
synchronize to the search thread. How do I make this work in C#?
foreach(port...)
{
open port
if in use then continue;
send ping
wait for pong (w/timeout)
if pong then break
}
Where does the Thread.Sleep come into the picture?

Arne
Sonnich Jensen
2016-01-11 08:32:24 UTC
Permalink
Post by Arne Vajhøj
Post by Sonnich Jensen
I have a small problem - I have a thread, which will scan comports
for a specific item (by starting by the most obvious). My problem is
that I rely on an answer from the device I am looking for. However, I
open a port, send a "ping", wait for "pong". But while my thread is
in Thread.Sleep I dont get the receive event from the port, and
thereby not any "pong" before I have been through all ports. In
Delphi I would have made an empty thread-class, which would
synchronize to the search thread. How do I make this work in C#?
foreach(port...)
{
open port
if in use then continue;
send ping
wait for pong (w/timeout)
if pong then break
}
Where does the Thread.Sleep come into the picture?
Arne
here is a detialed code:
but still, during the sleep I dont get the receive event in thread - it fires correctly once all the ports have been "scanned"

foreach(port...)
{
open port
if in use then continue;
pong=false;
send ping
// wait for pong (w/timeout) read:
int i timeout=100;
while( !pong && timeout-->0)
{
thread.sleep(10);
}

if pong then
break
else
port.close;

}
Arne Vajhøj
2016-01-28 03:08:02 UTC
Permalink
Post by Sonnich Jensen
but still, during the sleep I dont get the receive event in thread - it fires correctly once all the ports have been "scanned"
foreach(port...)
{
open port
if in use then continue;
pong=false;
send ping
int i timeout=100;
while( !pong && timeout-->0)
{
thread.sleep(10);
}
if pong then
break
else
port.close;
}
So this one:

https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived%28v=vs.110%29.aspx

is not firing if the main thread is in Thread.Sleep?

Arne
Arne Vajhøj
2016-02-07 02:08:49 UTC
Permalink
Post by Arne Vajhøj
Post by Sonnich Jensen
but still, during the sleep I dont get the receive event in thread -
it fires correctly once all the ports have been "scanned"
foreach(port...)
{
open port
if in use then continue;
pong=false;
send ping
int i timeout=100;
while( !pong && timeout-->0)
{
thread.sleep(10);
}
if pong then
break
else
port.close;
}
https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived%28v=vs.110%29.aspx
is not firing if the main thread is in Thread.Sleep?
What thread is this code executing in?

Arne

Loading...