从串行端口读取数据后,文本不会附加到TextBox

时间:2015-04-10 20:25:22

标签: c# asp.net serial-port rfid

我写了一个程序,我正在从串口读取数据。 读取数据后,我在名为" 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>

这是我的.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;
}


}

在适当的操作之后,我将文本附加到字符串变量&#34;结果&#34;然后将其分配给lblStatus.Text。 但文本未在浏览器中更新。 请帮助我更新TextBox中的文本。

1 个答案:

答案 0 :(得分:0)

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