Visual Basic Code (April 30, 2001 5:00 pm)

 

Dim sPulseCount As Long

Dim counting As Long

Public sInstr As String         ' Direction string from frmSerial

Public bDirChange As Boolean    ' Flag for direction change

 

Private Sub cmdExit_Click()     ' Exit program

    End

End Sub

 

Private Sub Form_Load()

counting = 0

' Use COM1

MSComm1.CommPort = 1

' 2400 baud, no parity, 8 data bits, 1 stop bit

MSComm1.Settings = "2400,N,8,1"

' Open the port

MSComm1.PortOpen = True

' Fire CommEvent every time (1) character is received

MSComm1.RThreshold = 1

 

' Display COM1 port setting to user

If MSComm1.PortOpen = True Then

    lblComStatus.Caption = "COM1 open for data transfer"

Else

    lblComStatus.Caption = "COM1 closed"

End If

 

' Default to optForward being selected

optForward.Value = True

 

frmPlot.Show              ' Show plotting interface form

 

End Sub

 

Private Sub cmdSend_Click()

 

'  Get instruction value and pulse output value

If optForward.Value = True Then

    sInstr = "F"

    sPulseCount = txtPulseForward.Text

ElseIf optBackward.Value = True Then

    sInstr = "B"

    sPulseCount = txtPulseBackward.Text

ElseIf optRight.Value = True Then

    sInstr = "R"

    sPulseCount = txtPulseRight.Text

ElseIf optLeft.Value = True Then

    sInstr = "L"

    sPulseCount = txtPulseLeft.Text

End If

 

bDirChange = True       ' Allow change in direction

 

' Do not allow for invalid data

'If nPulseCount > 255 Then nPulseCount = 255

'If nPulseCount < 1 Then nPulseCount = 1

 

' Send Out Data

MSComm1.Output = "A" & sInstr & sPulseCount

MSComm1.Output = "A" & sInstr & sPulseCount ' Extra one

' Print data that was sent

lblSout.Caption = "A" & sInstr & sPulseCount

 

End Sub

 

Private Sub Form_Unload(Cancel As Integer)

' Close port COM1

MSComm1.PortOpen = False

 

' Display COM1 port setting to user

If MSComm1.PortOpen = True Then

    lblComStatus.Caption = "COM1 open for data transfer"

Else

    lblComStatus.Caption = "COM1 closed"

End If

 

End Sub

 

Private Sub MSComm1_OnComm()

    Select Case MSComm1.CommEvent

        Case comEvReceive  ' Received data

           

            If counting = 200 Then counting = 0

            counting = counting + 1

' Plot the robot in response to input pulses

'            If (counting Mod 10) = 0 Then

                Call frmPlot.PlotRobot

'            End If

 

'lblSin.Caption = MSComm1.Input

'lblSin.Caption = counting & lblSin.Caption

'Print lblSin.Caption

            lblSin.Caption = counting

           

'            If MSComm1.Input = "E" Then

                lblSin.Caption = MSComm1.Input

'            End If

   

    End Select

End Sub