紫外工控论坛

 找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 11727|回复: 0

Agilent万用表用VB写的串口RS232来控制的例子

  [复制链接]
admin 发表于 2010-11-29 23:57:29 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
  1. Option Explicit
  2. ''' """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  3. '''  Copyright ?1999, 2000 Agilent Technologies Inc.  All rights reserved.
  4. '''
  5. ''' You have a royalty-free right to use, modify, reproduce and distribute
  6. ''' the Sample Application Files (and/or any modified version) in any way
  7. ''' you find useful, provided that you agree that Agilent Technologies has no
  8. ''' warranty,  obligations or liability for any Sample Application Files.
  9. '''
  10. ''' Agilent Technologies provides programming examples for illustration only,
  11. ''' This sample program assumes that you are familiar with the programming
  12. ''' language being demonstrated and the tools used to create and debug
  13. ''' procedures. Agilent Technologies support engineers can help explain the
  14. ''' functionality of Agilent Technologies software components and associated
  15. ''' commands, but they will not modify these samples to provide added
  16. ''' functionality or construct procedures to meet your specific needs.
  17. ''' """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
  18. '
  19. Dim DMM As VisaComLib.FormattedIO488
  20. Dim m_ioAddress As String
  21. Const IO_ADDRESS = "ASRL2::INSTR"

  22. Private Sub cmdConfigure_DragDrop(Source As Control, X As Single, Y As Single)

  23. End Sub

  24. Private Sub cmdMeasure_Click()
  25.     ' The following example uses Measure? command to make a single
  26.     ' ac current measurement. This is the easiest way to program the
  27.     ' multimeter for measurements. However, MEASure? does not offer
  28.     ' much flexibility.
  29.     '
  30.     ' Be sure to check set the instrument address on the control
  31.     ' to match the instrument
  32.     '
  33.     Dim reply As Double
  34.    
  35.     ' As part of the example, we check to see if this is RS232,
  36.     ' If it is RS232 we first set the instrument to remote
  37.     '
  38.     ' We need to do a Connect because we are using the io layer
  39.     If InStr(1, DMM.IO.ResourceName, "ASRL") Then ' the io is RS232
  40.         ' send the remote for RS232
  41.         DMM.WriteString "Syst:Rem"
  42.         ' for fast PC's > 300MHz add a delay of >= 50 ms
  43.         delay 50
  44.     End If
  45.    
  46.     ' Clear the text box
  47.     txtResult.Text = ""
  48.     txtResult.Refresh
  49.    
  50.     ' EXAMPLE for using the Measure command
  51.     With DMM
  52.         .WriteString "*RST"
  53.         .WriteString "*CLS"
  54.         ' Set meter to 1 amp ac range
  55.         .WriteString "Measure:Current:AC? 1A,0.001MA"
  56.         ' for fast PC's add a delay of => 50 ms
  57.         delay 200
  58.         reply = .ReadNumber
  59.     End With
  60.         
  61.     txtResult.Text = reply & " amps AC"
  62.    
  63. End Sub

  64. Private Sub cmdConfigure_Click()
  65.     ' The following example uses CONFigure with the dBm math operation.
  66.     ' The CONFigure command gives you a little more programming flexibility
  67.     ' than the MEASure? command. This allows you to 'incrementally'
  68.     ' change the multimeter's configuration.
  69.     '
  70.     ' Be sure to check set the instrument address on the control
  71.     ' to match the instrument address setting
  72.     '
  73.     Dim Readings() As Variant
  74.     Dim i As Long
  75.     Dim status As Long
  76.    
  77.     On Error GoTo ConfigError
  78.    
  79.     ' EXAMPLE RS232
  80.     ' As part of the example, we check to see if this is RS232,
  81.     ' If it is RS232 we first set the instrument to remote
  82.     '
  83.     ' We need to do a Connect once because we are using the io layer
  84.     If InStr(1, DMM.IO.ResourceName, "ASRL", vbTextCompare) > 0 Then ' the io is RS232
  85.         ' send the remote for RS232
  86.         DMM.WriteString "Syst:Rem"
  87.     End If
  88.    
  89.     ' clear the text box so we can tell when new data arrives
  90.     txtResult.Text = ""
  91.     txtResult.Refresh
  92.    
  93.     ' EXAMPLE for using the CONFigure command
  94.     With DMM
  95.         .IO.Timeout = 10000                    ' Set timeout to 10 sec to allow time to take reading
  96.         .WriteString "*RST"                     ' Reset the dmm
  97.         .WriteString "*CLS"                     ' Clear dmm status registers
  98.         .WriteString "CALC:DBM:REF 50"          ' set 50 ohm reference for dBm
  99.         ' the CONFigure command sets range and resolution for AC
  100.         ' all other AC function parameters are defaulted but can be
  101.         ' set before a READ?
  102.         .WriteString "Conf:Volt:AC 1, 0.001"     ' set dmm to 1 amp ac range"
  103.         .WriteString "Det:Band 200"             ' Select the 200 Hz (fast) ac filter
  104.         .WriteString "Trig:Coun 5"              ' dmm will accept 5 triggers
  105.         .WriteString "Trig:Sour IMM"            ' Trigger source is IMMediate
  106.         .WriteString "Calc:Func DBM"            ' Select dBm function
  107.         .WriteString "Calc:Stat ON"             ' Enable math and request operation complete
  108.         ' for fast PC's (RS232 only) add a delay before a query of > 50 ms
  109.         delay 200
  110.         .WriteString "Calc:Stat ON;*OPC?"       ' Enable math and request operation complete
  111.         status = .ReadNumber                     ' A returned value indicates dmm is ready
  112.         ' for fast PC's (RS232 only) add a delay before a query of > 50 ms
  113.         delay 200
  114.         .WriteString "Read?"                    ' Take readings; send to output buffer
  115.         Readings = .ReadList()                  ' Get readings and parse into array of doubles
  116.     End With
  117.         
  118.     ' print to Text box
  119.     txtResult.Text = ""
  120.     For i = 0 To 4
  121.         txtResult.SelText = Readings(i) & " dBm" & vbCrLf
  122.     Next i
  123.    
  124.     Exit Sub
  125.    
  126. ConfigError:
  127.     MsgBox "Error in Config: " & Err.Description
  128. End Sub

  129. Private Sub cmdSetIO_Click()
  130. ' set the I/O address to the text box in case the
  131. ' user changed it.
  132. ' bring up the input dialog and save any changes to the
  133. ' text box
  134.     Dim mgr As AgilentRMLib.SRMCls
  135.     Dim sfc As VisaComLib.ISerial

  136.     On Error GoTo ioError

  137.     m_ioAddress = InputBox("Enter the IO address of the DMM", "Set IO address", m_ioAddress)
  138.     If Len(m_ioAddress) > 5 Then
  139.         Set mgr = New AgilentRMLib.SRMCls
  140.         Set DMM = New VisaComLib.FormattedIO488
  141.         Set DMM.IO = mgr.Open(m_ioAddress)
  142.         ' Set RS232 parameters if this is RS232
  143.         If DMM.IO.HardwareInterfaceName = "ASRL" Then
  144.             Set sfc = DMM.IO
  145.             sfc.BaudRate = 9600
  146.             sfc.FlowControl = ASRL_FLOW_DTR_DSR
  147.             ' For RS232 set the termination character to LF
  148.             DMM.IO.TerminationCharacter = 10
  149.             DMM.IO.TerminationCharacterEnabled = True
  150.             ' send the remote for RS232 only
  151.             DMM.WriteString "Syst:Rem"
  152.         End If
  153.     End If

  154.     Exit Sub
  155. ioError:
  156.     MsgBox "Set IO error:" & vbCrLf & Err.Description
  157. End Sub

  158. Private Sub Form_Load()
  159.     m_ioAddress = IO_ADDRESS
  160.     cmdSetIO_Click
  161. End Sub
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则


--------------------------------------------------------------------------------------------------------------------
本站是工控技术交流站点,论坛内容均为网络收集或会员所发表,并不代表本站立场,会员拥有该内容的所有权力及责任!
本站内容如有侵犯您的版权,请按下面方式联系本站管理员,我们将及时删除处理
管理员:冰糖 QQ:5483695(请直击主题), Mail:admin#ziwai.net(#改成@) 其它非本人.
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论!

QQ|Archiver|手机版|小黑屋|紫外工控论坛. ( 苏ICP备11032118号-1 )

GMT+8, 2024-5-5 05:58 , Processed in 0.343752 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表