Chinaunix首页 | 论坛 | 博客
  • 博客访问: 157251
  • 博文数量: 33
  • 博客积分: 2057
  • 博客等级: 大尉
  • 技术积分: 430
  • 用 户 组: 普通用户
  • 注册时间: 2007-11-19 16:37
文章分类
文章存档

2013年(2)

2012年(23)

2011年(8)

分类: WINDOWS

2012-09-26 06:43:01


点击(此处)折叠或打开

  1. Dim hDevice As Long ' 设备句柄
  2. Dim lDeviceID As Long ' 设备地址
  3. Dim lADBuffer(0 To 7) As Long ' 采集数据缓冲区
  4. Dim lCOMID As Long ' 串口
  5. Dim lBaud As Long ' 波特率
  6. Dim bRet As Boolean
  7. Dim ADMODE As Long ' AD 模式
  8. Dim nIndex As Long
  9. Dim lTimeOut As Long ' 超时

  10. Dim Info As DAM3000M_DEVICE_INFO

  11. Dim gl_3039ADMode(0 To 17) As ADMODE


  12. Private Sub Form_Load()


  13.     ' --------初始化变量、控件----------
  14.     hDevice = INVALID_HANDLE_VALUE

  15.     Combo_COM.ListIndex = 1 ' 默认 串口 2
  16.     Combo_Baud.ListIndex = 3 ' 默认 波特率 2400
  17.     Combo_ShowMode.ListIndex = 0 ' 默认 显示类型 工作单位
  18.     Combo_Volt_Mode.ListIndex = 0 ' 默认 输入类型 4~ 20mA
  19.     Command_Start.Enabled = False ' 采集 按钮不可用
  20.     Command_ReleaseDevice.Enabled = False ' 释放 按钮不可用
  21.     
  22.     gl_3039ADMode(0).lMode = DAM3000M_CUR_N4_P20
  23.     gl_3039ADMode(1).lMode = DAM3000M_VOLT_N15_P15
  24.     gl_3039ADMode(2).lMode = DAM3000M_VOLT_N50_P50
  25.     gl_3039ADMode(3).lMode = DAM3000M_VOLT_N100_P100
  26.     gl_3039ADMode(4).lMode = DAM3000M_VOLT_N150_P150
  27.     gl_3039ADMode(5).lMode = DAM3000M_VOLT_N500_P500
  28.     gl_3039ADMode(6).lMode = DAM3000M_VOLT_N1_P1
  29.     gl_3039ADMode(7).lMode = DAM3000M_VOLT_N25_P25
  30.     gl_3039ADMode(8).lMode = DAM3000M_TMC_J
  31.     gl_3039ADMode(9).lMode = DAM3000M_TMC_K
  32.     gl_3039ADMode(10).lMode = DAM3000M_TMC_T
  33.     gl_3039ADMode(11).lMode = DAM3000M_TMC_E
  34.     gl_3039ADMode(12).lMode = DAM3000M_TMC_R
  35.     gl_3039ADMode(13).lMode = DAM3000M_TMC_S
  36.     gl_3039ADMode(14).lMode = DAM3000M_TMC_B
  37.     gl_3039ADMode(15).lMode = DAM3000M_TMC_N
  38.     gl_3039ADMode(16).lMode = DAM3000M_TMC_C
  39.     gl_3039ADMode(17).lMode = DAM3000M_TMC_WRE
  40.     

  41.   
  42.   
  43. End Sub
  44.     
  45.         

  46. Private Sub Command_CreateDevice_Click()

  47.         ' ----------------创建设备--------------------------
  48.     Dim lTimeOut As Long
  49.     Dim lMode As Long
  50.     Dim Info As DAM3000M_DEVICE_INFO
  51.     
  52.     COMID = Combo_COM.ListIndex + 1 ' 获得串口号
  53.     lDeviceID = Text_StartAddr.Text ' 开始搜索地址
  54.     lBaud = Combo_Baud.ListIndex ' 获得波特率
  55.     lTimeOut = Text_Timeout.Text ' 获得超时时间
  56.     
  57.     hDevice = DAM3000M_CreateDevice(COMID) ' 创建设备句柄
  58.     If hDevice = INVALID_HANDLE_VALUE Then
  59.         MsgBox "打开串口失败22222"
  60.         Exit Sub
  61.     End If
  62.     bRet = DAM3000M_InitDevice(hDevice, lBaud, True, lTimeOut) ' 初始化设备
  63.     If bRet = False Then
  64.         MsgBox "初始化设备失败"
  65.         Exit Sub
  66.     End If
  67.     
  68.     bRet = DAM3000M_GetDeviceInfo(hDevice, lDeviceID, Info)
  69.     If bRet = False Then
  70.         MsgBox "创建模块失败" & lDeviceID
  71.         DAM3000M_ReleaseDevice (hDevice)
  72.         hDevice = INVALID_HANDLE_VALUE
  73.         Exit Sub
  74.     End If
  75.       
  76.      If DAM3000M_SetModeAD(hDevice, 1, ADMODE) = False Then
  77.         Label5.Caption = "设置AD输入模式失败"
  78.         MsgBox "设置AD输入模式失败"
  79.     End If
  80.     
  81.     Combo_COM.Enabled = False '串口号 不可用
  82.     Combo_Baud.Enabled = False '波特率 不可用
  83.     Text_Timeout.Enabled = False '超时 不可用
  84.     Text_StartAddr.Enabled = False '设备地址 不可用
  85.     Command_CreateDevice.Enabled = False '创建设备 不可用
  86.     moreopen.Enabled = False '打开多个模块按钮 不可用
  87.     
  88.     
  89.     Command_ReleaseDevice.Enabled = True '释放设备 可用
  90.     
  91.     Command_Start.Enabled = True '开始采集 可用

  92.                 
  93. End Sub

  94. Private Sub moreopen_Click()
  95.     
  96.     ' ----------------创建设备--------------------------
  97.     openserial
  98.     
  99.     Command_CreateDevice.Enabled = False
  100.     moreopen.Enabled = False
  101.     
  102.     
  103.     '获得ADMODE的值
  104.     Dim SelIndex As Long
  105.     SelIndex = Combo_Volt_Mode.ListIndex
  106.     ADMODE = gl_3039ADMode(SelIndex).lMode
  107.     
  108.     If DAM3000M_SetModeAD(hDevice, 1, ADMODE) = False Then
  109.         Label5.Caption = "设置AD输入模式失败"
  110.         MsgBox "设置AD输入模式失败"
  111.     End If
  112.               
  113.                   
  114.   
  115.     Open "C:\Documents and Settings\lenovo\桌面\data2.txt" For Append As #1 '打开数据文件,以追加方式
  116.     
  117.     Dim froma As Long
  118.     Dim toa As Long
  119.     
  120.     '获得起始地址和终止地址
  121.     froma = fromaddress.Text
  122.     toa = toaddress.Text
  123.     
  124.     ProgressBar1.Max = toa
  125.     
  126.     Dim strVal As String
  127.     Dim dfVal As Double
  128.     
  129.     Dim nnindex As Long
  130.     
  131.     For nnindex = froma To toa '在起始和终止地址之间循环
  132.     
  133.     
  134.     
  135.      bRet = DAM3000M_GetDeviceInfo(hDevice, nnindex, Info) '创建模块
  136.      'debuglabel.Caption = "模块 " & nnindex
  137.       
  138.      If bRet = False Then '创建模块失败
  139.         MsgBox "创建模块失败 " & nnindex
  140.         Print #1, "t" & Format(Now(), "yyyy-mm-dd hh:mm:ss"); '写入当前时间
  141.         Print #1, vbCrLf
  142.         
  143.         Print #1, "n" & nnindex
  144.         Print #1, vbCtrlf

  145.     
  146.      Else '创建模块成功
  147.               
  148.               
  149.               
  150.     
  151.         'bRet = DAM3000M_GetModeAD(hDevice, nnindex, ADMODE) ' 获得设备的输入模式 这句有问题
  152.           
  153.         bRet = DAM3000M_ReadDeviceAD(hDevice, nnindex, lADBuffer(0), 8, 0, 7)
  154.         MsgBox "创建模块成功 " & nnindex
  155.         Print #1, vbCrLf
  156.         Print #1, "t" & Format(Now(), "yyyy-mm-dd hh:mm:ss"); '写入当前时间
  157.         Print #1, vbCrLf
  158.         Print #1, "n" & nnindex

  159.         If Combo_ShowMode.ListIndex = 0 Then
  160.         
  161.           For nIndex = 0 To 7
  162.             ConvertData lADBuffer(nIndex), dfVal
  163.             strVal = dfVal

  164.             Print #1, "d" & Format(strVal, "0.0000") ' 写入工作单位显示
  165.           Next nIndex
  166.         Else
  167.           For nIndex = 0 To 7
  168.             Print #1, "d" & lADBuffer(nIndex) ' 写入原码显示
  169.           Next nIndex
  170.         End If

  171.     End If
  172.       ProgressBar1.Value = nnindex
  173.     Next nnindex
  174.     
  175.     Close #1
  176.     MsgBox "扫描完毕!"
  177.     
  178. End Sub
  179. Private Sub Command_ReleaseDevice_Click()
  180.     If Not hDevice = INVALID_HANDLE_VALUE Then ' 释放设备
  181.         DAM3000M_ReleaseDevice (hDevice)
  182.         hDevice = INVALID_HANDLE_VALUE
  183.     End If
  184.     
  185.     Combo_COM.Enabled = True
  186.     Combo_Baud.Enabled = True
  187.     Text_Timeout.Enabled = True
  188.     Text_StartAddr.Enabled = True
  189.     Command_CreateDevice.Enabled = True
  190.     moreopen.Enabled = True
  191.     Command_CreateDevice.Enabled = True
  192.     
  193.     Command_ReleaseDevice.Enabled = False
  194.     Command_Start.Enabled = False
  195. End Sub




  196. Private Sub Command_Start_Click()

  197.     ' ----------------采集数据并保存到文件-----------------------
  198.     
  199.     ' ----------------采集数据
  200.     Dim strVal As String
  201.     Dim dfVal As Double
  202.     
  203.     Dim SelIndex As Long
  204.     SelIndex = Combo_Volt_Mode.ListIndex
  205.     ADMODE = gl_3039ADMode(SelIndex).lMode
  206.     
  207.        
  208.     If DAM3000M_SetModeAD(hDevice, 1, ADMODE) = False Then
  209.         Label5.Caption = "设置AD输入模式失败"
  210.         'MsgBox "设置AD输入模式失败"
  211.     End If
  212.     
  213.     bRet = DAM3000M_ReadDeviceAD(hDevice, lDeviceID, lADBuffer(0), 8, 0, 7)
  214.     If Combo_ShowMode.ListIndex = 0 Then
  215.       For nIndex = 0 To 7
  216.             ConvertData lADBuffer(nIndex), dfVal
  217.             strVal = dfVal
  218.             Label_value(nIndex).Caption = Format(strVal, "0.0000") ' 工作单位显示
  219.       Next nIndex
  220.     
  221.     Else
  222.       For nIndex = 0 To 7
  223.             Label_value(nIndex).Caption = lADBuffer(nIndex) ' 原码显示
  224.       Next nIndex
  225.     End If
  226.     
  227.     
  228.     ' ----------------保存到文件
  229.     Open "C:\Documents and Settings\lenovo\桌面\data.txt" For Append As #1 '打开数据文件,以追加方式
  230.     Print #1, Format(Now(), "yyyy-mm-dd hh:mm:ss"); Tab '写入当前时间
  231.     For nIndex = 0 To 7 '写入8个通道的电流值
  232.             Print #1, Label_value(nIndex).Caption; Tab
  233.     Next nIndex
  234.     Close #1

  235. End Sub

  236.     
  237. Private Sub ConvertData(ByVal lData As Long, ByRef dfVal As Double)
  238.     ' ---------------数据转换-------------------------------------
  239.     Select Case ADMODE
  240.     Case DAM3000M_CUR_N4_P20 ' 4~+20mA
  241.         dfVal = lData * 16# / 65536# + 4#
  242.     Case DAM3000M_VOLT_N15_P15 ' -15~+15mV
  243.         dfVal = lData * 30# / 65536# - 15#
  244.     Case DAM3000M_VOLT_N50_P50 ' -50~+50mV
  245.         dfVal = lData * 100 / 65536# - 50#
  246.     Case DAM3000M_VOLT_N100_P100 ' -100 ~ +100mV
  247.         dfVal = lData * 200 / 65536# - 100#
  248.     Case DAM3000M_VOLT_N150_P150 ' -150 ~ +150mV
  249.         dfVal = lData * 300 / 65536# - 150#
  250.     Case DAM3000M_VOLT_N500_P500 ' -500 ~ +500mV
  251.         dfVal = lData * 1000 / 65536# - 500#
  252.     Case DAM3000M_VOLT_N1_P1 ' -1 ~ +1V
  253.         dfVal = lData * 2# / 65536# - 1#
  254.     Case DAM3000M_VOLT_N25_P25 ' -2.5 ~ +2.5V
  255.         dfVal = lData * 5# / 65536# - 2.5
  256.     Case DAM3000M_TMC_J ' J型(0~1200℃)
  257.         dfVal = lData * 1200 / 65536#
  258.     Case DAM3000M_TMC_K ' K型(0~1300℃)
  259.         dfVal = lData * 1300 / 65536#
  260.     Case DAM3000M_TMC_T ' T型(-200~400℃)
  261.         dfVal = lData * 600 / 65536# - 200#
  262.     Case DAM3000M_TMC_E ' E型(0~1000℃)
  263.         dfVal = lData * 1000 / 65536#
  264.     Case DAM3000M_TMC_R ' R型(0~1700℃)
  265.         dfVal = lData * 1700 / 65536#
  266.     Case DAM3000M_TMC_S ' S型(0~1768℃)
  267.         dfVal = lData * 1768 / 65536#
  268.     Case DAM3000M_TMC_B ' B型(0~1800℃)
  269.         dfVal = lData * 1800 / 65536#
  270.     Case DAM3000M_TMC_N ' N型(0~1300℃)
  271.         dfVal = lData * 1300 / 65536#
  272.     Case DAM3000M_TMC_C ' C型(0~2090℃)
  273.         dfVal = lData * 2090 / 65536#
  274.     Case DAM3000M_TMC_WRE ' 钨铼5-钨铼26(0~2310℃)
  275.         dfVal = lData * 2310 / 65536#
  276.     Case Else
  277.         MsgBox "错误的输入模式"
  278.         dfVal = 8888.88
  279.     End Select

  280. End Sub

  281. Private Sub Form_Unload(Cancel As Integer)
  282.     If Not hDevice = INVALID_HANDLE_VALUE Then ' 释放设备
  283.         DAM3000M_ReleaseDevice (hDevice)
  284.         hDevice = INVALID_HANDLE_VALUE
  285.     End If
  286. End Sub



  287. Private Sub Text2_Change()

  288. End Sub



  289. Private Sub openserial()

  290.     COMID = Combo_COM.ListIndex + 1 ' 获得串口号
  291.     lBaud = Combo_Baud.ListIndex ' 获得波特率
  292.     lTimeOut = Text_Timeout.Text ' 获得超时时间
  293.     
  294.     hDevice = DAM3000M_CreateDevice(COMID) ' 创建设备句柄
  295.     If hDevice = INVALID_HANDLE_VALUE Then
  296.         MsgBox "打开串口失败"
  297.         Exit Sub
  298.     End If
  299.     bRet = DAM3000M_InitDevice(hDevice, lBaud, True, lTimeOut) ' 初始化设备
  300.     If bRet = False Then
  301.         MsgBox "初始化设备失败"
  302.         Exit Sub
  303.     End If
  304.     
  305.     Command_ReleaseDevice.Enabled = True '释放设备 可用

  306. End Sub

阅读(473) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~