从一个的serialport读取和显示结果 [英] Reading from a serialport and displaying the result

查看:226
本文介绍了从一个的serialport读取和显示结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的code,我先到了迄今:

here's the code I've come-up with so far:

protected void Page_Load(object sender, EventArgs e)
{
    try {
    serialPort1.PortName = "COM4";
    serialPort1.BaudRate = 9600;
    serialPort1.Open();
    this.serialPort1.DataReceived += new      
        System.IO.Ports.SerialDataReceivedEventHandler(this.serialPort1_DataReceived);
    Label1.Text = "Connected";
    UpdatePanel1.Update();
    }
    catch (Exception ex) {

    }
}

string x = "";
private void serialPort1_DataReceived(object sender, 
      System.IO.Ports.SerialDataReceivedEventArgs e){

    x = serialPort1.ReadExisting();
    TextBox1.Text = x;
    UpdatePanel1.Update();
}

问题是code运行后...(即时通讯使用AJAX更新面板刷新文本框的文本)的事情是,当我在调试过程中设置断点,从串口接收到的数据文本框保留为空在变量被设置为文本框的新文本,但是当code完成后会显示什么....我是pretty确保UpdatePanel的工作,因为我已经测试过它。

the problem is after the code runs the text box remains empty... (Im using AJAX update panel to refresh the textbox text) the thing is when I set breakpoints during debug, the data received from the serial port is in the variable and is set as the new textbox text but when the code finishes nothing is displayed.... I'm pretty sure the updatepanel works because I've tested it.

PS中的serialport连接到RFID读取器和IM尝试读取标签。我已经成功codeD Windows窗体应用程序做我想做什么,但我需要将其迁移到ASP.NET

PS The serialport is connected to an rfid reader and im trying to read tags. I have successfully coded a windows form app to do what I want but I need to migrate it to ASP.NET

推荐答案

在ASP.NET应用程序中的串行端口读取数据,如果读卡器连接到服务器(而不是运行浏览器的计算机)才有效。看来,因为你是在本地测试它只能工作。

Reading data from serial port in ASP.NET application will only work if the reader is connected to the server (not the machine that is running the browser). It seems to be working only because you are testing it locally.

在初始页面渲染完成的 serialPort1 对象将被销毁。 ASP.NET页面只活直到页面呈现。然后,它被破坏。下一个请求再次重新创建的所有对象。因此,串行端口将再次每次浏览器将显示在服务器时间打开和试。

The serialPort1 object will be destroyed after the initial page render is complete. The ASP.NET page only lives until the page is rendered. Then it is destroyed. The next request again recreates all objects. So the serial port would be opened again and again every time the browser goes to the server.

的UpdatePanel 只改变了传统浏览器请求到AJAX - 它不会改变ASP.NET服务器处理的工作方式。它是由客户端浏览器仍然触发请求 - 响应行动。在你的情况的UpdatePanel 必须查询服务器上的计时器。

UpdatePanel only changes the classic browser requests into AJAX - it does not change how ASP.NET server processing works. It is still request-response actions triggered by the client browser. In your case the UpdatePanel would have to query the server on a timer.

您最好的选择是创建基于的ActiveX 控制Windows窗体code和嵌入在自己的网站。

Your best bet is to create a ActiveX control based on your Windows Forms code and embed that in your website.

这篇关于从一个的serialport读取和显示结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆