Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从Arduino获取多个值;几乎是";C#中的实时?_C#_Performance_Arduino_Serial Communication - Fatal编程技术网

如何从Arduino获取多个值;几乎是";C#中的实时?

如何从Arduino获取多个值;几乎是";C#中的实时?,c#,performance,arduino,serial-communication,C#,Performance,Arduino,Serial Communication,所以我有一个C#应用程序,它通过Arduino的串行通信获取值。我根据一些管脚(数字管脚)的值给一些圆上色。当我运行应用程序时,我会有一点延迟,比如几乎一秒钟,我认为这是太多了。我将波特率从9600更改为112500(仍然相同)。在C#应用程序中,“读取”是通过调用计时器中的一些函数来实现的。我将计时器间隔从100毫秒更改为20毫秒,并且仍然保持相同的延迟。怎么办?此外,应用程序运行有点慢,例如,如果我将光标悬停在“最小化”按钮上方,该按钮不会立即高亮显示。我认为我发现了问题(或至少一个问题)

所以我有一个C#应用程序,它通过Arduino的串行通信获取值。我根据一些管脚(数字管脚)的值给一些圆上色。当我运行应用程序时,我会有一点延迟,比如几乎一秒钟,我认为这是太多了。我将波特率从9600更改为112500(仍然相同)。在C#应用程序中,“读取”是通过调用计时器中的一些函数来实现的。我将计时器间隔从100毫秒更改为20毫秒,并且仍然保持相同的延迟。怎么办?此外,应用程序运行有点慢,例如,如果我将光标悬停在“最小化”按钮上方,该按钮不会立即高亮显示。

我认为我发现了问题(或至少一个问题)

在你的勾号事件中,你写道

私人空间

tempreader_Tick(object sender, EventArgs e)
{
    red_light1 = Convert.ToInt32(comport.message(4, 8, 32));
    red_light2 = Convert.ToInt32(comport.message(4, 8, 33));

    yellow_light1 = Convert.ToInt32(comport.message(4, 8, 34));
    yellow_light2 = Convert.ToInt32(comport.message(4, 8, 35));

    green_light1 = Convert.ToInt32(comport.message(4, 8, 36));
    green_light2 = Convert.ToInt32(comport.message(4, 8, 37));

    [other actions]
您的
comport.message

public string message(byte paramone, byte paramtwo, byte paramthree)
{
    try
    {
        byte[] buffer = new byte[3];
        buffer[0] = Convert.ToByte(paramone);
        buffer[1] = Convert.ToByte(paramtwo);
        buffer[2] = Convert.ToByte(paramthree);
        currentPort.Open();
        currentPort.Write(buffer, 0, 3);
        int intReturnASCII = 0;
        char charReturnValue = (Char)intReturnASCII;
        Thread.Sleep(200);
        int count = currentPort.BytesToRead;
        string returnMessage = "";
        while (count > 0)
        {
            intReturnASCII = currentPort.ReadByte();
            returnMessage = returnMessage + Convert.ToChar(intReturnASCII);
            count--;
        }
        currentPort.Close();
        return returnMessage;
    }
    catch (Exception e)
    {
        return "Error";
    }

}
它包含
线程睡眠(200)
。因此,无论你调用tick的速度有多快,主线程都会在该函数中停留至少1.2秒


你必须减少这一点,或者,如果不能的话,使用类似于
backgroundworker
的东西来完成这项工作,并在其
runcompleted
事件中更新GUI。

我想看一些代码,但我认为这种行为的原因可能是你编写了一种永不停顿的阅读线程,因此,即使没有必要,CPU的使用率也总是100%。我希望没有太多的代码。。。然后是所有那些
Convert
方法的彻底疯狂,其中一半完全不做任何事情。