-
-
Save dj1711572002/0cd77df9d085e1d57f72aa0fe6d306dd to your computer and use it in GitHub Desktop.
VB.NET SerialReceive RealTimeGraph with Bitamap Trimming
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
'Serial Port Recieve Sample Program | |
'Form1 Object must create ,button1,2 & textbox 1,2 | |
Imports System.IO.Ports | |
Public Class Form1 | |
Dim dArr(500000) As String | |
Dim arr1() As String | |
Dim rn, rn_start, rn_pause As Integer | |
Dim sflag As Integer | |
Dim sourceBitmap As New Bitmap(2000, 400) 'X軸30000data(10msec周期で300秒) Y軸3300data(電圧3300mV) | |
Dim trimmedBitmap As New Bitmap(1000, 400) 'PictuerBox1に貼りこむBitmap | |
Dim pageN As Integer | |
Dim datastr As String | |
Dim pW As Integer = 1000 | |
Dim pH As Integer = 400 | |
Dim px As Integer | |
Dim py0, py0_1 As Integer | |
Dim py1, py1_1 As Integer | |
Dim py2, py2_1 As Integer | |
Dim startX As Integer | |
'dgv初期設定 | |
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load | |
For Each sp As String In My.Computer.Ports.SerialPortNames | |
ComboBox1.Items.Add(sp) | |
Next | |
PictureBox1.Width = 1000 | |
PictureBox1.Height = 400 | |
'dgv1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells 'セル幅を自動調整 | |
dgv1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText | |
dgv1.RowHeadersWidth = 60 | |
dgv1.ColumnCount = 16 | |
dgv1.RowCount = 1 | |
'dgv1.Rows.Add("CH数", "", "COM No", " ", "BaudRate", "115200") | |
Button1.Enabled = True | |
Button2.Enabled = False | |
CheckBox1.Checked = True | |
'--------Graphic Initialize------------------------ | |
End Sub | |
Delegate Sub DataDelegate(ByVal sdata As String) | |
'================================DATA 表示============================================================== | |
Private Sub PrintData(ByVal sdata As String) | |
rn = rn + 1 'RowNumber rn | |
px = rn Mod 2000 'sourceBitmap 幅で折り返し | |
dArr(rn) = sdata | |
Dim cnmastr() As String = sdata.Split(vbCrLf) 'crlf区切りで切取り | |
If cnmastr(0).Length > 2 Then | |
datastr = cnmastr(0) | |
Else | |
datastr = cnmastr(1) | |
End If | |
If cnmastr.Length > 1 Then | |
arr1 = datastr.Split(",") 'カンマ区切りで切り取り | |
'BITMAPへのデータPLOT | |
py0_1 = py0 | |
py1_1 = py1 | |
py2_1 = py2 | |
py0 = Int(arr1(0) - 1600) * 3 + 0 | |
py1 = Int(arr1(1) - 1600) * 3 + 60 | |
py2 = Int(arr1(2) - 1600) * 3 + 180 | |
'Graphic 定義 | |
Dim g1 As Graphics = Graphics.FromImage(sourceBitmap) ' sourceBitmapへ書き込むグラフィック処理名g1 | |
Dim g2 As Graphics = Graphics.FromImage(trimmedBitmap) 'trimmedBitmapへ書き込むグラフィック処理名g1 | |
'PLOT Line | |
If px > 0 Then | |
g1.DrawLine(Pens.Blue, px, py0, px - 1, py0_1) | |
g1.DrawLine(Pens.Red, px, py1, px - 1, py1_1) | |
g1.DrawLine(Pens.LawnGreen, px, py2, px - 1, py2_1) | |
End If | |
If rn Mod 2000 = 0 Then 'rn=pWの整数倍になった時に切取りX座標変更 | |
pageN += 1 'ページ番号 | |
TextBox2.Text = pageN | |
g1.Clear(Color.Black) | |
g2.Clear(Color.Black) | |
End If | |
'開始座標の切り分け | |
If px > pW Then | |
startX = px - pW | |
Else | |
startX = 0 | |
End If | |
'切取り四角の定義 | |
Dim srcRect As New Rectangle(startX, 0, pW, 400) 'ソースから切り取る四角の定義 | |
Dim desRect As New Rectangle(0, 0, pW, 400) '切り取った四角を貼りこむ四角の定義=>今回はソースと同じサイズなので等倍コピー | |
g2.DrawImage(sourceBitmap, desRect, srcRect, GraphicsUnit.Pixel) | |
PictureBox1.Image = trimmedBitmap | |
'****g2のクリアを間欠で実施*** | |
If rn Mod 4 = 0 Then | |
g2.Clear(Color.Black) | |
End If | |
'*********************************** | |
'データ番号表示すると遅くなるでコメントアウト | |
' Dim fnt As New Font("MS UI Gothic", 12) | |
'g2.FillRectangle(Brushes.Black, 0, 0, 100, 50) | |
'g2.DrawString("data=" & CStr(rn), fnt, Brushes.Blue, 10, 0) | |
'解放 | |
g1.Dispose() | |
g2.Dispose() | |
End If | |
End Sub | |
Private Sub DirectPlot(px As Integer, py0 As Integer, py1 As Integer, py2 As Integer) | |
If PictureBox1.Image Is Nothing Then | |
PictureBox1.Image = New Bitmap(PictureBox1.Width, PictureBox1.Height) | |
End If | |
Dim g0 As Graphics = Graphics.FromImage(PictureBox1.Image) ' sourceBitmapへ書き込むグラフィック処理名g1 | |
g0.FillEllipse(Brushes.LawnGreen, px, py0, 3, 3) | |
g0.FillEllipse(Brushes.Red, px, py1, 3, 3) | |
g0.FillEllipse(Brushes.Yellow, px, py2, 3, 3) | |
g0.Dispose() | |
' PictureBox1.Image = img | |
PictureBox1.Invalidate() | |
End Sub | |
'**************************************BITMAP SAMPLE From STA ************************************************************** | |
' '============Rectangleの定義部===================================== | |
' Dim srcRect As New Rectangle(PictureBox1.Width * scrollN, sY, cWidth, 600) '切り取り部分定義 | |
' Dim desRect As New Rectangle(0, dY, srcRect.Width, srcRect.Height) | |
''============================================================= | |
'bmap2 = New Bitmap(PictureBox1.Width, PictureBox1.Height) '差し込み用Bitamap bmap2を作成 | |
'g2 = Graphics.FromImage(bmap2) 'Graphics g2で描画した結果はbmap2へ書き込まれる宣言 | |
'g2.DrawImage(bmap1, desRect, srcRect, GraphicsUnit.Pixel) 'bmap1から指定サイズで切り取ってg2:bmap2へ書き込む | |
'g2.Dispose() | |
'書き込み完了したのでg2は不要'Y軸を180度回転させて上下反転して上を北に変更 | |
'bmap2.RotateFlip(RotateFlipType.Rotate180FlipX) | |
''======PictureBox1へ差し込み実行============ | |
'PictureBox1.Image = bmap2 'bmap2を表示 | |
'***************************************************************************************************** | |
'スタートボタン押し | |
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click | |
'シリアル設定値読み込む | |
If SerialPort1.IsOpen = False Then | |
SerialPort1.PortName = ComboBox1.SelectedItem ' "COM18" ' TextBox1.Text 'オープンするポート名を格納 | |
SerialPort1.BaudRate = CInt(TextBox3.Text) '115200 'baudrate 'baudrate ボーレートを格納 | |
SerialPort1.Open() 'ポートオープン | |
Button1.Enabled = False | |
Button2.Enabled = True | |
rn_start = 1 | |
'If dgv1.Rows(0).Cells(3).Value <> " " And dgv1.Rows(0).Cells(5).Value <> " " Then | |
' Dim portName As String = "COM" + dgv1.Rows(0).Cells(3).Value 'dgv1からポート番号読み込み | |
' SerialPort1.PortName = portName ' portName オープンするポート名を格納 | |
' Dim baudrate As String = dgv1.Rows(0).Cells(5).Value 'dgv1からバーレート読み込み | |
' SerialPort1.BaudRate = 115200 'baudrate 'baudrate ボーレートを格納 | |
' SerialPort1.Open() 'ポートオープン | |
' Button1.Enabled = False | |
' Button2.Enabled = True | |
' rn_start = 1 | |
'Else | |
' TextBox1.Text = "*******Please Input COM No*******" | |
'End If | |
End If | |
End Sub | |
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click | |
If SerialPort1.IsOpen = True Then 'ポートオープン済み | |
'CheckBox1.Checked = False | |
System.Threading.Thread.Sleep(2000) '受信タスク停止するまでdelay1秒いれてからクローズする | |
SerialPort1.Close() | |
dgv1.CurrentCell = dgv1(0, dgv1.Rows.Count - 1) '最下行にカーソルを維持 | |
dgv1.Rows.Clear() | |
rn = 0 | |
dgv1.RowHeadersWidth = 60 | |
dgv1.ColumnCount = 16 | |
dgv1.RowCount = 1 | |
dgv1.Rows.Add("CH1", "CH2", "CH3", "CH4", " CH5", "CH6") | |
End If | |
Button1.Enabled = True | |
Button2.Enabled = False | |
End Sub | |
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived | |
Dim ReceivedData As String = " " '受信データ用変数を宣言します | |
If CheckBox1.Checked = True Then | |
Try | |
ReceivedData = SerialPort1.ReadLine 'データを受信します | |
Catch ex As Exception | |
ReceivedData = ex.Message '例外処理を行います | |
End Try | |
If ReceivedData.Substring(0, 1) = Chr(13) Then | |
'SerialPort1.DiscardInBuffer() | |
'Invokeメソッドにより実行されるメソッドへのデリゲートの宣言を行い、受信データを表示します | |
Dim adre As New DataDelegate(AddressOf PrintData) | |
Me.Invoke(adre, ReceivedData) | |
End If | |
End If | |
End Sub | |
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click | |
End Sub | |
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged | |
PictureBox1.Visible = False | |
dgv1.Visible = True | |
End Sub | |
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged | |
PictureBox1.Visible = True | |
dgv1.Visible = False | |
End Sub | |
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged | |
If CheckBox1.Checked = True And SerialPort1.IsOpen = True Then | |
TextBox1.Text = "****Sampling****" | |
rn_start = rn | |
ElseIf CheckBox1.Checked = False And SerialPort1.IsOpen = True Then | |
TextBox1.Text = "****Paused****" | |
Dim i As Integer | |
For i = rn_start To rn | |
'Dim cnmastr() As String = dArr.Split(vbCrLf) | |
'If cnmastr.Length > 1 Then | |
Dim arr1() As String = dArr(i).Split(",") | |
TextBox1.Text = i.ToString | |
dgv1.Rows.Add(arr1) | |
dgv1.Rows(i).HeaderCell.Value = i.ToString() '行ヘッダーにデータ番号rn | |
dgv1.FirstDisplayedScrollingRowIndex = dgv1.Rows.Count - 1 '最新データの最下行を表示 | |
'End If | |
Next | |
End If | |
End Sub | |
Private Sub dgv1_MouseClick(sender As Object, e As MouseEventArgs) Handles dgv1.MouseClick | |
If dgv1.CurrentCell.RowIndex = 0 And dgv1.CurrentCell.ColumnIndex = 3 Then | |
TextBox1.Text = "" | |
End If | |
End Sub | |
End Class | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment