2013-07-10 197 views
2

我目前正在使用称为Fluke 5500A多产品校准设备的设备。我用C#编写了一个小程序来与它进行交互,并学习更多关于它是如何工作的,但不幸的是,SerialPort.DataReceived给了我一些非常奇怪的结果。该计划的时间不长,所以我打算将它张贴在其全部在这里:SerialPort.DataReceived返回奇怪的结果

class Program 
{ 
    static public bool isExecuting = true; 
    static private string serialCommand; 
    static private string dataReceived; 

    static void Main(string[] args) 
    { 
     SerialPortConnection serialPort = new SerialPortConnection(); 

     serialPort.OpenSerialConnection(); 

     while (isExecuting == true) 
     { 
      Console.WriteLine("Enter a command or type q to quit."); 
      serialCommand = Console.ReadLine().ToUpper(); 

      if (serialCommand == "Q") 
       isExecuting = false; 
      else if (serialCommand == "CLEAR") 
       Console.Clear(); 
      else 
      { 
       dataReceived = serialPort.WriteSerialConnection(serialCommand); 
       Console.WriteLine(dataReceived); 
      } 
     } 

     serialPort.CloseSerialConnection(); 
    } 
} 

}

而且我SerialPortConnection类:

public class SerialPortConnection 
{ 
    private SerialPort serialPort; 
    private string dataReceived = "none"; 

    public SerialPortConnection(string comPort = "Com3", int baud = 9600, System.IO.Ports.Parity parity = System.IO.Ports.Parity.None, int dataBits = 8, System.IO.Ports.StopBits stopBits = System.IO.Ports.StopBits.One) 
    { 
     serialPort = new SerialPort(comPort, baud, parity, dataBits, stopBits); 
    } 

    public void OpenSerialConnection() 
    { 
     try 
     { 
      serialPort.Open(); 
     } 
     catch (Exception e) 
     { 
      Console.Write("\nError"); 
      Console.Write(e); 
     } 
    } 

    public string WriteSerialConnection(string SerialCommand) 
    { 
     try 
     { 
      serialPort.Write(String.Format(SerialCommand + "\r")); 
      dataReceived = serialPort.ReadExisting(); 
      return dataReceived; 
     } 
     catch (Exception e) 
     { 
      Console.Write("\nError"); 
      Console.Write(e); 
      return "Execution Unsuccessful"; 
     } 
    } 

    public void CloseSerialConnection() 
    { 
     try 
     { 
      serialPort.Close(); 
     } 
     catch (Exception e) 
     { 
      Console.Write("\nError"); 
      Console.Write(e); 
     } 
    } 
} 

我的问题是目前输出到控制台看起来像这样:

Enter a command or type q to quit. 
*IDN? 

Enter a command or type q to quit. 
OUT 50V <-- Command input 
*IDN? <-- Previous command echoed back 
FLUKE,5500A,8030005,2.61+1.3+2.0+* <-- Data received from previous command 
161> 
Enter a command or type q to quit. 
OPER 
OUT 50V 
162> 
Enter a command or type q to quit. 
STBY 
OPER 
163> 
Enter a command or type q to quit. 
*RST 
STBY 
164> 
Enter a command or type q to quit. 

该命令执行得很好,但输出到公司nsole似乎是被执行的最后一个命令以及该命令返回的任何数据。我不知道为什么会这样。

编辑:

感谢罗伯特·普的回答我实现下面的代码:你的问题的

var received = ""; 
bool isReading = true; 
while (isReading == true) 
{ 
    try 
    { 
     received += serialPort.ReadExisting(); 
     if (received.Contains('>')) 
      isReading = false; 
    } 
    catch (Exception e) 
    { 
    } 
} 
Console.WriteLine(received); 
+0

该设备只是回显命令。但是你没有检查,也没有等待足够长的时间来接收它。您的ReadExisting()调用无法正常工作。使用ReadLine()取而代之。 –

+0

@HansPassant感谢您的信息,但只是让线程休眠几秒钟然后尝试ReadExisting()会不会更容易?我没有考虑到需要时间的事实。 – DanteTheEgregore

+0

@HansPassant假设我等了很长时间才能收到,我应该等多少时间?半秒钟?两秒钟?也许是十分之一秒? – DanteTheEgregore

回答

2

一个部分是串行通信是远远瞬间。 .ReadExisting()方法从SerialPort对象的缓冲区中提取数据,但不做任何事情来确保设备已完成发出其命令。想想看这样的:

+----+   +----------+   +------------+ 
|Your|--Command-->|Serial |---UART--->| Device | 
|Code|   |Port Obj |   |   | 
+----+   +----------+   +------------+ 

+----+   +----------+   +------------+ 
|Your|<--Read-----|Serial |   |Device  | 
|Code| (is empty) |   |   |(processing)| 
+----+   +----------+   +------------+ 

+----+   +----------+   +------------+ 
|Your|   |Serial |<-response-|Device  | 
|Code|   |(has data)|   |(responding)| 
+----+   +----------+   +------------+ 

+----+   +----------+   +------------+ 
|Your|--Command2->|Serial |---UART--->| Device | 
|Code|   |(has data)|   |   | 
+----+   +----------+   +------------+ 

+----+   +----------+   +------------+ 
|Your|<--Read-----|Serial |   |Device  | 
|Code| (previous |   |   |(processing)| 
+----+ response) +----------+   +------------+ 

相反,寻找一个图案,标记或其他识别标记知道传输完成之前发送下一个命令。继续阅读(期待超时 - 他们被抛出作为例外!),直到你知道你已收到整个答复。在这种情况下,可能是>字符表示“准备好进行更多输入”。您可以将其解释为“响应已完成”。

+0

谢谢。在您的建议之后添加了示例代码,以供我初始发布。我很感激帮助。 – DanteTheEgregore