将多个命令发送到串口并在发送另一个之前等待回复 [英] Sending multiple command to serial port and wait for reply before send another

查看:188
本文介绍了将多个命令发送到串口并在发送另一个之前等待回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



寻求串行端口datarecevied的帮助。



我有一个存储多个命令的文件。

我需要将命令逐个发送到串口并等待回复并显示继续发送另一个命令。



我在每个命令发送之间使用thread.sleep但它不起作用。



请帮忙。



谢谢



我的尝试:



Hi All,

seek for help on the serial port datarecevied.

I have a file stored multiple commands.
I need to send the command one by one to serial port and wait for reply and display for continue sending the other command.

I'm using thread.sleep between each command send but it does not work.

please help.

Thanks

What I have tried:

I'm using thread.sleep between each command send but it does not work. 

推荐答案

停止玩Thread.Sleep:它可能无法帮助你。

我的方式这样做是设置一个命令队列,并使用DataReceived事件来获取响应。当响应完成后,从队列中取出下一个命令,然后发送它,让DataReceived事件在它到达时处理它的响应。

当队列为空时,命令集完成。
Stop playing with Thread.Sleep: it probably won't help you.
The way I would do it is to set up a Queue of commands, and use the DataReceived event to get the response. When the response is complete, take the next command off the queue, and send that, and let the DataReceived event process it's response when it arrives.
When the Queue is empty, the set of commands is complete.


通常,所有异步IO操作都应该在自己的线程中执行,以避免阻塞应用程序。否则你会在GUI中滞后,或者在最糟糕的情况下GUI会被完全阻止。



对于你的情况,你需要至少一个线程来接收回复和发送排队的命令。



接收线程应该缓冲数据,直到收到完整的响应。如何执行此操作取决于数据(例如,使用文本数据换行或在接收到协议标题中公布的字节数后)。



已收到完整响应,这是向其他线程发出的信号。在您的情况下,这将是显示响应的主(GUI)线程和用于发送下一个命令的传输队列。请注意,将数据从线程传递到GUI元素需要特殊处理。



传输线程可以使用阻塞调用发送,因为它无论如何都必须等待。发送后必须等待收到的响应信号。



您可以将发送和接收放入一个或两个线程。



用于接收的伪代码(假设文本数据):

In general all asynchronous IO operations should be performed in own threads to avoid blocking the application. Otherwise you will have lags in the GUI or the GUI would be blocked completely in the worst case.

For your case you would need at least one thread for receiving replies and transmitting the queued commands.

The receiving thread should buffer data until a full response has been received. How to do this depends on the data (e.g. line feed with text data or after receiving the number of bytes that has been announced in a protocol header).

Once a full response has been received, this is signaled to other threads. In your case this will be the main (GUI) thread to show the response and the transmit queue to enable sending of the next command. Note that passing data from a thread to GUI elements requires special treatment.

The transmit thread can simply send using a blocking call because it has to wait anyway. After sending it has to wait for the response received signal.

You can put sending and receiving into one or two threads.

Pseudo code for receiving (assuming text data):
// Check for kill (terminate thread) event (wait with no timeout)
while (!KillEvent)
{
    do 
    {
        // Blocking call until a character is available
        // Should have a timeout and corresponding handling (break here)
        rxChar = ReceiveChar();
        Buffer += rxChar;
    }
    while (rxChar != '\n');
    // Signal other threads that a response has been received
    // Pass a copy of Buffer to the main thread for display
}



在传输线程中,在发送命令后调用上面的命令或者在自己的线程中完成接收时等待事件。



使用线程和事件,你可以避免调用 sleep ,这通常应该避免,并确保不浪费系统时间。


In the transmit thread, after sending a command call the above or wait for the event when receiving is done in an own thread.

Using threads and events you avoid calling sleep which should be generally avoided and ensure that no system time is wasted.


我的 SerialDevice 这里的课程:

用于GPIB / Visa /串行接口的多线程通信 [ ^ ]

实现完全是上面的Solution2中描述的过程:使用异步命令,I / O在一个单独的线程中完成,该线程使用简单的阻塞调用进行写/读。



关于解决方案1的一个评论:在非UI线程上调用DataReceived事件,因此如果你想使用它,你需要学习一些关于线程的东西(例如,DataReceived处理程序可以调用同步回调以避免共享字段或UI组件出现问题。)
My SerialDevice class here:
Multithreaded communication for GPIB/Visa/Serial interfaces[^]
implements exactly the process described in Solution2 above: with asynchronous commands the I/O is done in a separate thread which uses simple blocking calls for write/read.

One comment on the solution 1: the DataReceived event is called on a non-UI thread therefore you need to learn something about threading anyway if you want to use it (e.g. the DataReceived handler can call a synchronized callback to avoid problems with shared fields or UI components).


这篇关于将多个命令发送到串口并在发送另一个之前等待回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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