[Up] [Transmit VB Code] [Receive VB Code] [Feedback Hardware]
'MultiBot II Interface Source Code
'Senior Project 2003
'Receiver Program
'frmMain
Dim InString As String
Dim command As String
Dim ret As Long
Dim Binary As String
Dim Compass As String
Dim Sonar As String
Dim direction As String
Dim SonSen As String
Dim Computer As String
Option Explicit
Private Sub FExit_Click()
'Close Program
Computer = "OFF"
AwusbIO1.Close
End
End Sub
Private Sub Form_Load()
'Open Comm Port when robot starts
MSComm1.PortOpen = True
'Check if port is open and if it is
'set status label
If MSComm1.PortOpen = True Then
lblstatus.Caption = "Port is open"
Else
lblstatus.Caption = "Port is closed"
End If
'initialize parallel port to 00000000
vbOut 888, 0
'set variable to verify that form is loaded
Computer = "ON"
'update status and open board
txtStatus.Text = "Open " + AwusbIO1.Open(0)
'Enable all 16 I/O Pins
AwusbIO1.EnablePort (CLng(65535))
'clear the I/O Pins
AwusbIO1.OutPort (0)
End Sub
Public Sub Forward()
'Output 00000101 to the parallel port
vbOut 888, 5
lblTest.Caption = "Forward"
command = "Forward"
End Sub
Public Sub Reverse()
'Output 00001010 to the parallel port
vbOut 888, 10
lblTest.Caption = "Reverse"
command = "Reverse"
End Sub
Public Sub LeftT()
'Output 00001001 to the parallel port
vbOut 888, 9
lblTest.Caption = "Left"
command = "Left"
End Sub
Public Sub RightT()
'Output 00000110
vbOut 888, 6
lblTest.Caption = "Right"
command = "Right"
End Sub
Public Sub EStop()
'Output 00000000 to the parallel port
vbOut 888, 0
lblTest.Caption = "Stop"
command = "Stop"
End Sub
Public Sub turncleft()
'Output 00010000 to the parallel port
vbOut 888, 16
lblTest.Caption = "Turning camera left"
command = "stop"
End Sub
Public Sub turncright()
'Output 00100000 to the parallel port
vbOut 888, 32
lblTest.Caption = "Turning camera right"
command = "stop"
End Sub
Public Sub stopcam()
'Output 00000000 to the parallel port
vbOut 888, 0
lblTest.Caption = "Stopping camera"
command = "stop"
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
' Handle each event or error by placing
' code below each case statement
' Errors
'If RF link is broken stop robot
Case comEventBreak ' A Break was received.
Call EStop
Case comEventFrame ' Framing Error
Case comEventOverrun ' Data Lost.
Case comEventRxOver ' Receive buffer overflow.
Case comEventRxParity ' Parity Error.
Case comEventTxFull ' Transmit buffer full.
Case comEventDCB ' Unexpected error retrieving DCB]
' Events
Case comEvCD ' Change in the CD line.
Case comEvCTS ' Change in the CTS line.
Case comEvDSR ' Change in the DSR line.
Case comEvRing ' Change in the Ring Indicator.
Case comEvReceive ' Received RThreshold # of chars
' Retrieve all available data.
MSComm1.InputLen = 0
' Check for data.
If MSComm1.InBufferCount Then
' Read data.
Dim IString
As String
InString =
MSComm1.Input
IString =
InString
InString =
Right(IString, Len(IString) - 1)
'selects what
was sent from base computer
Select Case
InString
Case "forward"
Call Forward
Case "reverse"
Call Reverse
Case "left"
Call LeftT
Case "right"
Call RightT
Case "ltrim"
Call LTrim
Case "rtrim"
Call RTrim
Case "stop"
Call EStop
Case "turnr"
Call turncright
Case "turnl"
Call turncleft
Case "stopcam"
Call stopcam
End Select
End If
Case comEvSend ' There are SThreshold number of
' characters in the transmit buffer.
Case comEvEOF ' An EOF charater was found in
' the input stream
End Select
End Sub
Public Sub LTrim()
'Output 00000001 to the parallel port
vbOut 888, 1
command = "Left Trim"
End Sub
Public Sub RTrim()
'Output 00000100 to the parallel port
vbOut 888, 4
command = "Right Trim"
End Sub
Private Sub Feedback()
'sets up needed variables
Dim fp As Long
Dim filename As String
'sets file to write to
'and gets file pointer number from system
filename = "C:\feedback\feedback.txt"
fp = FreeFile
'opens file to write to
'each variable will be comma deliminated
'file is closed(must be done)
Open filename For Output As #fp
Write #fp, command, direction, SonSen, Computer
Close #fp
End Sub
Private Sub cmdClose_Click()
'update status and close board
txtStatus.Text = "Closed " + AwusbIO1.Close
End Sub
Private Sub cmdExit_Click()
Computer = "OFF"
'close board
AwusbIO1.Close
'unload form
Unload Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
'close board if form is closed
AwusbIO1.Close
End Sub
Private Sub TmrInput_Timer()
'input decimal value from inport
ret = AwusbIO1.InPort
'convert decimal input to binary value
Call Dec2Bin(ret)
'split the compass portion off of input
Compass = Right(Binary, 4)
'put compass binary in textbox
txtCompass = Compass
'send compass input through case statement
Heading (Compass)
'split the sonar section off of input
Sonar = Left(Binary, 4)
'show sonar binary in textbox
txtSonar = Sonar
'send sonar input through case statement
Alert (Sonar)
Call Feedback
End Sub
Public Function Dec2Bin(ByVal DecNum As Long) As String
'function that converts decimal integer to a binary string
Select Case DecNum
Case Is > 1
Dec2Bin =
Dec2Bin(DecNum \ 2) & CStr(DecNum And 1)
Case Is > 0
Dec2Bin = "1"
Case Else
Dec2Bin = "0"
End Select
Dec2Bin = Right("00000000" & Dec2Bin, 8)
Binary = Dec2Bin
End Function
Public Sub Heading(Compass)
'put heading of compass into variable for feedback
Select Case Compass
Case "1110"
direction =
"NORTH"
Case "1101"
direction =
"EAST"
Case "1100"
direction =
"NORTHEAST"
Case "1011"
direction =
"SOUTH"
Case "1001"
direction =
"SOUTHEAST"
Case "0111"
direction =
"WEST"
Case "0110"
direction =
"NORTHWEST"
Case "0011"
direction =
"SOUTHWEST"
Case Else
direction =
"ERROR"
End Select
End Sub
Public Sub Alert(Sonar)
'put sonar alert into variable for feedback
Select Case Sonar
Case "0000"
SonSen =
"CLEAR"
Case "0001"
SonSen =
"BACK"
Case "0010"
SonSen =
"RIGHT"
Case "0100"
SonSen =
"LEFT"
Case "1000"
SonSen =
"FRONT"
Case Else
SonSen =
"ERROR"
End Select
End Sub