Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.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
C# 从textfile命令获取值_C# - Fatal编程技术网

C# 从textfile命令获取值

C# 从textfile命令获取值,c#,C#,我有一个小问题,我想不出来。我有一个可以读取文本文件的代码。当我读取文本文件时,文件中有命令。此命令适用于连接到串行端口的泵。所以我要做的就是让所有的命令通过串口发送。我可以这样做,但现在我必须发出wait(value)命令。wait命令的值总是不同的。因此,我必须获取wait命令的值,然后将wait命令的值放入线程中。Sleep(waitvalue)。因此,waitvalue是wait命令的值 这是我读取文本文件的代码: Stream mystream; OpenFileDia

我有一个小问题,我想不出来。我有一个可以读取文本文件的代码。当我读取文本文件时,文件中有命令。此命令适用于连接到串行端口的泵。所以我要做的就是让所有的命令通过串口发送。我可以这样做,但现在我必须发出
wait(value)
命令。wait命令的值总是不同的。因此,我必须获取wait命令的
值,然后将wait命令的值放入
线程中。Sleep(waitvalue)
。因此,
waitvalue
是wait命令的值

这是我读取文本文件的代码:

Stream mystream;
        OpenFileDialog commandFileDialog = new OpenFileDialog();

        if (commandFileDialog.ShowDialog() == DialogResult.OK)
        {
            if ((mystream = commandFileDialog.OpenFile()) != null)
            {
                string fileName = commandFileDialog.FileName;
                CommandListTextBox.Text = fileName;

                string[] readText = File.ReadAllLines(fileName);
                foreach (string fileText in readText)
                {
                    _commandList.Add(fileText);
                }
                CommandListListBox.DataSource = _commandList;

            }
        }
_commandlist是一个StringList。StringList是我的同事提供的一个函数,在这个函数中,您将有一个字符串列表。 在stringlist中,我将放置文件中的文本。 然后我将把_commandlist指定为我的列表框的数据源

这是运行命令的代码,也是我试图从wait命令获取值的代码的一部分。但是我不知道如何得到这个值

_comport.PortName = "COM6";
        _comport.Open();
        foreach (string cmd in _commandList)
        {
            if (cmd.Contains("WAIT"))
            {
                //Action

            }
            _comport.Write(cmd + (char)13);
            Thread.Sleep(4000);

        }
        _comport.Close();
线程.Sleep(4000)
中,我必须用我的waitvalue替换4000

文本文件的一部分:
运行
等待(1000)
停止
等待(1600)
RUNW
等待(4000)
停止

有人能帮我吗? 提前感谢

试试这个:

foreach (string cmd in _commandList)
{
    if (cmd.StartsWith("WAIT"))
    {
        //Action
    }
    _comport.Write(cmd + (char)13);
    int wait = 0;
    var waitString=cmd.Substring(cmd.IndexOf('(') + 1, 
        cmd.IndexOf(')') - cmd.IndexOf('(') - 1);
    if (Int32.TryParse(waitString, out wait))
    {
        Thread.Sleep(wait);
    }
}
[编辑]

我不确定自己是否完全理解了处理逻辑,但下面是我对代码结构的最佳猜测

// iterate through the commands
foreach (string cmd in _commandList)
{
    // check if the current command is WAIT
    if (cmd.StartsWith("WAIT"))
    {
        int
            wait = 0, // the amount of milliseconds to sleep
            startPosition = cmd.IndexOf('(') + 1,
            length = cmd.IndexOf(')') - cmd.IndexOf('(') - 1;
        // check if the length of the string between '(' and ')' is larger then 0
        if (length > 0)
        {
            var waitString = cmd.Substring(startPosition, length);
            // check if the string between '(' and ')' is an integer
            if (Int32.TryParse(waitString, out wait))
            {
                // sleep for 'wait' milliseconds
                Thread.Sleep(wait);
            }
        }
    }
    // current command is not WAIT
    else
    {
        // send it to the COM port
        _comport.Write(cmd + (char)13);
    }
}

文件中等待项的结构是什么?知道这一点后,只需使用split拆分字符串,然后在生成的数字字符串上转换.ToInt32。将结果int传递到您的睡眠调用中。我将编辑问题,并将其放入文本文件的一部分。wait命令是字符串,因此是的,我必须将其转换为int,但我不知道如何获取值。小问题,线程中的wait是什么。Sleep(wait)。因为它不知道。或者我必须做一根绳子什么的?很抱歉这么问,但我仍在学习我得到了这个错误:长度不能小于零。参数名称:length只是为了确保:当读取等待行时,线程应该停止。如果该行不是等待行,则应处理该命令?谢谢您的帮助