【问题标题】:Why is received data from the Serial Port Null?为什么从 Serial Port Null 接收数据?
【发布时间】:2012-10-07 09:18:12
【问题描述】:

下面的代码构建没有错误,与设备的连接似乎也可以工作,尽管我没有从设备得到任何反馈。

在 V.S. 上,我在myReceivedLines = sp.ReadExisting(); 行设置了一个断点,变量myReceivedLines 返回为空。

在连接到同一设备的另一个类似程序上,出现几行反馈(见下文),为什么在我的情况下这个变量为空?

出现在其他程序上的行:

Connecting...
start
Printer is now online.
echo:Marlin: 1.0.0 RC2
echo: Last Updated: 2012-05-22-1 | Author: eMAKER
...etc...

代码:

 //Fields
    string myReceivedLines;

    //subscriber method for the port.DataReceived Event
    private void DataReceivedHandler(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        myReceivedLines = sp.ReadExisting();
    }


       protected override void SolveInstance(IGH_DataAccess DA)
      {

        List<string> gcode = new List<string>();
        DA.GetDataList(0, gcode);
        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);
        bool sendtoprint= default(bool);
        DA.GetData(4, ref sendtoprint);

        if (!DA.GetDataList(0, gcode)) return;
        if (!DA.GetData(1, ref selectedportname)) return;
        if (!DA.GetData(2, ref selectedbaudrate)) return;
        if (!DA.GetData(3, ref connecttodevice)) return;
        if (!DA.GetData(4, ref sendtoprint)) return;


        SerialPort port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One); //Create the serial port
        port.DtrEnable = true;   //enables the Data Terminal Ready (DTR) signal during serial communication (Handshaking)
        port.Open();             //Open the port
        port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);


        if (gcode == null)
        {
            AddRuntimeMessage(GH_RuntimeMessageLevel.Warning, "Specify a valid GCode");
            return;
        }

        if (connecttodevice == true)
        {
            DA.SetDataList(0, myReceivedLines);
        }
        else
        {
            port.Close();
        }

            if (sendtoprint == true)
            {
                foreach (String s in gcode)
                {
                    port.WriteLine(s);
                }
            }

                  }

【问题讨论】:

    标签: c# visual-studio-2010 event-handling serial-port grasshopper


    【解决方案1】:
        if (connecttodevice == true)
        {
            DA.SetDataList(0, myReceivedLines);
        }
    

    SerialPort.ReadExisting() 不能返回 null,最坏的情况是你会得到一个空字符串。简单的解释是您在收到任何数据之前使用 myReceivedLines。在这种情况下,非常很可能,您在打开端口后立即使用它。在您使用 myReceivedLines 之前 DataReceived 触发的可能性很小。该代码非常难以理解,您需要对其进行大量修改。请记住,串行端口将在完全不可预测的时刻接收数据。您需要让 DataReceived 事件处理程序推送进度。

    【讨论】:

    • 非常感谢,我该如何push progress我的事件处理程序?
    • 有人建议切换port.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler); 和port.Open(); 的顺序,以便处理程序在打开端口之前触发,这有帮助吗?
    猜你喜欢
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    相关资源
    最近更新 更多