Created
August 31, 2020 07:16
-
-
Save dj1711572002/f196dce54c1d80cc931dbd7b351c9a43 to your computer and use it in GitHub Desktop.
VB.NET_Serial_GraphicPlot_RealTimeDataMOnitor_6CH_7msecPeriod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'Seril Port Recieve Sample Program | |
'Form1 Object must create ,button1,2 & textbox 1,2 | |
Imports System.Drawing | |
Imports System.IO.Ports | |
Public Class Form1 | |
Dim onetime As Integer = 1 | |
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 | |
Dim sw As New System.Diagnostics.Stopwatch() | |
Dim tp As TimeSpan | |
Dim timestamp As Integer | |
Dim stime As Integer 'plot start sampling time | |
Dim etime As Integer 'plot end sampling time | |
Dim totalsec As Double | |
'============================================================= | |
Private Sub PrintData(ByVal sdata As String) | |
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) | |
'Debug.Print("timestamp=" & CStr(timestamp)) | |
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)) | |
timestamp = dataAry(dataNo, 6) | |
'=================PLOT SUBへ================= | |
If i < 6 Then | |
plotC(dataNo, dataAry(dataNo, i), i + 1) 'plotC(x,y,colorN) | |
End If | |
'=========================================== | |
'' | |
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 MovAve(ByRef dA(,) As Long, ByVal colN As Integer, ByVal MA As Integer, ByVal rowN As Integer) As Double() | |
Dim i, j As Integer | |
Dim dataSum() As Long | |
Dim dSum As Long | |
Dim dataA() As Double '= {0, 0, 0, 0, 0, 0, 0} | |
'Debug.Print("====colN=" & CStr(colN) & "MA=" & CStr(MA) & "rowN=" & CStr(rowN)) | |
ReDim dataSum(colN + 1) | |
ReDim dataA(colN + 1) | |
For j = 0 To colN - 1 | |
dSum = 0 | |
For i = rowN - MA To rowN | |
dSum = dSum + dA(i, j) | |
'Debug.Print("dA(" & CStr(i) & "," & CStr(j) & ")=" & CStr(dA(i, j)) & CStr(dataSum(j))) | |
Next | |
dataA(j) = CDbl(dSum / MA) | |
Next | |
MovAve = dataA | |
End Function | |
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click | |
Dim canvas As New Bitmap(PictureBox1.Width, PictureBox1.Height) | |
'ImageオブジェクトのGraphicsオブジェクトを作成する | |
Dim g As Graphics = Graphics.FromImage(canvas) | |
'位置(10, 20)に100x80の四角を赤色で描く | |
g.DrawRectangle(Pens.Red, 10, 20, 100, 80) | |
'先に描いた四角に内接する楕円を黒で描く | |
g.FillEllipse(Brushes.Black, 100, 100, 5, 5) | |
'リソースを解放する | |
g.Dispose() | |
'PictureBox1に表示する | |
PictureBox1.Image = canvas | |
End Sub | |
Sub plotC(ByVal dataNo As Integer, ByVal value As Integer, ByVal colorN As Integer) | |
If PictureBox1.Image Is Nothing Then '初回だけBITMAPを定義する Picture1.imageという名称をつかうこと | |
PictureBox1.Image = New Bitmap(600, 240) | |
End If | |
Dim g As Graphics = Graphics.FromImage(PictureBox1.Image) | |
Dim px As Integer | |
Dim py As Integer | |
'======================COLORS========================================= | |
Dim b As Brush | |
Select Case colorN | |
Case 1 | |
b = Brushes.Red | |
Case 2 | |
b = Brushes.Blue | |
Case 3 | |
b = Brushes.Green | |
Case 4 | |
b = Brushes.Magenta | |
Case 5 | |
b = Brushes.Orange | |
Case 6 | |
b = Brushes.Black | |
End Select | |
'===========-PLOT パラメータの定義 手計算で処理======================== | |
'Dim yh As Integer = 240 | |
'Dim xw As Integer = 600 | |
'Dim Ymax As Integer = 7000 | |
'Dim Ymin As Integer = 0 | |
'Dim Xmax As Integer = 600 | |
'Dim Xmin As Integer = 0 | |
'Dim dstep As Double | |
'Dim dvalue As Double | |
'-------------ドット変換値は手計算する------- | |
'dstep = xw / (Xmax - Xmin) | |
'=600/(600-0)=1 | |
'dvalue = yh / (Ymax - Ymin) | |
'=240/(7000)=0.0342857 | |
'=================================================================== | |
If dataNo Mod 600 = 0 Then '1画面終了したらimageクリア | |
tp = sw.Elapsed | |
totalsec = tp.TotalSeconds | |
'Debug.Print("tp.sec=" & totalsec) | |
sw.Stop() | |
If totalsec > 0.6 Then | |
TextBox11.Text = CStr(totalsec) | |
End If | |
sw.Reset() | |
sw.Start() | |
PictureBox1.Image = Nothing | |
Else | |
px = Int(1 * dataNo Mod 600) | |
py = Int(0.0342857 * value) | |
'Debug.Print("px=" & CStr(px) & "py=" & CStr(py)) | |
g.FillEllipse(b, px, py, 3, 3) | |
g.Dispose() | |
PictureBox1.Invalidate() | |
End If | |
End Sub | |
End Class | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment