串行端口连接 - 仅适用于断点

问题描述

问题

我有一个用于控制灯的串行端口连接。

我遇到的问题是它似乎经常工作,但在调试模式下始终如一地工作 我遇到了断点。

我的尝试

我尝试添加延迟以允许在初始化端口后进行连接

await Task.Delay(model.TimeIntervel);

代码

 public async Task<ActionResult> RunExperiment(Experimentviewmodel model)
    {

        if (model.Intensity > 250)
        {
            TempData["BadMsg"] = "Intensity value is out of range. Please try again with a value between 0 W/m2 and 250 W/m2";
            return RedirectToAction("Index");

        }
        if (model.TimeIntervel <= 0)
        {
            TempData["BadMsg"] = "Time interval value is out of range. Please try again with a value >0 in ms";
            return RedirectToAction("Index");

        }

        if (model.TimeIntervel <= 0 && model.Intensity > 250)
        {
            TempData["BadMsg"] = "Time interval value is out of range. Please try again with a value >0 in ms. Intensity value is out of range. Please try again with a value between 0 W/m2 and 250 W/m2";
            return RedirectToAction("Index");

        }
        #region Port Set Up
        //Set up the port 
        SerialPort port = new SerialPort("COM3",57600,(Parity)0,8,(StopBits)1);
        //Set Flow Control to none
        port.Handshake = Handshake.None;
        //Open Port
        port.open();
        #endregion

        if (model.AllLamps == true)
        {

            //Set Logic From Form
            port.Write("gintensity " + model.Intensity + " \r");
            port.Write("genable \r");

            //Set Time Delay
            await Task.Delay(model.TimeIntervel);

            //Turn Off Lamp/Lamps
            port.Write("gdisable \r");
        }
        else {

            //Set Logic From Form for individual lamp
            port.Write("w "+model.LampID+"m1 " + model.Intensity + " \r");
            port.Write("w "+model.LampID+"m1 enable \r");

            //Set Time Delay
            await Task.Delay(model.TimeIntervel);

            //Turn Off all Lamp/Lamps
            port.Write("gdisable \r");

        }

      

        //Close Port
        port.Close();

        //Successful Experiment Message
        TempData["GoodMsg"] = "Experiment Ran Successfully";

        //Return to Form
        return RedirectToAction("Index");
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)