Visual
Basic Code (May 6, 2001 5:00 pm)
' Paul Contino
' Personal Robot Technologies, Inc.
' May 6, 2001
' VB serial data exchange interface
Option
Explicit ' All variables must be declared
Dim
sPulseCount As Long
Dim
counting As Long
Public
sInstr As String ' Direction string from frmSerial
Public
bDirChange As Boolean ' Flag for direction change
Public
nOutLength As Integer ' Output string length
Public
nInLength As Integer ' Input string length
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
= "9600,N,8,1"
' Read entire buffer on Input
MSComm1.InputLen
= 0
MSComm1.InBufferSize
= 1024
MSComm1.OutBufferSize
= 1024
' Generate no OnComm event on received data
'MSComm1.RThreshold
= 0
' Generate OnComm event on received data
MSComm1.RThreshold
= 1
' The Input property stores data as text
MSComm1.InputMode
= comInputModeText
' Open the port
MSComm1.PortOpen
= True
' 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()
Dim
sPulseCount As Long
' 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
' Send Out Data
MSComm1.Output
= "A" & sInstr & Chr$(sPulseCount)
' Print data that was sent
lblSout.Caption
= "A" & sInstr & sPulseCount
' Get length of transmitted data
nOutLength
= Len("A" & sInstr & Chr$(sPulseCount))
' Probably should implememnt some way of sending data to the BS
' and if no data returned within a certain time, tell user that
' there might be a problem . . . like the stamp not hooked up
correctly
' or something along those lines
'Call GetData
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 GetData()
Dim
sInput As String ' MSComm input string
Dim
nEncoderCount As Long ' Counts read in by encoder
Dim
lEnd As Long
Dim
sData As String
' Check to see if BS sent
any data
' If MSComm1.InBufferCount > 0 Then
' Allow user to send
commands to robot
DoEvents
' Retrieve incoming
data
sInput = sInput & " "
& MSComm1.Input
' Length of input
buffer
nInLength = Len(sInput)
' Just the incoming
data - no echo
sInput = Mid$(sInput, nOutLength + 1,
nInLength)
' Display data
'lblSin.Caption =
sInput
' End If
' Get starting position
of "!"
lEnd = InStr(sInput, "!")
Do
' Starts the loop
If lEnd > 0 Then ' If > 0 then
we have an "!" in the buffer
sData = Left$(sInput, lEnd - 1)
' sData will be
all characters before lEnd
sInput = Mid$(sInput, lEnd + 1)
' Grab value
from buffer
nEncoderCount = Val(sData)
' We want to
delete the data that we just got from the buffer including the "!"
lEnd = InStr(sInput,
"!")
' Output value
lblSin.Caption = nEncoderCount
' Get starting
position of "!" in the new buffer
End If
Loop While lEnd > 0 ' Loop while "!" is still present in the buffer
End
Sub
Private
Sub MSComm1_OnComm()
Dim
sInput As String ' MSComm input
string
Dim
nTotalCount As Long ' Number of points to plot
Dim
nOldCount As Long ' Storage for old counts
Dim
nEncoderCount As Long ' Counts read in by encoder
Dim
lEnd As Long
Dim
sData As String
Dim i
As Long ' Loop increment
Select Case MSComm1.CommEvent
Case comEvReceive ' Received data
' Allow user to send
commands to robot
DoEvents
' Retrieve incoming
data
sInput = MSComm1.Input
' Length of input
buffer
'nInLength = Len(sInput)
' Just the incoming
data - no echo
'sInput =
Mid$(sInput, nOutLength + 1, nInLength)
'nEncoderCount = nEncoderCount + 1
' lblSin.Caption = nEncoderCount
' lblSin.Caption = sInput
' Get starting
position of "!"
lEnd = InStr(sInput, "!")
Do
' Starts the loop
If lEnd > 0 Then ' If > 0 then
we have an "!" in the buffer
sData = Left$(sInput, lEnd -
1)
' sData will be all characters before lEnd
sInput = Mid$(sInput, lEnd +
1)
' Store old
encoder count
nOldCount = nEncoderCount
' Grab value
from buffer
nEncoderCount = Val(sData)
' We want to
delete the data that we just got from the buffer including the "!"
lEnd = InStr(sInput,
"!")
' Output
value
lblSin.Caption = "OLD:
" & nOldCount & " New: " & nEncoderCount
' Compare
old encoder count with new
If nEncoderCount >
nOldCount Then
'
Calculate difference in counts
nTotalCount =
nEncoderCount - nOldCount
' Call
plotting routine as many times as necessary
For i = 1 To nTotalCount
Call frmPlot.PlotRobot
Next i
End If
' Get
starting position of "!" in the new buffer
End If
Loop While lEnd > 0 ' Loop while "!" is still present in the buffer
' Check for
end statement
If sInput = "END" Then
lblSin.Caption = sInput
End Select
End
Sub