在第二个线程中进行串行通信,异步进行吗? [英] Serial communication in second thread, asynchronously?

查看:71
本文介绍了在第二个线程中进行串行通信,异步进行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将与外部设备的串行通信放到第二个线程c#中.

这是我的第一个线程在做一些事情.
第二个线程是串行工作程序:
-打开串口
-它定期询问外部串行设备:您还活着吗?
-它从第一个线程接收带有字符串和int的查询,例如:"Command2",33,并将其提供给
外部串行设备.
-有时,外部串行设备会返回一个带有字符串和整数的答案,例如:"Command4",55,第二个线程应将此问题赋予第一个线程,并应在此启动进程.
-第二个线程也具有自己的功能,例如错误检测,我想让第一个线程保持空闲.

串行通信本身可以正常工作,但是我在线程,委托,BeginInvokes,EventHandlers等方面遇到问题.

我的第一个线程:

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,

推荐答案

要在线程之间传输数据,请使用阻塞队列.
请参阅我关于该主题的文章,并附有完整的源代码和用法示例:

用于线程通信和线程间调用的简单阻塞队列 [ ^ ].请注意,对于v.4.0,您也可以使用BlockingCollection,但应完全按照我的用法示例使用它.

基本上,这是与使用System.Threading.DispatcherSystem.Windows.Forms.Control InvokeBeginInvoke进行调用相同的机制的实现,仅解决方案适用于自定义线程,而上述方法仅适用于UI线程. >
现在,我担心的是:它定期询问外部串行设备:您还活着吗?".仅当您在串行电缆另一端的设备支持这种轮询时,此方法才有效.

另外,请理解:使用另一个线程的一个目的是避免异步模型的复杂性.同步执行所有操作,不要害怕阻塞呼叫.这就是线程的用途.

另外,请查看我过去有关线程和调用的答案的链接集合:

调用时:
Control.Invoke()与Control.BeginInvoke() [ ^ ],
Treeview Scanner和MD5的问题 [如何获取keydown事件在vb.net中的不同线程上操作 [启用禁用+多线程后控件事件不会触发 [ ^ ].

—SA
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


这篇关于在第二个线程中进行串行通信,异步进行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆