| Slave Control with Visual Basic Example Last Modified: 2006-11-07 | | |
| Acroname Robotics | PDF webpage version | ||
| Introduction This example has some routines that enable a Visual Basic application to communicate with a BrainStem acting as a slave controller. These routines were written for Microsoft Visual Basic 5 using MSComm control. With one minor change (omit the calls to DoEvents), this VBasic5 example should also work on an iPaq using embedded VBasic from MS embedded VisualTools 3.0. This code is not a complete program. It is only intended as a starting point. As a slave controller, the BrainStem receives command packets from a host application via link and sends data packets back to that application. The source code listed below has routines for sending and receiving packets through the serial port as the link between the controllers. Included are constants for command codes that may be used with the BrainStem GP 1.0 module. Source Code ' filename: sendget.txt
'--------------------------------------------------------------------------
Option Explicit
Public bstem As Byte ' bstem is a variable because
' I use 2 modules
Dim bbuff(0 To 12) As Byte ' Arrays and ...
Const cmdDEV_VAL As Byte = 4 ' constants can not be declared "Public"
Const cmdVAL_GET As Byte = 17 ' on object-module level in VB
Const cmdVAL_SET As Byte = 18
Const cmdVAL_SAV As Byte = 19
Const cmdSRV_SAV As Byte = 20
Const cmdVM_RUN As Byte = 21
Const cmdVM_KILL As Byte = 22
Const cmdDEBUG As Byte = 23
Const cmdRESET As Byte = 24
Const cmdA2D_RD As Byte = 25
Const cmdDIG_CFG As Byte = 26
Const cmdDIG_IO As Byte = 27
Const cmdDIG_RST As Byte = 28
Const cmdPTIME_RD As Byte = 29
Const cmdIR02_RD As Byte = 30
Const cmdSRV_CFG As Byte = 31
Const cmdSRV_LMT As Byte = 32
Const cmdSRV_ABS As Byte = 33
Const cmdSRV_REL As Byte = 34
Const cmdSRV_RFLX As Byte = 35
Const cmdSRV_STOP As Byte = 36
Const cmdIIC_RD As Byte = 37
Const cmdRAW_INPUT As Byte = 38
Const cmdTMR_SET As Byte = 39
Const cmdRFLXE_CFG As Byte = 40
Const cmdRFLXE_CHK As Byte = 41
Const cmdCTR_SET As Byte = 42
Const cmdCTR_CT As Byte = 43
Const cmdRAIL As Byte = 44
Const cmdFUNC As Byte = 45
Const cmdMPD_SET As Byte = 46
Const cmdMPD_CHK As Byte = 47
Const cmdWINDOW As Byte = 48
Const cmdERRAMP As Byte = 49
Const cmdERRATT As Byte = 50
Const cmdPAD_IO As Byte = 51
Const cmdPAD_INPUT As Byte = 52
Const cmdPWINDOW As Byte = 53
Const cmdPERRAMP As Byte = 54
Const cmdPERRATT As Byte = 55
Const cmdPTMR_SET As Byte = 56
Const cmdPCTR_SET As Byte = 57
Const cmdPCTR_WR As Byte = 58
'--------------------------------------------------------------------------
Private Sub Form_Load()
'The program uses the MSComm-Control default property settings:
'MSComm1.DTREnable = True
'MSComm1.EOFEnable = False
'MSComm1.Handshaking = comNone
'MSComm1.InBufferSize = 1024
'MSComm1.InputLen = 0
'MSComm1.InputMode = comInputModeText
'MSComm1.NullDiscard = False
'MSComm1.OutBufferSize = 512
'MSComm1.RTSEnable = False
'MSComm1.SThreshold = 0
MSComm1.RThreshold = 0 'this value should be different from
'default value 0 when using an OnComm event
MSComm1.Settings = "9600,N,8,1"
MSComm1.CommPort = 2 'select COM-Port
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
End Sub
'--------------------------------------------------------------------------
Private Sub Form_Unload(Cancel As Integer)
If MSComm1.PortOpen = True Then MSComm1.PortOpen = False
End Sub
'--------------------------------------------------------------------------
Private Sub sendPacketVB()
Dim i As Integer
Dim SendString As String
SendString = ""
SendString = Chr(bstem)
SendString = SendString & Chr(bbuff(0))
For i = 1 To bbuff(0)
SendString = SendString & Chr(bbuff(i))
Next
MSComm1.Output = SendString
End Sub
'--------------------------------------------------------------------------
Private Sub getPacketVB()
Dim txtBuff As String
Dim i As Integer
Dim c As Long
Dim BuffLength
MSComm1.InBufferCount = 0 ' clear inBuffer
Do ' wait for incoming bytes
DoEvents
Loop Until MSComm1.InBufferCount > 0
txtBuff = MSComm1.Input
BuffLength = Len(txtBuff) ' number of received bytes
' (<= 4 bytes)
bstem = Asc(Mid(txtBuff, 1, 1)) ' get module number
bbuff(0) = Asc(Mid(txtBuff, 2, 1)) ' get packet size
If BuffLength < 2 + bbuff(0) Then ' if received bytes < packet size
MSComm1.InBufferCount = 0 ' do it again
Do
DoEvents
Loop Until MSComm1.InBufferCount > 0
txtBuff = txtBuff & MSComm1.Input
End If
For i = 3 To Len(txtBuff) ' write received data
c = Asc(Mid(txtBuff, i, 1)) ' to array bbuff()
bbuff(i - 2) = c
Next
End Sub
'--------------------------------------------------------------------------
' just an example function to get started
Function readIR02_VB(moduleNR) As Byte
bstem = moduleNR
bbuff(0) = 2
bbuff(1) = cmdIR02_RD
bbuff(2) = 128
Call sendPacketVB
Call getPacketVB
readIR02_VB = bbuff(3)
End Function
Credits Source code contributed by Gunter Wendel. Revision History:
| |||
Related Examples: Using a BrainStem as a Serial Slave Device with a BasicX Controller Using Visual Basic to Control a Robot Truck and BrainStem Controllers | ||||
| voice: 720-564-0373, email: sales@acroname.com, address: 4822 Sterling Dr., Boulder CO, 80301-2350, privacy © Copyright 1994-2010 Acroname, Inc., Boulder, Colorado. All rights reserved. |