不再从串口接收数据了?

时间:2012-10-15 11:44:59

标签: c# event-handling serial-port

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

以前代码看起来像that并收到数据,但它没有发送数据。现在是另一种方式:

由于我在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");
            }
        }

1 个答案:

答案 0 :(得分:1)

尝试这样的事情,删除将eventhandler附加到端口创建部分

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();
    } 
}