简体   繁体   English

从串行端口读取数据后,文本未追加到TextBox

[英]Text is not Appending to TextBox after reading data from serial Port

I have written a program in which i am reading data from Serial Port. 我编写了一个程序,在其中我从串行端口读取数据。 After Reading the data, i am Displaying the status of it in a textbox named "lblStatus". 读取数据后,我正在名为“ lblStatus”的文本框中显示其状态。 But after reading from the port everytime, the text in the textbox is not getting updated. 但是每次从端口读取后,文本框中的文本都不会更新。

Here is my aspx file code. 这是我的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>

Here is my .cs file for it. 这是我的.cs文件。

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


}

After the appropriate operation I am appending text to string variable "result" and then assigning it to lblStatus.Text. 经过适当的操作后,我将文本附加到字符串变量“结果”,然后将其分配给lblStatus.Text。 But the Text is not updating in the Browser. 但是文本不会在浏览器中更新。 Please, Help me Update the Text in the TextBox. 请帮我更新文本框中的文本。

Your status message is inside an update panel with UpdateMode set to conditional. 您的状态消息位于将UpdateMode设置为有条件的更新面板中。 This means the update panel will only refresh if a control inside it causes a post back, or a defined trigger causes it to refresh. 这意味着更新面板仅在其内部的控件导致回发或定义的触发器导致其刷新时才会刷新。 You can define the trigger by adding the following after the closing (or before the opening) ContentTemplate tag, but still keeping it inside the asp:UpdatePanel tags : <Triggers> asp:AsyncPostBackTrigger ControlID="btnStart" /> </Triggers> 您可以通过在关闭(或在打开之前)ContentTemplate标记之后添加以下内容来定义触发器,但仍将其保留在asp:UpdatePanel标记内: <Triggers> asp:AsyncPostBackTrigger ControlID="btnStart" /> </Triggers>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM