|
-
Jan 12th, 2010, 03:26 AM
#24
Thread Starter
Lively Member
Re: [RESOLVED] Converting voltage level into graphical display
Following is the code of PictureBox2, to display the waveform.
Code:
Private Sub PictureBox2_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox2.Paint
Dim g As Graphics = e.Graphics
Dim vertFont As New Font("Verdana", 10, FontStyle.Bold)
Dim horzFont As New Font("Verdana", 10, FontStyle.Bold)
Dim vertBrush As New SolidBrush(Color.Blue)
Dim horzBrush As New SolidBrush(Color.Blue)
Dim horzBrush1 As New SolidBrush(Color.Blue)
Dim BluePen As New Pen(Color.Blue)
' Drawing a vertical and a horizontal line
g.DrawLine(BluePen, 50, 225, 50, 15) 'vertical line
g.DrawLine(BluePen, 50, 120, 260, 120) 'horizontal line
'Details of the graph layout
Dim Origin As New Point(50, 119)
Dim xAxisLength As Integer = 210
Dim yAxisLength As Integer = -120
If voltsQ.Count > 2 Then
'Turn the queue into an array:
Dim voltsQarray As Single() = voltsQ.ToArray
'Define an array of data points:
Dim DataPoints(voltsQ.Count - 1) As Point
Dim x, y As Integer
'Fill in the array using the voltage readings:
For i As Integer = 0 To voltsQ.Count - 1
'Get the volts value from the queue:
Dim v As Single = voltsQarray(i)
'Find the distance along the x axis:
x = Origin.X + CInt(i * xAxisLength / 40)
'find the height of the data point on the Y axis:
y = Origin.Y + CInt(v * yAxisLength / maxVolts)
'Put the point in the array
DataPoints(i) = New Point(x, y)
Next
'Draw the waveform:
e.Graphics.DrawCurve(Pens.Green, DataPoints)
End If
'Draw vertical strings
Dim vertStrFormat As New StringFormat()
vertStrFormat.FormatFlags = StringFormatFlags.DirectionVertical
g.DrawString("-", horzFont, horzBrush, 49, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 49, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 59, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 69, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 79, 113, vertStrFormat)
g.DrawString("--", horzFont, horzBrush, 89, 106, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 99, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 109, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 119, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 129, 113, vertStrFormat)
g.DrawString("--", horzFont, horzBrush, 139, 106, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 149, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 159, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 169, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 179, 113, vertStrFormat)
g.DrawString("--", horzFont, horzBrush, 189, 106, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 199, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 209, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 219, 113, vertStrFormat)
g.DrawString("-", horzFont, horzBrush, 229, 113, vertStrFormat)
g.DrawString("--", horzFont, horzBrush, 239, 106, vertStrFormat)
' y-axis drawing
g.DrawString(" 4 --", vertFont, vertBrush, 25, 12)
g.DrawString(" --", vertFont, vertBrush, 25, 12)
g.DrawString(" -", vertFont, vertBrush, 25, 17)
g.DrawString(" -", vertFont, vertBrush, 25, 22)
g.DrawString(" -", vertFont, vertBrush, 25, 27)
g.DrawString(" -", vertFont, vertBrush, 25, 32)
g.DrawString(" 3 --", vertFont, vertBrush, 25, 37)
g.DrawString(" --", vertFont, vertBrush, 25, 37)
g.DrawString(" -", vertFont, vertBrush, 25, 42)
g.DrawString(" -", vertFont, vertBrush, 25, 47)
g.DrawString(" -", vertFont, vertBrush, 25, 52)
g.DrawString(" -", vertFont, vertBrush, 25, 57)
g.DrawString(" 2 --", vertFont, vertBrush, 25, 62)
g.DrawString(" --", vertFont, vertBrush, 25, 62)
g.DrawString(" -", vertFont, vertBrush, 25, 67)
g.DrawString(" -", vertFont, vertBrush, 25, 72)
g.DrawString(" -", vertFont, vertBrush, 25, 77)
g.DrawString(" -", vertFont, vertBrush, 25, 82)
g.DrawString(" 1 --", vertFont, vertBrush, 25, 87)
g.DrawString(" --", vertFont, vertBrush, 25, 87)
g.DrawString(" -", vertFont, vertBrush, 25, 92)
g.DrawString(" -", vertFont, vertBrush, 25, 97)
g.DrawString(" -", vertFont, vertBrush, 25, 102)
g.DrawString(" -", vertFont, vertBrush, 25, 107)
g.DrawString(" 0", vertFont, vertBrush, 25, 112)
g.DrawString(" -", vertFont, vertBrush, 25, 117)
g.DrawString(" -", vertFont, vertBrush, 25, 122)
g.DrawString(" -", vertFont, vertBrush, 25, 127)
g.DrawString(" -", vertFont, vertBrush, 25, 132)
g.DrawString(" --", vertFont, vertBrush, 25, 137)
g.DrawString(" -1--", vertFont, vertBrush, 25, 137)
g.DrawString(" -", vertFont, vertBrush, 25, 142)
g.DrawString(" -", vertFont, vertBrush, 25, 147)
g.DrawString(" -", vertFont, vertBrush, 25, 152)
g.DrawString(" -", vertFont, vertBrush, 25, 157)
g.DrawString(" --", vertFont, vertBrush, 25, 162)
g.DrawString(" -2--", vertFont, vertBrush, 25, 162)
g.DrawString(" -", vertFont, vertBrush, 25, 167)
g.DrawString(" -", vertFont, vertBrush, 25, 172)
g.DrawString(" -", vertFont, vertBrush, 25, 177)
g.DrawString(" -", vertFont, vertBrush, 25, 182)
g.DrawString(" --", vertFont, vertBrush, 25, 187)
g.DrawString(" -3--", vertFont, vertBrush, 25, 187)
g.DrawString(" -", vertFont, vertBrush, 25, 192)
g.DrawString(" -", vertFont, vertBrush, 25, 197)
g.DrawString(" -", vertFont, vertBrush, 25, 202)
g.DrawString(" -", vertFont, vertBrush, 25, 207)
g.DrawString(" --", vertFont, vertBrush, 25, 212)
g.DrawString(" -4--", vertFont, vertBrush, 25, 212)
' Dispose of objects
vertFont.Dispose()
horzFont.Dispose()
vertBrush.Dispose()
horzBrush.Dispose()
BluePen.Dispose()
'Display text in Y-axis
Dim Font1 As New Font("Comic Sans MS", 10, FontStyle.Bold)
g.TranslateTransform(30, 148)
g.RotateTransform(180)
g.DrawString("Volts (V)", Font1, _
Brushes.Blue, 0, 0, New StringFormat(StringFormatFlags.DirectionVertical))
End Sub
Following is the coding for the timer.
I included this line, Single.TryParse(TextBoxVoltage.Text, volts1), so that the waveform will move according to the voltage shown in the "Voltage Level in Volts(V)", and not follow the average voltage(which will produce the straight line).
Code:
Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
'read the voltage and get value from RS232 interface
Call OpenPORT1()
Single.TryParse(TextBoxVoltage.Text, volts1)
'add the new reading to the end of the queue
voltsQ.Enqueue(volts1)
'remove the old reading at the head of the queue, to keep max. 40 data points
If voltsQ.Count > 40 Then voltsQ.Dequeue()
'update the display with the new data
Me.Refresh()
End Sub
As for Call OpenPORT1(),
Code:
Private Sub OpenPORT1()
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = 9600
SerialPort1.Parity = System.IO.Ports.Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = System.IO.Ports.StopBits.One
SerialPort1.RtsEnable = True
SerialPort1.Open()
End Sub
I hope everything is correct.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|