1

我需要一个 PHP 服务器来与我的无线传感器交互。但我还需要该服务器由 Visual Basic 应用程序控制。

我在 Visual Basic 应用程序中需要的一些功能:

  1. 启动/停止服务器
  2. 服务器配置
  3. 访问服务器目录上的文件。

PHP 文件(服务器应用程序)只是从无线传感器模块接收数据并存储在平面数据库文件(CSV、XML)中。写入此数据后,Visual Basic 必须访问平面数据库文件以执行分析。

关于使用什么服务器以及哪些特定方法可以提供最简单的解决方案的任何建议?

4

1 回答 1

2

好吧,您想要的内容很广泛,但是关于您的 PHP 部分的信息还不够。

但我可以帮助你使用 VB.NET。这是一个可以真正提供帮助的类(以及一个子和一个事件)。

先举几个例子

只需加载 HTML 代码:

Dim Page As New WEBhtml("http://www.example.com/index.php?get=something")
While Page.IsReady = False
End While
If IsNothing(Page.Exception) Then 
    MsgBox(Page.GetHtml)
Else
    MsgBox(Page.Exception.Message)
End If

将 POST 发送到目的地(只是 Dim 行):

Dim Page As New WEBhtml("http://www.example.com/index.php?get=something", {"a=alpha", "b=beta", "c=I Don't Know :D !"})

使用处理:

Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim Page As New WEBhtml("http://www.e.com/i.php")
End Sub
Private Sub html_done(ByRef sender As WEBhtml) Handles Me.WebHtml_Done
    MsgBox("Timetook: " & sender.TimeTook / 1000 & "s")
    MsgBox("Url: " & sender.Url)
    If IsNothing(sender.Exception) Then
        MsgBox("Bandwidth: " & sender.Bytes / 1024 & "kb")
        MsgBox("HTML: " & sender.GetHtml)
    Else
        MsgBox("Error: " & sender.Exception.Message)
    End If
End Sub

看?很容易。


现在开始

按照这两个步骤

第一:添加 System.Web 引用

转到 Project > [Project name] Properties > Reference 之后按“添加...”按钮,并选中“System.Web”,然后按Ok。还要在“导入的命名空间”中检查它

第二:在“结束类”之前复制这个块

Public Shared Event WebHtml_Done(ByRef sender As WEBhtml)
Friend Shared Sub RaiseDone(ByRef wh As WEBhtml)
    RaiseEvent WebHtml_Done(wh)
End Sub
Public Class WEBhtml
    Private thrd As Threading.Thread
    Private Err As Exception
    Private BytesUsed As ULong = 0
    Private Time As UInteger = 0
    Private Html As String = ""
    Private _Url As String
    Private tmr As New Timer

    Private Sub initialize()
        tmr.Interval = 50
        AddHandler tmr.Tick, AddressOf Tick
        tmr.Enabled = True
        tmr.Start()
    End Sub

    Public Sub New(ByVal Url As String)
        thrd = New Threading.Thread(Sub() WEB_POST(Url))
        initialize()
        thrd.Start()
    End Sub
    Public Sub New(ByVal Url As String, ByVal PostData As String())
        thrd = New Threading.Thread(Sub() WEB_POST(Url, PostData))
        initialize()
        thrd.Start()
    End Sub

    Private Sub Tick(sender As Object, e As EventArgs)
        If thrd.IsAlive = False Then
            tmr.Enabled = False
            RaiseDone(Me)
        End If
    End Sub
    Private Sub WEB_POST(ByVal url As String, Optional ByVal values() As String = Nothing)
        _Url = url
        Dim data As String = ""
        Dim a, b As Integer
        b = My.Computer.Clock.TickCount
        Try
            For i = 0 To values.GetLength(0) - 1
                a = values(i).IndexOf("=")
                If a >= 0 Then
                    data += System.Web.HttpUtility.UrlEncode(Mid(values(i), 1, a)) & "=" & System.Web.HttpUtility.UrlEncode(Mid(values(i), a + 2))
                    If i < values.GetLength(0) - 1 Then data += "&"
                End If
            Next
        Catch
            data = ""
        End Try

        Try
            Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
            request.Method = "POST"
            Dim postdata As String = data
            Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postdata)
            request.ContentType = "application/x-www-form-urlencoded"
            request.ContentLength = byteArray.Length
            request.Timeout = 100000
            Dim dataStream As IO.Stream = request.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
            dataStream.Close()

            Dim response As Net.WebResponse = request.GetResponse()
            dataStream = response.GetResponseStream()
            Dim reader As New IO.StreamReader(dataStream)
            Dim responseFromServer As String = reader.ReadToEnd()
            reader.Close()
            dataStream.Close()
            response.Close()
            BytesUsed += responseFromServer.Length + byteArray.Length
            Time = My.Computer.Clock.TickCount - b
            Html = (responseFromServer)
        Catch ex As Exception
            Err = ex
            Time = My.Computer.Clock.TickCount - b
            Html = ""
        End Try
    End Sub

    Public ReadOnly Property Exception() As Exception
        Get
            Return Err
        End Get
    End Property
    Public ReadOnly Property TimeTook() As UInteger
        Get
            Return Time
        End Get
    End Property
    Public ReadOnly Property Bytes() As ULong
        Get
            Return BytesUsed
        End Get
    End Property
    Public ReadOnly Property GetHtml() As String
        Get
            Return Html
        End Get
    End Property
    Public ReadOnly Property IsReady() As Boolean
        Get
            Return Not thrd.IsAlive
        End Get
    End Property
    Public ReadOnly Property Url() As String
        Get
            Return _Url
        End Get
    End Property
End Class

我相信这可以正常工作

希望能帮助到你。

于 2013-08-27T10:34:50.983 回答