Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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#SerialPort Readline_C#_Serial Port - Fatal编程技术网

缓冲区中有多个换行符的C#SerialPort Readline

缓冲区中有多个换行符的C#SerialPort Readline,c#,serial-port,C#,Serial Port,我需要使用rs232从温度控制器获取一些数据。 控制器在每个完整命令的末尾向我发送每个字符的回显,如果命令成功应用,则返回“§”和“.”。我的测试代码如下所示: static string GetData(int memoryAddress) { if (!_session.IsOpen) return "No Connection"; string toSend = "*A_r_" + memoryAddress.ToString() + "_0" +

我需要使用rs232从温度控制器获取一些数据。 控制器在每个完整命令的末尾向我发送每个字符的回显,如果命令成功应用,则返回“§”和“.”。我的测试代码如下所示:

static string GetData(int memoryAddress)
 {
     if (!_session.IsOpen)
         return "No Connection";

     string toSend = "*A_r_" + memoryAddress.ToString() + "_0" + (char)21;

     foreach (char character in toSend)
         _session.Write(character.ToString());

    Thread.Sleep(100);
    return _session.ReadExisting();
}
*A_r_0_0§.250§
并返回如下内容:

static string GetData(int memoryAddress)
 {
     if (!_session.IsOpen)
         return "No Connection";

     string toSend = "*A_r_" + memoryAddress.ToString() + "_0" + (char)21;

     foreach (char character in toSend)
         _session.Write(character.ToString());

    Thread.Sleep(100);
    return _session.ReadExisting();
}
*A_r_0_0§.250§
现在我还需要在控制器中存储一些数据,并希望重用上面的部分,是否可以使用

ReadNewline (with Newline-Symbol §)
只获取回音并验证它,以便在下一个函数中读取返回的数据(如果我选择读取某些内容)? 首先返回哪个值?最后到达的那个还是第一个


另外,使用ReadNewline超时而不是Thread.sleep(100)是否更好?

最好使用“DataReceived”事件

    private Object responseSignal = new object();
    private string portResponse;
   _session.DataReceived += new SerialDataReceivedEventHandler(PortDataReceived);

static string GetData(int memoryAddress)
{
   if (!_session.IsOpen)
     return "No Connection";

   string toSend = "*A_r_" + memoryAddress.ToString() + "_0" + (char)21;

   foreach (char character in toSend)
     _session.Write(character.ToString());

  if (System.Threading.Monitor.Wait(responseSignal, 10000)) // max time we want to wait to receive the response
   {
       // successful get
   }   
   else
   {
       //  failed to receive the response 
    }
    return portResponse;
  }

  public void PortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        try
        {
            lock (responseSignal)
            {
                string tmpPortResponse = _session.ReadExisting();
                portResponse += tmpPortResponse;


                //Complete the response only when you get the end character
                if (tmpPortResponse.Contains('.'))
                {
                    // signal allows a waiting SendMessage method to proceed
                    System.Threading.Monitor.Pulse(responseSignal);
                }
            }
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message.ToString());
        }
    }

更改换行符属性,以便可以使用ReadLine()或ReadTo()。永远不要使用Sleep()来修复问题,强烈避免ReadExisting()。只需调用ReadTo()两次,第一次是获取回音,第二次是获取响应。你是否真的使用了响应取决于你。在第一个echo(char)到达之后,这个不会被调用,所以我仍然需要等待其余的响应到达吗?是的。您可以存储接收到的数据,并在接收到要查找的结束字符('.')时发出事件信号。这样做比使用Thread.Sleep()更好