NOTE: I cannot graph my data

advertisement
NOTE: I cannot graph my data. I am able to read the data.
Imports
Imports
Imports
Imports
Imports
System.Math
System.Drawing
System.IO
System.Globalization
Microsoft.VisualBasic
Public Class Form1
' Create a new serial port
Dim WithEvents SerialPort1 As New IO.Ports.SerialPort
' Create a new picture box
Private pictureBox1 As New PictureBox()
' Define Global variables
Dim x1 As Integer
Dim x2 As Integer
Dim y1 As Integer
Dim y2 As Integer
channel 1)
Dim y11 As Integer
Dim Offset As Integer
Dim Offset2 As Integer
'Dim BadCount As Integer
Dim HighByteFlag As Integer
low byte
Dim ADData As Integer
10 bit output
Dim Chan2Flag As Integer
channel 1 or channel 2
Dim Gain As Single
amplitude)
Dim Freq As Single
'graph
'graph
'graph
'graph
X1
X2
Y1
Y2
point
point
point
point (previous point for
'graph previous point for channel 2
'offset for Y axis for channel 1
'offset for Y axis for channel 2
'number of bad inputs from serial port
'flag to tell if input is high byte or
'sum of the byte and low byte to form
'flag to tell if data bytes if from
'vertical gain control (i.e. signal
'horizontal gain control
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
' Configure Serial Port
SerialPort1.PortName = "COM2"
'using COM2
SerialPort1.BaudRate = 4800
SerialPort1.Parity = IO.Ports.Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = IO.Ports.StopBits.One
SerialPort1.DtrEnable = True
SerialPort1.RtsEnable = True
'SerialPort1.ReceivedBytesThreshold = 1
SerialPort1.Close()
'Label4.Text = ""
ascii of each input
' Initialize Graph Settings
Offset = 2100
Offset2 = 3000
'this is used to display
x1 = 1
x2 = 1
y1 = Offset
y11 = Offset2
y2 = Offset
Gain = 2
Freq = 5
'Initialize Variables
'BadCount = 0
ADData = 0
HighByteFlag = 1
'note that high byte, not low byte comes
first
Chan2Flag = 1
'note that channel 2, not channel 1, comes
first
'Disable 'Down' Gain and Freqency at Start (and anytime Gain =
default)
Button4.Enabled = False
Button5.Enabled = False
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
' Open SerialPort1
SerialPort1.Open()
' Declare Local Variables
Dim q As String
Dim i As Integer
' Bombard DATAQ with initialization commands
For i = 1 To 100
q = vbNullString + "NZ"
SerialPort1.Write(q)
q = vbNullString + "S1"
SerialPort1.Write(q)
q = vbNullString + "C3"
SerialPort1.Write(q)
Next
End Sub
' Plot data when 'Plot Data' button is pressed
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
' Make a new picture box each time the button is clicked
pictureBox1 = New PictureBox()
' Set the location and size of the PictureBox control.
Me.pictureBox1.Location = New System.Drawing.Point(15, 70)
Me.pictureBox1.Size = New System.Drawing.Size(700, 350)
Me.pictureBox1.TabStop = False
' Set the SizeMode property to the StretchImage value.
will shrink or enlarge the image
This
' as needed to fit into the PictureBox.
Me.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
' Set the border style to a three-dimensional border.
Me.pictureBox1.BorderStyle = BorderStyle.Fixed3D
'Set the background color to white
Me.pictureBox1.BackColor = Color.White
' Connect the Paint event of the PictureBox to the event
handler method.
AddHandler pictureBox1.Paint, AddressOf Me.pictureBox1_Paint
' Add the PictureBox control to the Form.
Me.Controls.Add(pictureBox1)
End Sub
' Code for actually plotting data
Private Sub pictureBox1_Paint(ByVal sender As Object, ByVal e As
System.Windows.Forms.PaintEventArgs)
' Create a local version of the graphics object for the
PictureBox
Dim g As Graphics = e.Graphics
If x2 > pictureBox1.Width Then
'test if end of
screen
x2 = Freq
x1 = 1
pictureBox1.Image = Nothing
End If
'clear PictureBox1
g.DrawLine(System.Drawing.Pens.Blue, x1, y1, x2, y2)
g.DrawLine(System.Drawing.Pens.Red, x1, y11, x2, y2)
End Sub
' This sub-routine is entered when data is available
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal
e As System.IO.Ports.SerialDataReceivedEventArgs) Handles
SerialPort1.DataReceived
' Declare Local Variables
Dim s As String
Dim n As Long
' Set up graph
'Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
'create Bitmap that is same size as PictureBox1
'Dim gr As Graphics = Graphics.FromImage(bmp)
'assign graphics object to Bitmap (graphics object is like a drawing
canvas)
' Load Available Data
s = SerialPort1.ReadByte()
byte from input buffer
n = Asc(s)
'On Error GoTo xx
data cannot be converted to ascii
'Label4.Text = Label4.Text + Str(n)
in Label4
'synchronously read one
'convert to ascii
'set error trap if
'display data as ascii
' Collect Data for Channel 1
If Chan2Flag = 0 Then
If HighByteFlag = 0 Then
'test if input data is
low byte
ADData = n
'if yes, add it to
ADData
ADData = ADData \ 16
'eliminate lower four
bits (see raw data input byte definitions)
HighByteFlag = 1
'look next for high
byte
Else
n = n \ 2
'this high byte and
shift left 4 bits
n = n * 8
ADData = ADData + n
'add to ADData for
complete 10 bit data. 10 bit mask is not necessary
y2 = Offset - Gain * ADData
data so it can be graphed
'gr.DrawLine(Pens.Purple, x1, y1, x2, y2)
(channel 1 = purple)
y1 = y2
'modify
'draw line
'saving
previous point
'If x2 > pictureBox1.Width Then
'test if
end of screen
'x2 = Freq
'x1 = 1
'pictureBox1.Image = Nothing
'clear
PictureBox1
'End If
HighByteFlag = 0
'prepare for next data
point
Chan2Flag = 1
End If
' Collect Data for Channel 2
Else
If HighByteFlag = 0 Then
ADData = n
ADData = ADData \ 16
HighByteFlag = 1
Else
n = n \ 2
n = n * 8
ADData = ADData + n
y2 = Offset2 - Gain * ADData
'get channel 2 now
'gr.DrawLine(Pens.OrangeRed, x1, y11, x2, y2)
line (channel 2 = orange-red)
'PictureBox1.Refresh()
'draw
y11 = y2
x1 = x2
x2 = x1 + Freq
HighByteFlag = 0
Chan2Flag = 0
End If
End If
'GoTo xxx
' Error Trapping
'xx:
'this is entered if input data causes error in asc()
'BadCount = BadCount + 1
'Label3.Text = Str(BadCount)
'Resume xxx
'increment badcount number
'display
'continue, do nothing else
'xxx:
'Application.DoEvents()
'On Error GoTo 0
'clear error trapping
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Gain = Gain + 2
If Gain > 2 Then
Button4.Enabled = True
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Gain = Gain - 2
If Gain <= 2 Then
Button4.Enabled = False
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button6.Click
Freq = Freq + 2
If Freq > 5 Then
Button5.Enabled = True
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button5.Click
Freq = Freq - 2
If Freq <= 5 Then
Button5.Enabled = False
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
pictureBox1.Dispose()
pictureBox1 = Nothing
End
End Sub
End Class
Download