Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I want to put the serial communication with an external device to a second thread, c#.

Here there is my first thread doing some stuff.
And there is the serial worker, the second thread:
- It opens the serial port
- It asks the external serial device regulary: Are you alive?
- It receives queries from the first thread with a string and an int like: "Command2", 33 and it should give this to the
external serial device.
- Sometimes there comes an answer from the external serial device back with a string and an int like: "Command4", 55 and the second thread should give this to the first thread and it should start a process there.
- The second thread also does own things like error detection which I want to keep free the first thread of.

Serial communication itself works fine but I have problems with threads, delegates, BeginInvokes, EventHandlers, etc.

My first thread:

Class MainProgramme
{
internal void StartSerialCommunication
{
  //Here I want the second thread to open the serial port and I want to start the regulary serial device check, all in the second thread.
}
internal bool SerialCommand(string s, int i)
{
  //Here I want to give the command s and i to the second thread asynchronously. That means this process does not wait for an answer from the second thread.
}
//The following process should be fired by the second thread because the second thread has received some important data from the external serial device.
internal void SerialAnswered
{
  //Here I want to get the string and the int from the second device back.
}
}
My second thread:
Class SerialCommunication
{
internal bool SerialDeviceIsAlive = true;
public bool SerialOpen
{	
SerialPortMsp.Open();
RegularyDeviceCheckTimer.Enabled = true;
return true;
}
private void RegularyDeviceCheckTimer_Tick(System.Object 
sender, System.EventArgs e)
{
if SerialDeviceIsAlive == true)
{ 
SerialDeviceIsAlive == false;
}
else
{
  //Here I want to inform the first thread that the answer from the external serial device is missing.
}
SerialSend("AreYouAlive");
}
public void SerialReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{ 
IAmAlive = true;
int buff = SerialPortMsp.ReadExisting();
if (buff.IndexOf(new string('\n', 1)) > 0)
{
  //Here I want to inform the first thread that the answer was buff.
}
}
public bool SerialSend(string SerialCommand) 
{
SerialPortMsp.Write(SerialCommand.ToCharArray(), 0, 
SerialCommand.Length);
return true;	
}
}

I need help in calling processes and transferring data between the two threads.
Thank you,
Posted
Updated 1-Jun-11 5:29am
v2
Comments
Orcun Iyigun 1-Jun-11 11:30am    
Edited for readability.

1 solution

For transferring data between threads, use a blocking queue.
Please see my article on the topic, complete with full source code and usage samples:

Simple Blocking Queue for Thread Communication and Inter-thread Invocation[^]. Pay attention, for v.4.0 you can also use BlockingCollection, but is should be used exactly as in my usage samples.

Basically, this is the implementation of the same mechanism as invocation using System.Threading.Dispatcher or System.Windows.Forms.Control Invoke or BeginInvoke, only solution is for a custom threads, and the methods mentioned above only works for UI threads.

Now, my concern is this: "It asks the external serial device regularly: Are you alive?". This will only work if your device on the other end of a serial cable supports this kind of polling.

Also, please understand: one purpose of using another thread is to avoid the complexity of asynchronous models. Do everything synchronously, don't be afraid of blocking calls. This is what threads are for.

Also, take a look at my collection of links to my past answers about threads and invocation:

On invocation:
Control.Invoke() vs. Control.BeginInvoke()[^],
Problem with Treeview Scanner And MD5[^].

Threading:
How to get a keydown event to operate on a different thread in vb.net[^],
Control events not firing after enable disable + multithreading[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900