Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么从串行端口接收的数据为空?_C#_Visual Studio 2010_Event Handling_Serial Port_Grasshopper - Fatal编程技术网

C# 为什么从串行端口接收的数据为空?

C# 为什么从串行端口接收的数据为空?,c#,visual-studio-2010,event-handling,serial-port,grasshopper,C#,Visual Studio 2010,Event Handling,Serial Port,Grasshopper,下面的代码构建时没有错误,与设备的连接似乎也能正常工作,尽管我没有从设备获得任何反馈 在V.S.上,我在行myReceivedLines=sp.ReadExisting()处放置了一个断点和变量myReceivedLines返回空值 在另一个连接到同一设备的类似程序上,会出现几行反馈(见下文),为什么在我的例子中这个变量为null 出现在其他程序上的行: Connecting... start Printer is now online. echo:Marlin: 1.0.0 RC2 echo:

下面的代码构建时没有错误,与设备的连接似乎也能正常工作,尽管我没有从设备获得任何反馈

在V.S.上,我在行
myReceivedLines=sp.ReadExisting()处放置了一个断点
和变量
myReceivedLines
返回空值

在另一个连接到同一设备的类似程序上,会出现几行反馈(见下文),为什么在我的例子中这个变量为null

出现在其他程序上的行:

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

                  }
代码:

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

                  }
//字段
字符串myReceivedLines;
//port.DataReceived事件的订阅服务器方法
私有void DataReceivedHandler(对象发送方,System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort sp=(SerialPort)发送方;
myReceivedLines=sp.ReadExisting();
}
受保护的覆盖实例(IGH_DataAccess DA)
{
List gcode=新列表();
DA.GetDataList(0,gcode);
string selectedportname=默认值(字符串);
DA.GetData(1,ref selectedportname);
int selectedbaudrate=默认值(int);
DA.GetData(2,参考选择波特率);
bool connecttodevice=默认值(bool);
DA.GetData(3,参考connecttodevice);
bool sendtoprint=默认值(bool);
DA.GetData(4,参考sendtoprint);
如果(!DA.GetDataList(0,gcode))返回;
如果(!DA.GetData(1,ref selectedportname))返回;
如果(!DA.GetData(2,ref selectedbaudrate))返回;
如果(!DA.GetData(3,ref connecttodevice))返回;
如果(!DA.GetData(4,ref sendtoprint))返回;
SerialPort端口=新的SerialPort(selectedportname,selectedbaudrate,奇偶校验.None,8,StopBits.One);//创建串行端口
port.DtrEnable=true;//在串行通信(握手)期间启用数据终端就绪(DTR)信号
port.Open();//打开端口
port.DataReceived+=新的SerialDataReceivedHandler(DataReceivedHandler);
if(gcode==null)
{
AddRuntimeMessage(GH_RuntimeMessageLevel.Warning,“指定有效的GCode”);
返回;
}
if(connecttodevice==true)
{
DA.SetDataList(0,myReceivedLines);
}
其他的
{
port.Close();
}
if(sendtoprint==true)
{
foreach(gcode中的字符串s)
{
写线港(s);;
}
}
}

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

非常感谢,我如何
推送进度
我的事件处理程序?建议有人切换
port.DataReceived+=新的SerialDataReceivedEventHandler(DataReceivedHandler)的顺序
端口打开()以便处理程序在打开端口之前启动,这有帮助吗?