如何从串口将数据绘制到用户控件中?

时间:2013-10-01 09:37:00

标签: vb.net

我正在尝试从串口中提取数据,然后以图形的形式显示它。我该怎么做?请帮帮我(我创建了一个用户控件顺便说一句)

这是用户控制下的GUI代码:

Public Class ArtificialHorizon

Private g As Graphics

Private _roll_angle As Double
Public Property roll_angle() As Double
    Get
        Return _roll_angle
    End Get
    Set(ByVal value As Double)
        _roll_angle = value
        Invalidate()
    End Set
End Property
Private _pitch_angle As Double
Public Property pitch_angle() As Double
    Get
        Return _pitch_angle
    End Get
    Set(ByVal value As Double)
        _pitch_angle = value
        Invalidate()
    End Set
End Property



Private Function pitch_to_pix(ByVal pitch As Double) As Integer
    Return pitch / 35.0 * Me.Height / 2
    'Return pitch / 45.0 * Me.Height / 2
End Function

Private Sub ArtificialHorizon_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    g = e.Graphics
    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality

    g.Clear(Me.BackColor)
    Dim sin As Double = Math.Sin(roll_angle / 180 * 3.14)

    g.ResetTransform()
    '        g.FillRegion(Brushes.White, New Region(New Rectangle(0, 0, Me.Width, Me.Height)))

    ' rounded rectangle
    Dim path As New Drawing2D.GraphicsPath()
    Dim r As Single = 50

    path.AddArc(0, 0, r, r, 180, 90)
    path.AddArc(Me.Width - r, 0, r, r, 270, 90)
    path.AddArc(Me.Width - r, Me.Height - r, r, r, 0, 90)
    path.AddArc(0, Me.Height - r, r, r, 90, 90)
    'path.AddEllipse(0, 0, Me.Width, Me.Height)
    path.CloseFigure()
    g.SetClip(path)

    g.TranslateTransform(Me.Width / 2, Me.Height / 2)

    g.RotateTransform(roll_angle)
    g.TranslateTransform(0, pitch_to_pix(pitch_angle))

    ' chocolate
    Dim b As New System.Drawing.Drawing2D.LinearGradientBrush(New RectangleF(-Me.Width, 0, Me.Height * 2, Me.Width * 2), Color.FromArgb(255, 219, 140, 21), Color.Brown, Drawing2D.LinearGradientMode.Vertical)
    g.FillRectangle(b, New RectangleF(-Me.Width * 2, +1, Me.Height * 4, Me.Width * 4))

    g.RotateTransform(180)

    ' color.aqua
    b = New System.Drawing.Drawing2D.LinearGradientBrush(New RectangleF(-Me.Width, -1, Me.Height * 2, Me.Width * 2), Color.FromArgb(255, 28, 134, 186), Color.DarkBlue, Drawing2D.LinearGradientMode.Vertical)
    g.FillRectangle(b, New RectangleF(-Me.Width * 2, 0, Me.Height * 4, Me.Width * 4))




    g.ResetTransform()
    Dim w2 As Single = Me.Width / 2
    Dim s As Single = Me.Width / 38
    g.TranslateTransform(Me.Width / 2, Me.Height / 2)
    g.RotateTransform(45)
    g.TranslateTransform(-w2 + s, 0)
    g.DrawLine(New Pen(Color.White, 2), 0, 0, s * 2, 0)
    g.TranslateTransform(+w2 - s, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 3, 0)
    'g.DrawString("0°", New System.Drawing.Font("sans-serif", 9), Brushes.White, -w2 + 40, -4)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 2, 0)
    g.RotateTransform(15)
    g.DrawLine(New Pen(Color.White, 2), -w2 + s, 0, -w2 + s * 3, 0)
    'g.DrawString("+45°", New System.Drawing.Font("sans-serif", 9), Brushes.White, -w2 + 40, -4)


    g.ResetTransform()

    Dim length As Single = Me.Width / 4
    Dim notch As Single = Me.Width / 30
    g.TranslateTransform(Me.Width / 2, Me.Height / 2)
    g.DrawLine(New Pen(Color.White, 3), -length + notch * 2, 0, -notch, 0)
    g.DrawLine(New Pen(Color.White, 3), notch, 0, length - notch * 2, 0)
    g.DrawArc(New Pen(Color.White, 3), -notch, -notch, notch * 2, notch * 2, 180, -180)

    g.ResetTransform()

    ' driehoekje
    Dim ww As Single = Me.Width / 38
    g.TranslateTransform(Me.Width / 2, Me.Height / 2)
    g.RotateTransform(-90 + roll_angle)
    path = New Drawing2D.GraphicsPath()
    path.AddLine(w2 - ww * 3, 0, w2 - ww * 4, ww)
    path.AddLine(w2 - ww * 4, -ww, w2 - ww * 4, ww)
    path.AddLine(w2 - ww * 4, -ww, w2 - ww * 3, 0)
    g.FillRegion(Brushes.White, New Region(path))
    g.DrawLine(New Pen(Color.White, 1), w2 - ww * 3, 0, w2 - ww * 4, ww)
    g.DrawLine(New Pen(Color.White, 1), w2 - ww * 4, -ww, w2 - ww * 4, ww)
    g.DrawLine(New Pen(Color.White, 1), w2 - ww * 4, -ww, w2 - ww * 3, 0)



    g.ResetTransform()
    g.ResetClip()
    path = New Drawing2D.GraphicsPath()
    path.AddPie(New Rectangle(ww * 3, ww * 3, Me.Width - ww * 6, Me.Height - ww * 6), 0, 360)
    g.SetClip(path)

    g.TranslateTransform(Me.Width / 2, Me.Height / 2)
    g.RotateTransform(roll_angle)
    g.TranslateTransform(0, pitch_to_pix(pitch_angle))
    For i As Integer = -80 To 80 Step 10
        drawpitchline(g, i)
    Next i

End Sub

Private Sub drawpitchline(ByVal g As Graphics, ByVal pitch As Double)
    Dim w As Single = Me.Width / 8
    g.DrawLine(Pens.White, -w, pitch_to_pix(-pitch + 5), w, pitch_to_pix(-pitch + 5))
    g.DrawLine(Pens.White, -w * 5 / 3, pitch_to_pix(-pitch), w * 5 / 3, pitch_to_pix(-pitch))
    g.DrawString(pitch, Me.Font, Brushes.White, -w * 75 / 30, pitch_to_pix(-pitch) - 5)
    g.DrawString(pitch, Me.Font, Brushes.White, w * 2, pitch_to_pix(-pitch) - 5)
End Sub

Private Sub drawrollline(ByVal g As Graphics, ByVal a As Single)
    Dim w2 As Single = Me.Width / 2


    g.RotateTransform(a + 90)
    g.TranslateTransform(-w2 + 10, 0)
    g.DrawLine(Pens.White, 0, 0, 20, 0)
    g.TranslateTransform(10, 5)
    g.RotateTransform(-a - 90)
    g.DrawString("" & (a) & "°", New System.Drawing.Font("sans-serif", 9), Brushes.White, 0, 0)
    g.RotateTransform(+90 + a)
    g.TranslateTransform(-10, -5)
    g.TranslateTransform(+w2 - 10, 0)
    g.RotateTransform(-a - 90)
End Sub
End Class

这是我在表单事件下放置的visual basic和arduino之间串口通信的代码。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' Dim WithEvents SerialPort1 As New IO.Ports.SerialPort
    'Delegate Sub myMethodDelegate(ByVal [text] As String)
    'Dim myDelegate As New myMethodDelegate(AddressOf ShowString)

    Dim BaudRates() As String = {"300", "1200", "2400", "4800", "9600", "14400", "19200", "28800", "38400", "57600", "115200"}
    cmbxBaud.Items.AddRange(BaudRates)
    cmbxBaud.SelectedIndex = 4


    Try
        'Get all the port available from the system
        GetSerialPortNames()
        cmbxPort.SelectedIndex = 1
    Catch
        MsgBox("No ports connected.")
    End Try

End Sub

'Sub ShowString(ByVal myString As String)

'If ckbxAutoScroll.Checked = True Then
' txtbxIn.SelectionStart = Len(txtbxIn.Text)
'txtbxIn.ScrollToCaret()
' End If

'txtbxIn.AppendText(myString)
' End Sub

Private Sub GetSerialPortNames()
    For Each SerialPort1ort As String In My.Computer.Ports.SerialPortNames
        cmbxPort.Items.Add(SerialPort1ort)
    Next
End Sub



Private Sub btnConnect_Click(sender As Object, e As EventArgs) Handles btnConnect.Click


    If SerialPort1.IsOpen Then
        SerialPort1.Close()
    End If

    With SerialPort1
        'Setup to communicate with Arduino Pro Mini
        .PortName = cmbxPort.SelectedItem.ToString
        .BaudRate = cmbxBaud.SelectedItem.ToString
        .Parity = IO.Ports.Parity.None
        .DataBits = 8
        .StopBits = IO.Ports.StopBits.One
    End With

    SerialPort1.Open()

    btnConnect.Visible = False
    cmbxPort.Enabled = False
    cmbxBaud.Enabled = False
    btnDisconnect.Visible = True

End Sub

Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
    Try

        btnConnect.Visible = True
        btnDisconnect.Visible = False
        cmbxPort.Enabled = True
        cmbxBaud.Enabled = True
        SerialPort1.Close()

    Catch
        MessageBox.Show("Some kind of problem.")
    End Try
End Sub


' Private Sub SerialPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
'Dim str As String = SerialPort1.ReadExisting()
'BeginInvoke(myDelegate, str)
'End Sub

Private Sub ArtificialHorizon1_Load(sender As Object, e As EventArgs)

End Sub
End Class

0 个答案:

没有答案