2

我正在使用 SerialPort.ReadLine() 方法来显示从串行端口接收到的数据(下面的代码)。

以前的代码看起来像这样并接收到数据,但它没有发送数据。现在反过来了:

由于我将port.DataReceived事件放在if(port==null)语句中并添加SerialPort port;为字段,因此我不再接收数据。将事件放在 if 语句中可以改变接收和显示数据的方式吗?我该如何解决?

 //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);
                bool connecttodevice = default(bool);
                DA.GetData(3, ref connecttodevice);

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

                //Event Handling Method
                if (connecttodevice == true)
                {
                    port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
                    DA.SetDataList(0, myReceivedLines);
                }
                //Enables the data terminal ready (dtr) signal during serial communication (handshaking)
                port.DtrEnable = true;
                port.Open();
            }

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

            //If the port is open do all the rest
            if (port.IsOpen)
            {
                //Assigning the input to variables for the code.
                List<string> gcode = new List<string>();
                DA.GetDataList(0, gcode);
                bool sendtoprint = default(bool);
                DA.GetData(4, ref sendtoprint);
                bool homeall = default(bool);
                DA.GetData(5, ref homeall);

                //What happens when input is set
                if (sendtoprint == true)
                {

                if (homeall == true)
                {
                    port.Write("G28" + "\n");
                }

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

1 回答 1

1

尝试这样的事情,从端口创建部分中删除事件处理程序的附加

if (port == null) 
{ 
    string selectedportname = default(string); 
    DA.GetData(1, ref selectedportname); 
    int selectedbaudrate = default(int); 
    DA.GetData(2, ref selectedbaudrate); 
    bool connecttodevice = default(bool); 
    DA.GetData(3, ref connecttodevice); 

    //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; 

} 

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); 
       // DA.SetDataList(0, myReceivedLines); // Not sure how you want to remove this
        port.Close();
    } 
}
于 2012-10-15T12:01:37.570 回答