Public Class Form1
Dim volts As Single
Dim voltsQ As New Queue(Of Single)
Dim maxVolts As Single = 5
Dim WithEvents serialPort As New IO.Ports.SerialPort
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer) 'for sleep statements
Dim Picaxeregisters(0 To 13) As Byte ' registers b0 to b13
Dim value As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
Timer1.Interval = 100
Call Serialsensor()
End Sub
Sub Serialsensor()
Dim LabelString As String ' string to display byte values
Dim DataPacket(0 To 17) As Byte ' entire data packet "Data"+14 bytes
Dim ios As Integer ' i is always useful for loops etc
'Label1.Text = "" ' clear the text on the screen
For ios = 0 To 3
DataPacket(ios) = Asc(Mid("Data", ios + 1, 1)) ' add the word "Data" to the packet
Next
For ios = 0 To 13
DataPacket(ios + 4) = Picaxeregisters(ios) ' add all the bytes to the packet
Next
If serialPort.IsOpen Then
serialPort.Close() ' just in case already opened
End If
Try
With serialPort
.PortName = "COM1" ' Most new computers default to com1 but any pre 1999 computer with a serial mouse will probably default to com2
.BaudRate = 2400 ' 2400 is the maxiumum speed for small picaxes
.Parity = IO.Ports.Parity.None ' no parity
.DataBits = 8 ' 8 bits
.StopBits = IO.Ports.StopBits.One ' one stop bit
'.ReadTimeout = 800 ' milliseconds so times out in 1 second if no response
.Open() ' open the serial port
.DiscardInBuffer() ' clear the input buffer
.Write(DataPacket, 0, 18) ' send the datapacket array
Call Sleep(110) ' 100 milliseconds minimum to wait for data to come back and more if data stream is longer
.Read(DataPacket, 0, 18) ' read back in the data packet array
.Close() ' close the serial port
End With
For ios = 4 To 17
LabelString = LabelString + " " + Str(DataPacket(ios)) ' turn into a text string
Next
Label1.Text = LabelString ' put the text string on the screen
Catch ex As Exception
' MsgBox(ex.ToString) ' uncomment this if want to see the actual error message
Label1.Text = "Nothing Received" ' will display this if picaxe not connected etc
End Try
value = DataPacket(5)
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'read the voltage:
volts = value 'get value from RS232 interface
'add the new reading to the end of the queue:
voltsQ.Enqueue(volts)
'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
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
Dim Origin As New Point(200, 200)
Dim xAxisLength As Integer = 300
Dim yAxisLength As Integer = 150
Dim x As Integer
Dim y As Integer
'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
'Fill in the array using the voltage readings:
For i As Integer = 0 To
'Get the volts value from the queue:
Dim v As Single = voltsQarray(i)
' Dim v As Single = voltsQ.ElementAt(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 Sub
End Class