C# 不再从串行端口接收数据?

C# 不再从串行端口接收数据?,c#,event-handling,serial-port,C#,Event Handling,Serial Port,我使用SerialPort.ReadLine()方法显示从串行端口接收的数据(下面的代码) 以前,代码看起来像并接收数据,但它没有发送数据。现在情况正好相反: 因为我将port.DataReceived事件放在if(port==null)语句中,并添加了SerialPort端口作为字段,我不再接收数据。将事件放在if语句中是否会改变接收和显示数据的方式?我怎样才能解决这个问题 //Fields List<string> myReceivedLines = new L

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

以前,代码看起来像并接收数据,但它没有发送数据。现在情况正好相反:

因为我将
port.DataReceived
事件放在
if(port==null)
语句中,并添加了
SerialPort端口作为字段,我不再接收数据。将事件放在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");
            }
        }
//字段
List myReceivedLines=新列表();
串行端口;
//port.DataReceived事件的订阅服务器方法
私有void DataReceivedHandler(对象发送方,System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort sp=(SerialPort)发送方;
而(sp.BytesToRead>0)
{
尝试
{
添加(sp.ReadLine());
}
捕获(超时异常)
{
打破
}
}
}
受保护的覆盖实例(IGH_DataAccess DA)
{
//开埠
如果(端口==null)
{
string selectedportname=默认值(字符串);
DA.GetData(1,ref selectedportname);
int selectedbaudrate=默认值(int);
DA.GetData(2,参考选择波特率);
bool connecttodevice=默认值(bool);
DA.GetData(3,参考connecttodevice);
//将对象指定给SolveInstance方法()中的字段
端口=新的串行端口(selectedportname、selectedbaudrate、奇偶校验.None、8、StopBits.One);
//事件处理方法
if(connecttodevice==true)
{
port.DataReceived+=新的SerialDataReceivedHandler(DataReceivedHandler);
DA.SetDataList(0,myReceivedLines);
}
//在串行通信(握手)期间启用数据终端就绪(dtr)信号
port.DtrEnable=true;
port.Open();
}
//显示端口是否打开
if(端口等参线)
{
DA.设置数据(1,“端口打开”);
}
//如果端口打开,则执行所有其他操作
if(端口等参线)
{
//将输入分配给代码的变量。
List gcode=新列表();
DA.GetDataList(0,gcode);
bool sendtoprint=默认值(bool);
DA.GetData(4,参考sendtoprint);
bool homeall=默认值(bool);
DA.获取数据(5,参考homeall);
//设置输入时会发生什么
if(sendtoprint==true)
{
if(homeall==true)
{
端口写入(“G28”+“\n”);
}
}
其他的
{
DA.设置数据(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();
    } 
}

什么调用
SolveInstance
?如果调用方没有设置
端口
,它将是
null
,因为您在定义它时没有设置它。当您第一次创建端口时,connecttodevice为true?connecttodevice是否设置为true?@MarkHall和@BugFinder
connecttodevice
默认设置为falsen打开程序后,我将其设置为true以连接到特定端口。在某些情况下,当我打开程序时,端口已经打开。这可能是问题所在吗?是的,因为由于端口已创建,如果connecttodevice为false,它将永远不会附加事件感谢lot@MarkHall,它可以工作,但我收到一条错误消息,说
解决方案例外情况:程序中的端口已打开
,这会阻止我向串行端口发送命令。此外,只有在我发送命令(如
HomeAll
)时才会出现反馈。反馈是否仅在设备接收数据时出现?因此,我是否可以向机器发送虚拟代码,以便在与机器连接时获得反馈?@ArthurMamou Mani你得到的是哪一行端口已经打开异常?你是否每次发送数据时都发送一个connecttodevice?当设置
HomeAll
为true时,异常会发生在程序中,有没有办法在调试时找出它在代码中发生的位置?@ArthurMamou Mani我不确定你使用的是什么类型的设备r正在连接到,但端口打开通常仅在PC上,没有数据发送到设备。@ArthurMamou Mani看到对connecttodevice代码的更改,在打开端口之前添加了一个检查,以查看端口是否未打开。您还可以在端口上放置一个断点。open语句用于检查每次发送命令时是否命中端口