2

以下のコードでは、受信した文字列がmyReceivedLinesシリアル ポートに接続したときに表示されます (connecttodeviceが true の場合)。ただし、別のコマンドを起動すると消えます ( homealltrue の場合)。

myReceivedLinesクラス内で呼び出されるフィールドを追加してString.Add()、受信したすべてのフィードバックと送信されたコマンドにメソッドを使用できるようにしました (プログラム内のコンソールのように)。

コマンドが送信されるとフィードバックが消えるのはなぜですか? また、すべての文字列が変数に残るようにするにはどうすればよいmyReceivedLinesですか? myReceivedLineサブスクライバーメソッド内で発生するため、文字列が消えますか? どうすれば解決できますか?

NB: GH_DataAccess.SetDataList(Int32, IEnumerable) は、値を出力に割り当てるための、 Grasshopperと呼ばれるソフトウェアであるカーネルからのメソッドです (このカーネルからの GH_Component.SolveInstance() メソッド内で使用する必要があります)。これを使用して myReceivedLines を視覚化します。

コード:

 public class SendToPrintComponent : GH_Component
    {
        //Fields
        List<string> myReceivedLines = new List<string>();
        SerialPort port;

        //subscriber method for the port.DataReceived Event
        private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            SerialPort sp = (SerialPort)sender;
            while (sp.BytesToRead > 0)
            {
                try
                {
                    myReceivedLines.Add(sp.ReadLine());
                }
                catch (TimeoutException)
                {
                    break;
                }
            }
        }

        protected override void SolveInstance(IGH_DataAccess DA)
        {
           //Opening the port
            if (port == null)
            {
                string selectedportname = default(string);
                DA.GetData(1, ref selectedportname);
                int selectedbaudrate = default(int);
                DA.GetData(2, ref selectedbaudrate);


                //Assigning an object to the field within the SolveInstance method()
                port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One);

                //Enables the data terminal ready (dtr) signal during serial communication (handshaking)
                port.DtrEnable = true;
                port.WriteTimeout = 500;
                port.ReadTimeout = 500;
            }

            //Event Handling Method
            bool connecttodevice = default(bool);
            DA.GetData(3, ref connecttodevice);

            **if (connecttodevice == true)**
            {
                if (!port.IsOpen)
                {
                    port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                    DA.SetDataList(0, myReceivedLines);
                    port.Open();

                }
            }
            else
            if (port.IsOpen)
                {
                    port.DataReceived -= new SerialDataReceivedEventHandler(DataReceivedHandler);
                    port.Close();
                }


            if (port.IsOpen)
            {
                DA.SetData(1, "Port Open");
            }

            //If the port is open do all the rest
            if (port.IsOpen)
            {
                bool homeall = default(bool);
                DA.GetData(5, ref homeall);


                //Home all sends all the axis to the origin
                **if (homeall == true)**
                {
                    port.Write("G28" + "\n");
                    myReceivedLines.Add("G28" + "\n");
                    DA.SetDataList(2, myReceivedLines);
                }

            }
            else
            {
                DA.SetData(1, "Port Closed");
            }
        }
}
4

2 に答える 2

2

文字列に追加しようとしている場合は、StringBuilder オブジェクトをお勧めします。

または、解像度を下げて、+= 演算子を使用します。

string s = "abcd";
s+="efgh";
Console.WriteLine(s); //s prints abcdefgh
于 2012-10-16T13:57:27.957 に答える
0

まず、変数 (myReceivedLines とポート) は静的ではありません。SendToPrintComponent クラスをどのように使用しているかがわからないため、それらを静的にするかどうかはわかりません。DA.SetDataList(0, myReceivedLines); について説明していただけますか。問題がそこにある可能性があるため、コードを含めることをお勧めします...

于 2012-10-16T14:23:26.910 に答える