我对我的代码做了一些修改,并对其进行了测试。它仍然不能很好地工作,所以我希望在这里有一些指导。
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.IO.Ports
Partial Class Main
Inherits System.Web.UI.Page
Protected Sub form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles form1.Load
Dim Serialport7 As New IO.Ports.SerialPort
With Serialport7
.PortName = "COM7"
.BaudRate = 9600
.Parity = Parity.None
.DataBits = 8
.StopBits = StopBits.One
End With
Try
Serialport7.Open()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
Dim ReceivedData As String
ReceivedData = Serialport7.ReadLine()
If ReceivedData.Substring(0, 1) = "T" Then
TextBox1.Text = ReceivedData.Remove(0, 1)
ElseIf ReceivedData.Substring(0, 1) = "H" Then
TextBox2.Text = ReceivedData.Remove(0, 1)
End If
TextBox3.Text = System.DateTime.Now
Serialport7.Close()
End Sub
End Class我所遇到的错误:
感谢所有的指导。谢谢。
发布于 2013-03-30 16:16:50
您有一个简单的解决方案来刷新您的页面,因为您的服务器无法控制客户端web浏览器,因此不会在任何时候刷新新的数据(当然,这不是不可能的,但它很复杂,也不太安全)。
因此,您可以做的是添加一段javascript代码来定期刷新页面。
最好的方法是添加一个javascript函数,该函数对webservice进行ajax调用,后者将读取串口数据,然后将数据发送回客户端页面。在那里,您可以更新文本框值。
要更新页面,可以使用以下javascript代码:window.location.reload(),如果需要定期更新它,可以查看这个问题
要在webforms中添加一个webservice并定期调用它,请查看以下链接:http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-web-services
http://api.jquery.com/
如果您不希望定期刷新页面,并且只想在新数据可用时刷新您的值,请查看websockets (在我开始回答时忘记了它们)。
关于websockets的编辑也应该考虑,您的方法可以被许多不同的用户调用,也可以在不同的线程中调用。因此,您需要确保串口读取线程的安全性,并将结果存储在某个地方,以避免向不同的用户发送不同的数据。--
图像您有一个用户A谁更新您的网页,然后用户B更新它。当用户A调用页面时,您已经从串口读取了一些数据,因此用户A和用户B将显示不同的数据。
如果数据为1, 2, 3, 4
在您当前的实现中,用户A将看到1,用户B将看到2
https://stackoverflow.com/questions/15719901
复制相似问题