Imports System.IO
Imports System.Net
Imports System.Net.Http
Imports System.Text
Imports System.Globalization
Public Class LEDNDSample
Private digit As Integer = 7
Private Sub BtnDisp_Click(sender As Object, e As EventArgs) Handles BtnDisp.Click
Dim a() As String = {Chr(&H1F), Chr(&H1), "1", "2", "3", "4", "5", "6", "7"}
Dim disp As String = ""
Dim dg As Integer
If RadioUSB.Checked Then
For dg = 0 To digit + 1
SendData(a(dg))
Next
ElseIf RadioWiFi.Checked Then
For dg = 0 To digit + 1
disp += a(dg)
Next
SendData(disp)
ElseIf RadioLAN.Checked Then
For dg = 0 To digit + 1
disp += a(dg)
Next
SendData(disp)
End If
End Sub
Private Sub BtnClear_Click(sender As Object, e As EventArgs) Handles BtnClear.Click
SendData("")
End Sub
Private Sub SendData(ByVal msg As String)
Dim ret As Integer = 0
Dim sendData As String = ""
Dim dispMethod As String = ""
If RadioUSB.Checked Then
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 115200
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.Handshake = Ports.Handshake.RequestToSend
If msg = "" Then
sendData = Chr(&HC)
Else
sendData = msg
End If
SerialPort1.Open()
SerialPort1.Write(sendData)
SerialPort1.Close()
ElseIf RadioWiFi.Checked Then
Dim url As String = "http://192.168.1.250/disp/"
If msg = "" Then
sendData = url + "%0C"
Else
sendData = url + msg
End If
SendByHttp(sendData)
ElseIf RadioLAN.Checked Then
Dim url As String = "http://192.168.1.250"
If msg = "" Then
sendData = url + "/disp/?d=%0C"
Else
sendData = url + "/disp/?d=" + msg
End If
Dim resText As String
Try
Dim enc As Encoding = Encoding.UTF8
Dim webC As New System.Net.WebClient
webC.Encoding = enc
Dim nameC As New System.Collections.Specialized.NameValueCollection
Dim resData As Byte() = {}
Try
resData = webC.UploadValues(sendData, nameC)
Catch ex As ArgumentException
MsgBox(ex.Message)
Catch ex As WebException
MsgBox(ex.Message)
Finally
webC.Dispose()
End Try
Try
resText = System.Text.Encoding.UTF8.GetString(resData)
Catch ex As ArgumentNullException
Console.WriteLine("エラー")
End Try
Catch ex As Exception
MsgBox("HTTP通信エラー", MsgBoxStyle.Critical)
End Try
End If
End Sub
Private Async Sub SendByHttp(ByVal url As String)
Dim sendUrl As Uri = New Uri(url)
Dim html As String = ""
Using httpcl As New HttpClient()
html = Await httpcl.GetStringAsync(sendUrl)
End Using
End Sub
End Class
|