Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Last active August 28, 2020 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/b8468b3afb09ff1b5bd751a2fd0adb02 to your computer and use it in GitHub Desktop.
Save dj1711572002/b8468b3afb09ff1b5bd751a2fd0adb02 to your computer and use it in GitHub Desktop.
VB.NET_SerialApp7CH
'Seril Port Recieve Sample Program
'Form1 Object must create ,Button1,2 & TextBox 1-10 & ListBox1
Imports System.IO.Ports
Public Class Form1
Delegate Sub DataDelegate(ByVal sdata As String)
'=========Class内で共通に使う変数はここで宣言すること======
   Dim dataNo As Integer 
Dim dataAry(,) As Integer
Dim dcount As String
'======================================================
Private Sub PrintData(ByVal sdata As String)
Dim dataNo As Integer
Dim delimiter As String = ","
Timer1.Interval = 1
'タイマー開始
Timer1.Enabled = True
'TextBox1.text=sdata
dcount = sdata & ":" & TextBox2.Text
ListBox1.Items.Add(dcount)
Dim dStr() As String = Split(sdata, delimiter, -1, CompareMethod.Text)
If (dStr.Length > 6) Then
dataNo = CInt(TextBox2.Text) + 1
ReDim dataAry(600000, dStr.Length)
For i = 0 To dStr.Length - 1
dataAry(dataNo, i) = CInt(dStr(i))
TextBox2.Text = dataNo
TextBox3.Text = dStr.Length
TextBox4.Text = dStr(0)
TextBox5.Text = dStr(1)
TextBox6.Text = dStr(2)
TextBox7.Text = dStr(3)
TextBox8.Text = dStr(4)
TextBox9.Text = dStr(5)
TextBox10.Text = dStr(6)
' Debug.Print(CStr(dataNo) & "," & CStr(i) & "," & CStr(dataAry(dataNo, i)))
Next
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SerialPort1.PortName = TextBox1.Text 'オープンするポート名を格納
SerialPort1.Open() 'ポートオープン
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If SerialPort1.IsOpen = True Then 'ポートオープン済み
SerialPort1.Close() 'ポートクローズ
End If
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim ReceivedData As String = " " '受信データ用変数を宣言します
Try
ReceivedData = SerialPort1.ReadLine 'データを受信します
Catch ex As Exception
ReceivedData = ex.Message '例外処理を行います
End Try
'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します
Dim adre As New DataDelegate(AddressOf PrintData)
Me.Invoke(adre, ReceivedData)
End Sub
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment