处理器接口
概述
这个样本程序通过处理器I/O端口与外部仪器进行通信。
这个程序将5(位2和位0设置为低,其他位设置为高)输出到处理器I/O端口的端口A,然后等待直到端口C的位3设置为低。
参见该程序的输入/输出数据。
程序编码
Excel VBA
Sub Handler_Click()
Dim defrm As Long 'Session to Default Resource Manager.
Dim vi As Long 'Session to instrument.
Dim Out_Data As Integer 'Decimal value.
Dim In_Data As Long
Dim Bit_stat As Integer
Dim Flag_bit As Integer
Dim Out_Data_Bin As String
Dim Ret As Long 'Return value.
Dim i As Long
Dim X As Long
Const TimeOutTime = 40000 'timeout time.
Out_Data_Bin = "00000101" 'Store the output data on the port A (binaly).
Flag_bit = 3 'Bit location (bit 3).
Call viOpenDefaultRM(defrm) 'Initializes the VISA system.
Call viOpen(defrm, "GPIB0::17::INSTR", 0, 0, vi) 'Opens the session to the specified instrument.
Call viSetAttribute(vi, VI_ATTR_TMO_VALUE, TimeOutTime) 'The state of an attribute for the specified session.
Call viVPrintf(vi, "*RST" & vbLf, 0) 'Presets the setting state of the ENA.
Call viVPrintf(vi, "*CLS" & vbLf, 0) 'Clears the all status register.
Call viVPrintf(vi, ":CONT:HAND:C:MODE INP" & vbLf, 0) 'Configures the port C to input port.
Call viVPrintf(vi, ":CONT:HAND:IND:STAT ON" & vbLf, 0) 'Line enable /INDEX signal.
Call viVPrintf(vi, ":CONT:HAND:RTR:STAT ON" & vbLf, 0) 'Line enable /READY FOR TRIGGER signal.
For i = 1 To Len(Out_Data_Bin) 'Convert Out_Dara_Bin to a decimal value.
If Mid(Out_Data_Bin, Len(Out_Data_Bin) - i + 1, 1) = "1" Then
X = 2 ^ (i - 1)
Ret = Ret + X
End If
Next i
Out_Data = Ret 'Sets the decimal value.
Call viVPrintf(vi, ":CONT:HAND:A " & Ret & vbLf, 0) 'Sets to the port A.
Call viVPrintf(vi, ":CONT:HAND:C?" & vbLf, 0) 'Outputs data to output port C.
Call viVScanf(vi, "%t", In_Data) 'Reads data from the port C.
Call ErrorCheck(vi) 'Checking the error.
Call viClose(vi) 'Closes the resource manager session.
Call viClose(defrm) 'Breaks the communication and terminates the VISA system.
End
End Sub
Sub ErrorCheck(vi As Long)
Dim err As String * 50, ErrNo As Variant, Response
Call viVQueryf(vi, ":SYST:ERR?" & vbLf, "%t", err) 'Reads error message.
ErrNo = Split(err, ",") 'Gets the error code.
If Val(ErrNo(0)) <> 0 Then
Response = MsgBox(CStr(ErrNo(1)), vbOKOnly) 'Display the message box.
End If
End Sub
HT Basic (handler.htb)
10 INTEGER Out_data,In_data,Bit_stat
20 DIM Out_data_bin$[9]
30 !
40 ASSIGN @Agte507x TO 717
50 !
60 Out_data_bin$="00000101"
70 Flag_bit=3
80 !
90 OUTPUT @Agte507x;":CONT:HAND:C:MODE INP"
100 OUTPUT @Agte507x;":CONT:HAND:IND:STAT ON"
110 OUTPUT @Agte507x;":CONT:HAND:RTR:STAT ON"
120 !
130 Out_data=IVAL(Out_data_bin$,2)
140 OUTPUT @Agte507x;":CONT:HAND:A ";Out_data
150 !
160 REPEAT
170 OUTPUT @Agte507x;":CONT:HAND:C?"
180 ENTER @Agte507x;In_data
190 Bit_stat=BIT(In_data,Flag_bit)
200 UNTIL Bit_stat=1
210 END