Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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#_Asp.net_Serial Port_Rfid - Fatal编程技术网

C# 从串行端口读取数据后,文本未附加到文本框

C# 从串行端口读取数据后,文本未附加到文本框,c#,asp.net,serial-port,rfid,C#,Asp.net,Serial Port,Rfid,我已经写了一个程序,在其中我从串行端口读取数据。 读取数据后,我在名为lblStatus的文本框中显示数据的状态。 但每次从端口读取后,文本框中的文本不会得到更新 这是我的aspx文件代码 <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="AddRFID.aspx.cs" Inherits="AddRFID" %> <

我已经写了一个程序,在其中我从串行端口读取数据。 读取数据后,我在名为lblStatus的文本框中显示数据的状态。 但每次从端口读取后,文本框中的文本不会得到更新

这是我的aspx文件代码

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master"     AutoEventWireup="true" CodeFile="AddRFID.aspx.cs" Inherits="AddRFID" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
 </asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">

<div id="wrapper">
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">Add RFID</h1>
        </div>

    </div>
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    Add RFID
                </div>
                <div class="panel-body">
                    <div class="row">
                        <div class="col-lg-6">

                            <div class="panel-body">
                                <form id="Form1" runat="server">
                                <asp:Button ID="btnStart" runat="server" Text="Start" OnClick="btnStart_Click"/>

                                <asp:Panel ID="Panel1" runat="server">

                                <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
                                <asp:UpdatePanel ChildrenAsTriggers="false" OnPreRender="UpdatePanel1_PreRender" UpdateMode="Conditional" ID="UpdatePanel1" runat="server">
                                <ContentTemplate>
                                <asp:TextBox ID="txtStatus" CssClass="form-control" runat="server" Rows="20" Height="200px" ></asp:TextBox>

                                </ContentTemplate>
                                </asp:UpdatePanel>


                                 <asp:Button ID="Button2" runat="server" Text="Read" OnClick="Button2_Click" />


                                </asp:Panel>

                       </form>
                            </div>

                        </div>

                    </div>
                </div>

            </div>
        </div>
    </div>
</div>
</asp:Content>
在适当的操作之后,我将文本附加到字符串变量result,然后将其分配给lblStatus.text。 但文本不会在浏览器中更新。
请帮助我更新文本框中的文本。

您的状态消息位于更新面板中,UpdateMode设置为conditional。这意味着仅当更新面板中的控件导致回发或定义的触发器导致刷新时,更新面板才会刷新。您可以通过在关闭ContentTemplate标记之后或打开ContentTemplate标记之前添加以下内容来定义触发器,但仍将其保留在asp:UpdatePanel标记中: asp:AsyncPostBackTrigger ControlID=btnStart/>

那么,您尝试过哪些诊断方法?你调试过了吗?发生了什么事?
public partial class AddRFID : System.Web.UI.Page
{
RFIDImplementation ri;
public string rfid_no;
public SerialPort port;
public static string withoutLast, result;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Panel1.Visible = false;
        txtStatus.TextMode = TextBoxMode.MultiLine;
    }
}
protected void submit_Click(object sender, EventArgs e)
{

}

protected void btnStart_Click(object sender, EventArgs e)
{
    Panel1.Visible = true;
    port = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
    SerialPortProgram();

}

private void SerialPortProgram()
{
    Console.WriteLine("Incoming Data:");

    // Attach a method to be called when there
    // is data waiting in the port's buffer
    port.DataReceived += new
      SerialDataReceivedEventHandler(port_DataReceived);

    // Begin communications
    try
    {
        port.Open();
        result += "Port Open \n";
    }

    catch (UnauthorizedAccessException uae)
    {
        result += "Access Denied to Port \n";
    }

    catch (InvalidOperationException ioe)
    {
        result += "Port Already Open \n";
    }
    catch (IOException io)
    {
        result += "RFID not Attached \n";
    }


}

private void port_DataReceived(object sender,
 SerialDataReceivedEventArgs e)
{
    // Show all the incoming data in the port's buffer
    string s = port.ReadLine();
    withoutLast = s.Substring(0, s.Length - 4);

    ShowData(withoutLast);
}

private void ShowData(String withoutLast)
{

    RFIDLogic rl = new RFIDLogic();
    RFID r = new RFID();

    r.R_No = withoutLast;
    r.O_ID = Convert.ToInt32(null);
    r.R_Status = false;

    if (rl.CheckExistingRno(r) == 0)
    {
        rl.Insert(r);
        result = result + "RFID Added \n";

    }
    else
    {
        result = result + "RFID Already Exist \n";

    }


}

protected void UpdatePanel1_PreRender(object sender, EventArgs e)
{
                 txtStatus.Text = result;
}
protected void UpdatePanel1_Load(object sender, EventArgs e)
{
                  txtStatus.Text = result;
}


}