5

シリアルポート (arduino から) から送信されたコマンドを監視し、キーストロークを他のさまざまなプログラムに送信するプログラムを作成しています。

イベントにディスパッチャーを配置しましたが、一度しか実行されません...コードをステップ実行しましたが、最初は最後まで実行されましたが、2 回目は実行されませんでした。

private void portReadEvent(object sender, SerialDataReceivedEventArgs args)
        { //break here
            Action dump = delegate() 
            {
                dumpInfoText(serialPort.ReadExisting());
            }; //create my deligate

            txt_portInfo.Dispatcher.Invoke(dump); //run dispatcher

        }

        private void dumpInfoText(string text)
        {
            string completeCommand = "";
            foreach (char C in text)
            {
                if (serDump.Length <=8 && (C != (char)0x0A || C != (char)0x0D)) //check 
                {
                    serDump = serDump + C; //add char
                }
                if (serDump.Length == 8) //check to see if my info has a complete command (serDump is global)
                {
                    completeCommand = serDump; //move to complete
                    serDump = ""; // reset dump
                    if (C == (char)0x0A || C == (char)0x0D) //dont add crlf
                    {
                        serDump = serDump + C; //add char
                    }
                }
                if (completeCommand.Length == 8)
                {
                    txt_portInfo.Text = txt_portInfo.Text + completeCommand; //do something with it.
                }
            }
4

1 に答える 1

0

ああ、修正されました...理由がわかりません。

    private void portReadEvent(object sender, SerialDataReceivedEventArgs args)
    {
        txt_portInfo.Dispatcher.Invoke(new Action(() => {dumpInfoText(serialPort.ReadExisting());})); //run dispatcher

    }
于 2012-12-08T03:53:42.107 に答える