Freigeben über


VB, .Net Gadgeteer, .Net Micro Framework SDK 4.2 и простой веб-сервер

Грэг Дункан

Сегодняшний пост от PepLluis очень короток, но я просто отдаю VB.Net свои симпатии здесь, поэтому…

Веб–сервер для VBGadgeteerWebserver , SDK 4.2 QFE2 и библиотека SpiderPremium

Этот маленький пример кода показывает, как создать веб-сервер для нашего базового устройства Gadgeteer, используя VB с библиотеками Spider и 4.2 RTM Premium.

Запустите Visual Studio с новым проектом gadgeteer и скопируйте в него фрагмент…Это все! (не забудьте добавить ссылки на библиотеки).

 Imports System.Net

Imports System.Text

Imports Microsoft.SPOT.Hardware

Imports GHI.Premium.Net

Imports GT = Gadgeteer

Partial Public Class Program

    Private WithEvents ethernet As New EthernetBuiltIn()

' 

    Private ip As New IPAddress(New Byte() {0, 0, 0, 0})

    Private WithEvents myweb As WebEvent

'

    Private WithEvents timer As GT.Timer = New GT.Timer(1000)
    
    Public Sub ProgramStarted()

        'Initialize adapter
        
        ethernet.Open()

        ethernet.NetworkInterface.EnableDhcp()
        
        ethernet.NetworkInterface.EnableDynamicDns()

        NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet)

        'app timer on

        timer.Start()

    End Sub

    Private Sub ethernet_CableConnectivityChanged(sender As Object, e As GHI.Premium.Net.EthernetBuiltIn.CableConnectivityEventArgs) Handles ethernet.CableConnectivityChanged
    
        If e.IsConnected Then

            ' reboot device after plug... to startup again

            PowerState.RebootDevice(True)

        End If
    
    End Sub

    Private Sub ReceivedWebEventHandler(path As String, method As WebServer.HttpMethod, responder As Responder) Handles myweb.WebEventReceived

    ' webb response

        responder.Respond(New System.Text.UTF8Encoding().GetBytes("Hello from " + ip.ToString() + " At " + System.DateTime.Now.ToString()), "text/html")

    End Sub

    Private tic As Boolean

    Private Sub timer_Tick(timer As Gadgeteer.Timer) Handles timer.Tick

        ' blink debug led... system working

        tic = Not tic

        Mainboard.SetDebugLED(tic)
    
        ' get an IP from DHCP if no previous one

        If (ip.ToString = "0.0.0.0") And ethernet.IsCableConnected Then

            ip = IPAddress.GetDefaultLocalAddress()

            WebServer.StartLocalServer(ip.ToString(), 80)

            myweb = WebServer.SetupWebEvent("Hello")

         End If

    End Sub

End Class

...