簡體   English   中英

如何從C#中的串行端口讀取整數值?

[英]how to read integer values from serial port in C#?

我有一個通過RS-232通信的Keyence相機。 它被配置為在觸發時輸出三個整數值。 我在讀取整數值時遇到問題。 我嘗試使用char數組緩沖區,但它僅讀取輸出中的第一個+號。 我用膩子測試過,輸出是這樣的

+ 346.0,+ 261.0,098

我想知道是否有什么需要讀取這些整數值的?

    static void Main(string[] args)
    {

        char[] buffer1 = new char[200] ;


        SerialPort port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);

        port.Open();
        if (port.IsOpen) { Console.WriteLine("port is now open"); } else { Console.WriteLine("port not opened correctly"); }

        port.Write("T"); //triggers the camera

        port.Read(buffer1, 0, 200);

        for (int i = 0; i < 200; i++)
        {
            Console.WriteLine(buffer1[i]);
        }
        Console.ReadLine();
    }

從串行端口讀取數據之前,我並沒有讀取預期的所有內容。

原來我正在讀取設備的響應,但尚未編寫。 我認為串行端口對象將繼續嘗試填充緩沖區,直到達到讀取超時為止,而事實並非如此。

在我的場景中,我知道要從串行端口讀取多少個字符。 因此,如果您知道可以對讀取執行重復操作,直到字符緩沖區已滿。 我不知道如果從SerialPort.BaseStream讀取是否同樣適用。

SerialPort serialPort; 

char[] buffer = new char[expectedLength];
int totalBytesRead = 0;

//continue to read until all of the expected characters have been read
while (totalBytesRead < expectedLength)
{
    totalBytesRead += serialPort.Read(buffer, totalBytesRead, expectedLength - totalBytesRead); 
}

這是我使用的代碼(簡體):

public class Scanner : SerialPort
{
    private string _word;
    private int _globalCounter;
    private readonly char[] _rxArray = new char[2047];

    public Scanner()
    {
        DataReceived += MyDataReceivedEventHandler;
    }

    public event EventHandler<CodeScannedEventArgs> CodeScanned;
    private void MyDataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
    {
        do
        {
            var rxByte = (byte)ReadByte();

            // end of word
            if (rxByte == 10)
            {
                // first byte (02) and last two bytes (13 and 10) are ignored
                _word = new string(_rxArray, 1, _globalCounter - 2);

                DisplayData(_word);

                _globalCounter = 0;
            }
            else
            {
                _rxArray[_globalCounter] = (char)rxByte;
                _globalCounter++;
            }
        } while (BytesToRead > 0);
    }

    private void DisplayData(string receivedText)
    {
        OnCodeScanned(new CodeScannedEventArgs(receivedText));
    }

    protected void OnCodeScanned(CodeScannedEventArgs e)
    {
        EventHandler<CodeScannedEventArgs> handler = CodeScanned;

        if (handler != null)
        {
            handler(this, e);
        }
    }
}

我使用的掃描儀在其掃描的所有內容中都添加了字節02作為前綴,並添加了字節13和10作為后綴,因此我很容易將其分解為單詞。 顯然,您將需要稍微更改實現,以便它對您有用。

編輯-CodeScannedEventArgs類:

public class CodeScannedEventArgs : EventArgs
{
    public CodeScannedEventArgs(string scannedCode)
    {
        ScannedCode = scannedCode;
    }

    public string ScannedCode { get; set; }
}

我使用了port.ReadTo(“ \\ r”),它可以正常工作,因為輸出以回車結束。

但是我想知道使用數據接收事件有什么好處?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM