2

私は現在、Fluke 5500A Multi-Product Calibration Device というデバイスを使用しています。私はC#で小さなプログラムを書いて、それと対話し、それがどのように機能するかについて学びましたが、残念ながら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.

コマンドは問題なく実行されますが、コンソールへの出力は、実行された最後のコマンドと、そのコマンドによって返されたデータのように見えます。なぜこれができるのかわかりません。

編集:

Robert P's answer のおかげで、次のコードを実装しました。

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

1 に答える 1

2

あなたの問題の 1 つは、シリアル通信が瞬時に実現できないことです。この.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) +----------+           +------------+

代わりに、パターン、トークン、またはその他の識別マークを探して、次のコマンドを送信する前に送信が完了したことを確認してください。返信全体を受け取ったことを確認するまで、読み続けてください (タイムアウトを予期してください - それらは例外としてスローされます!)。この場合、その>文字は「さらに入力する準備ができている」ことを表している可能性があります。「対応完了」とも解釈できます。

于 2013-07-10T15:51:19.230 に答える