for循环not循环not looping [英] a for loop not loop not looping

查看:155
本文介绍了for循环not循环not looping的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

之前问了一个关于字符串数组的愚蠢问题,并在键入问题后自己找到了答案(也感谢Manfred R. Bilhy),所以我希望再次这样做.我必须将一组字符串从串行端口发送到等待板.
我对董事会有意见,可以发送和接收数据,只是无法获取所有数据而已:

Hi All,

Asked a stupid question earlier about string arrays and found the answer myself after typing the question (thanks also to Manfred R. Bilhy) so I hoping to do this again. I have to send a group of strings from the serial port to a waiting board.
I have comms to the board I can sent and recieve data as well it is just I can''t get it to upload all the data:

for (int b = 0; b <= 5; b++)
         {
             Command = CommandA[1] + '\n' + '\r';
             rtbIncoming.SelectionStart = rtbIncoming.Text.Length;
             rtbIncoming.ScrollToCaret();
             Length_Command = Command.Length;
             for (int Counter = 0; Counter <= Length_Command + 1; Counter++)
             {
                 if (Counter == Command.Length)
                 {
                     return;
                 }
                 Command_Sent = Command.Substring(Counter, 1);
                 myComPort.Write(Command_Sent);

             }


那就是for循环,这就是写出作品的方法的问题(我很久以前就写过的一首歌.
使用串行端口监视器,我可以告诉它使用0D或回车,命令OK和OD以及>来回复我的命令.提示.
有人有想法吗?
Glenn


That is the for loop thats being the problem the method of writing out works (bit of a kludge I worked out ages ago.
Using a Serial Port Monitor I can tell it replies to my command with 0D or Carriage Return, the text OK and an OD and a > prompt.
Anybody Any Ideas?
Glenn

推荐答案

我们使用AGG的Advanced Serial Port Monitor非常好.我添加了可怕的Thread.Sleep并得到了一个奇怪的反应.我讨厌这么说,但是我认为开发板的运行状况不佳!
We use Advanced Serial Port Monitor by AGG its quite good. I added the dreaded Thread.Sleep and got an odd reaction. I hate to say it but I think the board is not playing nicely!


如果您的开发板仅等待一个字符,则可能是该循环太快了,因此您的微控制器程序认为它很多字符您是否尝试过在连续写入之间添加睡眠?

艾迪·塔斯(Addy Tas),建议阅读另一篇文章,我同意.但是单线程微控制器可能不需要它.我有一种直觉,您可以在得到答复时直接阻止.
您可以轻松地使用这样的循环来等待:

If your board wait an only character, may be this loop is too fast, so your microcontroller program considere it a many chars. Have you tried to add a sleep between successive writes ?

Addy Tas, suggest to read in another thread, i agree. But the mono-threaded microcontroller probably do not need that. I have the intuition you can simply block while getting your replies.
You can easily wait using a loop like this one :

while(myComPort.BytesToRead ==0) 
   ;



如果什么也没发生,那是一个危险的inifnite循环,可以使用类似的方法解决(我这里没有代码,但是应该是类似的东西)



Which is a dangerous inifnite loop if nothing happend, can be worked around with something like this (i don''t have the code here, but it should be something like that)

public int WaitForData(int NbDataToWait, int Timeout)
{
    int timeWaited = 0;
    int timeToWait = 1; // 1 ms
    while (timeWaited < Timeout && MyCompPort.BytesToRead < NbDataToWait)
    {
        timeWaited += timeToWait ;
        System.Threading.Thread.Sleep(timeToWait );
    }
    return timeWaited;
}


这篇关于for循环not循环not looping的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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