Serialport Threading

A

agloth

Hi,

I have a serial port reader application that uses datareceived event
[port_DataReceived(object, SerialDataReceivedEventArgs)] to read
incoming data. The application also send response some of the
messages.

The problem is the application sometimes behaves weird. (My
application does not respond some of the messages) And i think its
because of the threading. I know that datareceived event uses a
seperate thread. Should i use a third thread to response messages ?

If so , how can i do that. Can i use something like that ? And again
the main question is ; should i use the datareceive thread or use a
seperate thread to send messages ?

private void SendData(string data)
{
Thread dataThread = new Thread(delegate()
{
Manager.SendData(data);
});
dataThread.Start();
}

Im not good at threading so i will appreciate any help.

Regards,

Tolga
 
J

jp2msft

Ok, I've got to get off these boards and start doing some work before I get
into trouble, but real quick I'll say this:

You need a TcpListener. Start the TcpListener then declare a TcpClient and
set it to the TcpListener's AcceptTcpClient method.

Google these, as there are examples on how to do them out there.

Sorry I don't have more time.

Hope it helps.
 
J

Jim H

agloth said:
Hi,

I have a serial port reader application that uses datareceived event
[port_DataReceived(object, SerialDataReceivedEventArgs)] to read
incoming data. The application also send response some of the
messages.

The problem is the application sometimes behaves weird. (My
application does not respond some of the messages) And i think its
because of the threading. I know that datareceived event uses a
seperate thread. Should i use a third thread to response messages ?

If so , how can i do that. Can i use something like that ? And again
the main question is ; should i use the datareceive thread or use a
seperate thread to send messages ?

private void SendData(string data)
{
Thread dataThread = new Thread(delegate()
{
Manager.SendData(data);
});
dataThread.Start();
}

Im not good at threading so i will appreciate any help.

Regards,

Tolga

Be careful threading serial port applications. The device you are talking
to may not be able to handle receiving data while transmitting data back to
your app. Been there, had those issues. Again, it depends on the device
and the amount of data. Also, I've never data received events, but I'd say
check and make sure data didn't come in while you were handling that
received event and that's why your app "does not respond" to some messages.
A new event may not be triggered if data was still in the receive buffer.
Again, I haven't used receive events, so I don't know what sets and resets
the triggers.

jim
 
T

Tolga Sofuoglu

Thanks for the answers.

So what should i do to avoid the send receive datas to overlap. Will
using a 3rd thread to send data solve this ?

I'm still confused ; wether to create a seperate thread (to send data)
or not ?


I have a serial port reader application that uses datareceived event
[port_DataReceived(object, SerialDataReceivedEventArgs)] to read
incoming data. The application also send response some of the
messages.
The problem is the application sometimes behaves weird. (My
application does not respond some of the messages) And i think its
because of the threading. I know that datareceived event uses a
seperate thread. Should i use a third thread to response messages ?
If so , how can i do that. Can i use something like that ? And again
the main question is ; should i use the datareceive thread or use a
seperate thread to send messages ?
private void SendData(string data)
{
Thread dataThread = new Thread(delegate()
{
Manager.SendData(data);
});
dataThread.Start();
}
Im not good at threading so i will appreciate any help.

Tolga

Be careful threading serial port applications. The device you are talking
to may not be able to handle receiving data while transmitting data back to
your app. Been there, had those issues. Again, it depends on the device
and the amount of data. Also, I've never data received events, but I'd say
check and make sure data didn't come in while you were handling that
received event and that's why your app "does not respond" to some messages.
A new event may not be triggered if data was still in the receive buffer.
Again, I haven't used receive events, so I don't know what sets and resets
the triggers.

jim
 
A

agloth

I solved the problem 2 mins ago. Sending in a seperate thread worked
like a charm. But i will appreciate ; if there is any expert to
confirm this is the best way for serial communication. (Main Thread,
Receive Thread, Send Thread)

Regards,
Tolga

Thanks for the answers.

So what should i do to avoid the send receive datas to overlap. Will
using a 3rd thread to send data solve this ?

I'm still confused ; wether to create a seperate thread (to send data)
or not ?

news:45a40861-ba62-4752-a31b-453aa513a934@y21g2000hsf.googlegroups.com...
Hi,
I have a serial port reader application that uses datareceived event
[port_DataReceived(object, SerialDataReceivedEventArgs)] to read
incoming data. The application also send response some of the
messages.
The problem is the application sometimes behaves weird. (My
application does not respond some of the messages) And i think its
because of the threading. I know that datareceived event uses a
seperate thread. Should i use a third thread to response messages ?
If so , how can i do that. Can i use something like that ? And again
the main question is ; should i use the datareceive thread or use a
seperate thread to send messages ?
private void SendData(string data)
{
Thread dataThread = new Thread(delegate()
{
Manager.SendData(data);
});
dataThread.Start();
}
Im not good at threading so i will appreciate any help.
Regards,
Tolga
Be careful threading serial port applications. The device you are talking
to may not be able to handle receiving data while transmitting data back to
your app. Been there, had those issues. Again, it depends on the device
and the amount of data. Also, I've never data received events, but I'd say
check and make sure data didn't come in while you were handling that
received event and that's why your app "does not respond" to some messages.
A new event may not be triggered if data was still in the receive buffer.
Again, I haven't used receive events, so I don't know what sets and resets
the triggers.
 
B

Ben Voigt [C++ MVP]

agloth said:
I solved the problem 2 mins ago. Sending in a seperate thread worked
like a charm. But i will appreciate ; if there is any expert to
confirm this is the best way for serial communication. (Main Thread,
Receive Thread, Send Thread)

Depends on what you mean by "best"... simple to implement, simple to use,
fast, reliable, efficient?

You can't get all of them.
Regards,
Tolga

Thanks for the answers.

So what should i do to avoid the send receive datas to overlap. Will
using a 3rd thread to send data solve this ?

I'm still confused ; wether to create a seperate thread (to send
data) or not ?

news:45a40861-ba62-4752-a31b-453aa513a934@y21g2000hsf.googlegroups.com...

I have a serial port reader application that uses datareceived
event [port_DataReceived(object, SerialDataReceivedEventArgs)] to
read incoming data. The application also send response some of the
messages.
The problem is the application sometimes behaves weird. (My
application does not respond some of the messages) And i think its
because of the threading. I know that datareceived event uses a
seperate thread. Should i use a third thread to response messages ?
If so , how can i do that. Can i use something like that ? And
again the main question is ; should i use the datareceive thread
or use a seperate thread to send messages ?
private void SendData(string data)
{
Thread dataThread = new Thread(delegate()
{
Manager.SendData(data);
});
dataThread.Start();
}
Im not good at threading so i will appreciate any help.


Be careful threading serial port applications. The device you are
talking to may not be able to handle receiving data while
transmitting data back to your app. Been there, had those issues.
Again, it depends on the device and the amount of data. Also, I've
never data received events, but I'd say check and make sure data
didn't come in while you were handling that received event and
that's why your app "does not respond" to some messages. A new
event may not be triggered if data was still in the receive buffer.
Again, I haven't used receive events, so I don't know what sets and
resets the triggers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top