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
{
}
internal bool SerialCommand(string s, int i)
{
}
internal void SerialAnswered
{
}
}
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
{
}
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)
{
}
}
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,