Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Created August 28, 2020 08:25
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/e0dfc4cd23a32f7327bc07eb603bb344 to your computer and use it in GitHub Desktop.
Save dj1711572002/e0dfc4cd23a32f7327bc07eb603bb344 to your computer and use it in GitHub Desktop.
VBNET_StatTest00_SerialSample_DataCalc00
'Seril Port Recieve Sample Program
'Form1 Object must create ,button1,2 & textbox 1,2 
Imports System.IO.Ports
Public Class Form1
Delegate Sub DataDelegate(ByVal sdata As String)
'=========Available Parameters in thie Calss=================
Dim dataAry(600000, 7) As Long
Dim dataNo As Long
Dim Mave() As Double
'=============================================================
Private Sub PrintData(ByVal sdata As String)
'Dim dataAverage As Double
Dim delimiter As String = ","
Dim i As Integer
Dim dcount As String
Timer1.Interval = 1
'タイマー開始
Timer1.Enabled = True
'TextBox1.text=sdata
dcount = sdata & ":" & TextBox2.Text
ListBox1.Items.Add(dcount)
'Debug.Print("sdata=" & sdata)
Dim dStr() As String = Split(sdata, delimiter, -1, CompareMethod.Text)
Dim colN As Integer
colN = 7
ReDim Mave(colN)
If (dStr.Length = 7) Then
dataNo += 1
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("dataAry(" & CStr(dataNo) & "," & CStr(i) & ")=" & CStr(dataAry(dataNo, i)))
If dataNo > 100 Then
'Fuction MoveAve(dataArray(,) as long ,ColumnNo,MA,rowNo)
Mave = MovAve(dataAry, 7, 10, dataNo)
End If
'For i = 0 To colN - 1
' Debug.Print("Mave(" & CStr(i) & ")=" & CStr(Mave(i)))
'Next
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
'Function Ave_2d(ByRef dA(,) As Integer, ByVal colN As Integer, ByVal startN As Integer, ByVal endN As Integer) As Double
' Dim i, j As Integer
' Dim dataSum As Integer
' Dim dataAverage As Double
' For i = startN To endN
' For j = 0 To colN - 1
' ' dataSum = dataSum + dA(i, colN)
' ' Debug.Print("dA(" & CStr(i) & "," & CStr(colN) & ")=" & CStr(dA(i, colN)))
' Debug.Print("dA(" & CStr(i) & "," & CStr(j) & ")=" & CStr(dA(i, j)))
' Next
' Next
' dataAverage = dataSum / (endN - startN + 1)
' Ave_2d = dA(endN, colN)
'End Function
End Class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment