通过串口写入
下面你可以看到我的程序输出。当我的设备开始启动时,我通过串行端口发送serialport1.write("\n");我把自动启动顶上了。那么我必须使用serialport1.write命令发送三个命令。如果线程在命令之前休眠,则命令执行,但我在命令完成后看到结果。我的意思是,如果我在命令之前 1 秒让线程休眠,对于 3 个命令,我会在 3 秒后在 richtextbox 中看到结果。为什么?还有其他好的方法来执行命令吗?我的代码如下。
代码:
_data = _serialPort.ReadExisting();
if (_data.StartsWith("Hit"))
{
Thread.Sleep(1000);
_serialPort.Write("\n");
Thread.Sleep(200);
_serialPort.Write("set moviargs...\n");
Thread.Sleep(200);
_serialPort.Write("saveenv\n");
Thread.Sleep(200);
_serialPort.Write("boot\n");
}
输出:
按任意键停止自动启动:3 2 1 0
SMDKV210 # 设置电影参数“setenv bootargs 控制台=ttySAC2,115200 smsc95xx.macaddr=00:09:DF:90:00:03"
SMDKV210#saveenv
将环境保存到SMDK可启动 设备...
完成
SMDKV210#启动
below you can see my program output.While my device begin to boot, through serial port I send serialport1.write("\n"); and i topped the autoboot. then I must send three commands with serialport1.write command. if the thread sleeps before the command the commands are executed but i see the result after the commands done. I mean if i get slept the thread 1 sec before the commands, for 3 commands i see the result in richtextbox after 3 seconds. Why? Is there any other good way to executing the commands? my code is below.
THE CODE:
_data = _serialPort.ReadExisting();
if (_data.StartsWith("Hit"))
{
Thread.Sleep(1000);
_serialPort.Write("\n");
Thread.Sleep(200);
_serialPort.Write("set moviargs...\n");
Thread.Sleep(200);
_serialPort.Write("saveenv\n");
Thread.Sleep(200);
_serialPort.Write("boot\n");
}
THE OUTPUT:
Hit any key to stop autoboot: 3 2 1
0SMDKV210 # set moviargs "setenv
bootargs console=ttySAC2,115200
smsc95xx.macaddr=00:09:DF:90:00:03"SMDKV210 # saveenv
Saving Environment to SMDK bootable
device...done
SMDKV210 # boot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的 UI 不会在 Thread.Sleep 中更新。
在每个“Sleep”调用之前添加一些 Application.DoEvents 步骤以更新 UI。 (或在后台线程上运行与串行端口通信的代码)
Application.DoEvents 调用应该处理“等待”事件,例如屏幕更新,但也会尝试启动按钮单击。您必须添加一种机制来禁用按钮单击(只需禁用按钮)。
You're UI won't update in Thread.Sleep.
Add some Application.DoEvents step before each 'Sleep' call to update the UI. (or run the code talking to the serial port on a background thread)
The Application.DoEvents call should handle 'waiting' events like screenupdate but will also try and start button clicks. You'd have to add a mechanism to disable button clicks(just disable the buttons).