Lake Shore Model 321 Autotuning Temperature Controller User’s Manual
4-6
Remote Operation
Table 4-3. Visual Basic Serial Interface Program
Public gSend As Boolean 'Global used for Send button state
Private Sub cmdSend_Click() 'Routine to handle Send button press
gSend = True 'Set Flag to True
End Sub
Private Sub Form_Load() 'Main code section
Dim strReturn As String 'Used to return response
Dim strHold As String 'Temporary character space
Dim Term As String 'Terminators
Dim ZeroCount As Integer 'Counter used for Timing out
Dim strCommand As String 'Data string sent to instrument
frmSerial.Show 'Show main window
Term = Chr(13) & Chr(10) 'Terminators are <CR><LF>
ZeroCount = 0 'Initialize counter
strReturn = "" 'Clear return string
strHold = "" 'Clear holding string
If frmSerial.MSComm1.PortOpen = True Then 'Close serial port to change settings
frmSerial.MSComm1.PortOpen = False
End If
frmSerial.MSComm1.CommPort = 1 'Example of Comm 1
frmSerial.MSComm1.Settings = "9600,o,7,1" 'Example of 9600 Baud,Parity,Data,Stop
frmSerial.MSComm1.InputLen = 1 'Read one character at a time
frmSerial.MSComm1.PortOpen = True 'Open port
Do
Do 'Wait loop
DoEvents 'Give up processor to other events
Loop Until gSend = True 'Loop until Send button pressed
gSend = False 'Set Flag as false
strCommand = frmSerial.txtCommand.Text 'Get Command
strReturn = "" 'Clear response display
strCommand = UCase(strCommand) 'Set all characters to upper case
If strCommand = "EXIT" Then 'Get out on EXIT
End
End If
frmSerial.MSComm1.Output = strCommand & Term 'Send command to instrument
If InStr(strCommand, "?") <> 0 Then 'Check to see if query
While (ZeroCount < 20) And (strHold <> Chr$(10)) 'Wait for response
If frmSerial.MSComm1.InBufferCount = 0 Then 'Add 1 to timeout if no character
frmSerial.Timer1.Enabled = True
Do
DoEvents 'Wait for 10 millisecond timer
Loop Until frmSerial.Timer1.Enabled = False
ZeroCount = Zer 1 'Timeout at 2 seconds
Else
ZeroCount = 0 'Reset timeout for each character
strHold = frmSerial.MSComm1.Input 'Read in one character
strReturn = str strHold 'Add next character to string
End If
Wend 'Get characters until terminators
If strReturn <> "" Then 'Check if string empty
strReturn = Mid(strReturn, 1, InStr(strReturn, Term) - 1) 'Strip terminators
Else
strReturn = "No Response" 'Send No Response
End If
frmSerial.txtResponse.Text = strReturn 'Put response in textbox on main form
strHold = "" 'Reset holding string
ZeroCount = 0 'Reset timeout counter
End If
Loop
End Sub
Private Sub Timer1_Timer() 'Routine to handle Timer interrupt
frmSerial.Timer1.Enabled = False 'Turn off timer
End Sub