Receiving Data From Serial Port & Starting a Timer - Visual Basic Express

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Fitzgerald
    New Member
    • May 2012
    • 1

    Receiving Data From Serial Port & Starting a Timer - Visual Basic Express

    Alright. So I'm building a small timing system as a "beta" for a local racetrack. I know that the VB Timer is inaccurate .. But that's why it's a beta. I'm using Visual Basic Express 2010

    I'm taking an infrared sensor, and connecting it to a microcontroller . The microcontroller will read the input of the sensor (whether it's broke or not) and then decide what it needs to do.

    Here's how it's SUPPOSED to work together:

    You choose how many laps of qualifying you would want to do on the VB program. 1 lap or 2 laps. The VB program will then send a string to the microcontroller saying whether it's 1 or 2 laps. Next, the microcontroller checks to make sure the infrared is not broke, and it's reflecting off of the reflector. If it is reflecting, the program will wait until the infrared beam is broke. Now it will send a string to the VB program. The VB program will decide what that string is supposed to mean. (Start or Stop the timer). The problem I'm having is getting the VB program to receive the data being sent, and doing something with it. Here is the code i'm using to send data to the microcontroller :
    Code:
    Private WithEvents serialPort As New IO.Ports.SerialPort
    
    
    
    serialPort.Write("1") ' ThPrivate Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If serialPort.IsOpen Then
    serialPort.Close()
    End If
    Try
    With serialPort
    .PortName = "COM5"
    .BaudRate = 9600
    .Parity = IO.Ports.Parity.None
    .DataBits = 8
    .StopBits = IO.Ports.StopBits.One
    .Handshake = IO.Ports.Handshake.None
    End With
    serialPort.Open()
    Catch ex As Exception
    MsgBox(ex.ToString)
    End Try
    
    End Sub
    
    Private Sub OneLap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OneLap.Click
    
    'When you click OneLap it will show a bunch of labels, and other buttons.
    
    PictureBox2.Visible = True
    Label1.Visible = False
    Button1.Visible = False
    Button2.Visible = False
    Button3.Visible = True
    OneThousandths.Visible = True
    OneHundredths.Visible = True
    OneTenths.Visible = True
    OneDP.Visible = True
    OneSeconds.Visible = True
    Button4.Visible = True
    Button6.Visible = True
    Label2.Visible = True
    Label15.Visible = True
    Label16.Visible = True
    CheckBox1.Visible = Truee microcontroller reads "1" as 1 Lap Qualifying
    
    End Sub
    This code above only shows what would happens when you click a button for 1 Lap Qualifying.

    Here's what I need the VB Program to do:

    When the Microcontroller sends a string of "1" it needs to start the timer. When it receives the string of "2" it needs to stop the timer.

    (if it's two laps)

    When the Microcontroller sends a string of "3" it needs to start the Lap One timer. When it Receives the string of "4" it needs to stop the Lap One timer and Start the Lap Two timer. When it receives the string "5" it needs to stop the Lap Two timer.

    I am fairly new to this, so how would I do it??

    Any help is appreciated!!

    -Ryan
    Last edited by PsychoCoder; May 25 '12, 08:54 PM. Reason: Code tags added
Working...