Chapter 3  Sample programs

 

3.1 Visual Basic Example programs

 

RS-232C Example program

 

The following example programs are made of "MSComm control" of the Visual Basic.

 

Follow the following procedure in order to execute programs.

(1)   Open the component of the project from the menu of the Visual Basic, then check the checkbox of the "Comm Control" and press the button, OK. The "MSComm control" is registered.

(2)   Paste the "text box", "MSComm control", and "command button" to the "form".

(3)   Change the property of the "MultiLine" of the "Text1" into "True".

(4)   Write the following example programs after double clicking the command button.

 

 

 

“&” markindicates the string variable in the following programs.

 

 

 

Example 1  Using a setting command

 

►Send the command in the format specified, when the conditions for the command to be acceptable are met.

 

Private Sub Sample1_Click()
'*******************************************************************************
' RS232C SAMPLE PROGRAM NO.1
'*******************************************************************************
               deli$ = Chr$(13) & Chr$(10)
               Comm1.PortOpen = True
               Comm1.Output = ":FUNCTION MEM" & deli$
               Comm1.Output = ":CONFIGURE:TDIV 1.E-3" & deli$
               Comm1.Output = ":CONFIGURE:SHOT 20" & deli$
               Comm1.Output = ":TRIGGER:SOURCE OR" & deli$
               Comm1.Output = ":TRIGGER:KIND CH1,LEVEL" & deli$
               Comm1.Output = ":TRIGGER:PRETRIG 5" & deli$
               Comm1.Output = ":TRIGGER:LEVEL CH1,2" & deli$
               Comm1.Output = ":TRIGGER:SLOPE CH1,UP" & deli$
               Comm1.Output = ":TRIGGER:KIND CH2,OFF" & deli$
               Comm1.Output = ":TRIGGER:KIND CH3,OFF" & deli$
               Comm1.Output = ":TRIGGER:KIND CH4,OFF" & deli$
               Comm1.Output = ":START" & deli$
               Comm1.PortOpen = False
End Sub

 

 

 

 

Example 2  Using a query

 

►Send the query in the format specified, when the conditions for the query to be acceptable are met.

►The response data from the query is returned in the format specified for the corresponding command.

 

Private Sub Sample2_Click()

'*******************************************************************************

' RS232C SAMPLE PROGRAM NO.2

'*******************************************************************************

             deli$ = Chr$(13) & Chr$(10)

             Comm1.PortOpen = True

             Comm1.Output = ":HEADER OFF" & deli$

             Comm1.Output = ":FUNCTION?" & deli$

 

             Do

                           ans$ = ans$ & Comm1.Input

             Loop Until InStr(1, ans$, Chr$(10))

            

             Comm1.Output = ":SYSTEM:TIME?" & deli$

 

             Do

                           tm$ = tm$ & Comm1.Input

             Loop Until InStr(1, tm$, Chr$(10))

 

             Text1.Text = "FUNCTION = " & ans$

             Text1.Text = Text1.Text & "TIME = " & tm$

             Comm1.PortOpen = False

End Sub

 

 

 

 

Example 3  Outputting stored data

 

►Using the :MEMORY:MAXPOINT? query, this program checks whether data can be output from memory. If this query returns zero, no data is stored, and it cannot therefore be output.

►Next, the program specifies the channel and point for output, using the :MEMORY:POINT command. As data is input or output, the point is incremented automatically. If capturing data consecutively, it is sufficient to specify the point once only.

►To capture data in ASCII format use the :MEMORY:ADATA? query, and to capture data as voltage values use the :MEMORY:VDATA? query. The number of data samples which may be output in one set is 1 to 80 using :ADATA? and 1 to 40 using the :VDATA? query.

►Outputting data in bigger sets reduces the overall processing time.

►Read data (2000 samples) for channel 1 when stored with a 20-division recording length.

 

Private Sub Sample3_Click()

'*******************************************************************************

' RS232C SAMPLE PROGRAM NO.3

'*******************************************************************************

             deli$ = Chr$(13) & Chr$(10)

             Dim d(2000)

             Comm1.PortOpen = True

             Comm1.Output = ":FUNCTION MEM" & deli$

             Comm1.Output = ":CONFIGURE:SHOT 20" & deli$

             Comm1.Output = ":TRIGGER:MODE SINGLE" & deli$

             Comm1.Output = ":START;*OPC?" & deli$

 

             Do

                           o$ = o$ & Comm1.Input

             Loop Until InStr(1, o$, Chr$(10))

 

             Comm1.Output = ":HEADER OFF" & deli$

             Comm1.Output = ":MEMORY:MAXPOINT?" & deli$

 

             Do

                           mx$ = mx$ & Comm1.Input

             Loop Until InStr(1, mx$, Chr$(10))

 

             If (Val(mx$) <> 2000) Then

                           Comm1.PortOpen = False

                           Exit Sub

             End If

 

             Comm1.Output = ":MEMORY:POINT CH1,0" & deli$

 

             For i = 0 To 2000

                           Comm1.Output = ":MEMORY:VDATA? 1" & deli$

                           Do

                                         vd$ = vd$ & Comm1.Input

                           Loop Until InStr(1, vd$, Chr$(10))

                           d(i) = Val(vd$)

             Next

             For i = 0 To 2000

                           Text1.Text = d(i)

             Next

             Comm1.PortOpen = False

End Sub

 

 

 

 

Example 4 Inputting storage data.

 

►This program prepares storage memory, using the :MEMORY:PREPARE command.

►Next, the program specifies the channel and point for input, using the :MEMORY:POINT command, and then uses the :MEMORY:ADATA command to input data.

 

Private Sub Sample4_Click()
'*******************************************************************************
' RS232C SAMPLE PROGRAM NO.4
'*******************************************************************************
               deli$ = Chr$(13) & Chr$(10)
               Comm1.PortOpen = True
               Comm1.Output = ":FUNCTION MEM" & deli$
               Comm1.Output = ":CONFIGURE:SHOT 20" & deli$
               Comm1.Output = ":MEMORY:PREPARE;*OPC?" & deli$
 
               Do
                               o$ = Comm1.Input
               Loop Until InStr(1, o$, Chr$(10))
 
               Comm1.Output = ":MEMORY:POINT CH1,0" & deli$
 
               For i = 0 To 2000
                               Comm1.Output = ":MEMORY:ADATA " & Ltrim(Str$(Int(500 * Sin(3.14 * i / 500)))) &
                               deli$
               Next
               Comm1.PortOpen = False
End Sub

 

 

 

 

Example 5  Checking the presence of input unit, and displaying the input ranges on the screen.

 

 

Private Sub Sample5_Click()
'*******************************************************************************
' RS232C SAMPLE PROGRAM NO.5
'*******************************************************************************
               deli$ = Chr$(13) & Chr$(10)
               Comm1.PortOpen = True
               Comm1.Output = ":HEADER OFF" & deli$
               Comm1.Output = "*OPT?" & deli$
 
               Do
                               op$ = op$ & Comm1.Input
               Loop Until InStr(1, op$, Chr$(10))
 
               ch1% = Val(Mid$(op$, 1, 1))
               ch2% = Val(Mid$(op$, 3, 1))
               ch3% = Val(Mid$(op$, 5, 1))
               ch4% = Val(Mid$(op$, 7, 1))
               Comm1.Output = ":MEMORY:GETREAL"& deli$
 
               If (ch1% <> 0) Then
                               Comm1.Output = ":MEMORY:AREL? CH1" & deli$
                               ar$ = ""
                               Do
                                              ar$ = ar$ & Comm1.Input
                               Loop Until InStr(1, ar$, Chr$(10))
                               ch1_data$ = "CH1 = " & ar$
               Else
                               ch1_data$ = "CH1 = NON"
               End If
 
               If (ch2% <> 0) Then
                               Comm1.Output = ":MEMORY:AREL? CH2" & deli$
                               ar$ = ""
                               Do
                                              ar$ = ar$ & Comm1.Input
                               Loop Until InStr(1, ar$, Chr$(10))
                               ch2_data$ = "CH2 = " & ar$
               Else
                               ch2_data$ = "CH2 = NON"
               End If
 
               If (ch3% <> 0) Then
                               Comm1.Output = ":MEMORY:AREL? CH3" & deli$
                               ar$ = ""
                               Do
                                              ar$ = ar$ & Comm1.Input
                               Loop Until InStr(1, ar$, Chr$(10))
                               ch3_data$ = "CH3 = " & ar$
               Else
                               ch3_data$ = "CH3 = NON"
               End If
 
               If (ch4% <> 0) Then
                               Comm1.Output = ":MEMORY:AREL? CH4" & deli$
                               ar$ = ""
                               Do
                                              ar$ = ar$ & Comm1.Input
                               Loop Until InStr(1, ar$, Chr$(10))
                               ch4_data$ = "CH4 = " & ar$
               Else

                               ch4_data$ = "CH4 = NON"

               End If

 

               Text1.Text = ch1_data$ & deli$ & ch2_data$ & deli$ & ch3_data$ & deli$ & ch4_data$ &

               deli$

               Comm1.PortOpen = False

End Sub

 

 

 

 

Example 6  Saving stored data onto drive A

 

 

Private Sub Sample6_Click()
'*******************************************************************************
' RS232C SAMPLE PROGRAM NO.6
'*******************************************************************************
               deli$ = Chr$(13) & Chr$(10)
               na$ = "a:\sample.dat"
               Comm1.PortOpen = True
               Comm1.Output = ":HEADER OFF" & deli$
               Comm1.Output = ":MEMORY:MAXPOINT?" & deli$
 
               Do
                               mx$ = mx$ & Comm1.Input
               Loop Until InStr(1, mx$, Chr$(10))
 
               If (Val(mx$) = 0) Then
                               Comm1.PortOpen = False
                               Exit Sub
               End If
 
               Open na$ For Output As #1
               Comm1.Output = ":MEMORY:POINT CH1,0" & deli$
               Print #1, 10
 
               For i = 0 To 10
                               Comm1.Output = ":MEMORY:ADATA? 1" & deli$
                               ad$ = ""
                               Do
                                              ad$ = ad$ & Comm1.Input
                               Loop Until InStr(1, ad$, Chr$(10))
                               Print #1, Val(ad$)
               Next
 
               Close #1
               Comm1.PortOpen = False
End Sub

 

 

 

 

Example 7  Reading the data saved in Example 6, and loading it into the unit.

 

 

Private Sub Sample7_Click()
'*******************************************************************************
' RS232C SAMPLE PROGRAM NO.7
'*******************************************************************************
               deli$ = Chr$(13) & Chr$(10)
               na$ = "a:\sample.dat"
               Comm1.PortOpen = True
               Comm1.Output = ":HEADER OFF" & deli$
               Comm1.Output = ":MEMORY:PREPARE" & deli$
 
               Do
                               o$ = Comm1.Input
               Loop Until InStr(1, o$, Chr$(10))
 
               Open na$ For Output As #1
               Comm1.Output = ":MEMORY:POINT CH1,0" & deli$
               Input #1, mx
 
               For i = 0 To mx
                               Input #1, dt
                               Comm1.Output = ":MEMORY:ADATA " & Str$(dt) & deli$
               Next
 
               Close #1
               Comm1.PortOpen = False
End Sub