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

2013年(2)

2012年(23)

2011年(8)

分类: WINDOWS

2012-09-14 10:27:06


点击(此处)折叠或打开

  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 = 1 ' 默认 波特率 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.     Combo_ShowMode.Enabled = False ' 显示类型 不可用
  22.     Combo_Volt_Mode.Enabled = False ' 输入类型 不可用
  23.     
  24.     gl_3039ADMode(0).lMode = DAM3000M_CUR_N4_P20
  25.     gl_3039ADMode(1).lMode = DAM3000M_VOLT_N15_P15
  26.     gl_3039ADMode(2).lMode = DAM3000M_VOLT_N50_P50
  27.     gl_3039ADMode(3).lMode = DAM3000M_VOLT_N100_P100
  28.     gl_3039ADMode(4).lMode = DAM3000M_VOLT_N150_P150
  29.     gl_3039ADMode(5).lMode = DAM3000M_VOLT_N500_P500
  30.     gl_3039ADMode(6).lMode = DAM3000M_VOLT_N1_P1
  31.     gl_3039ADMode(7).lMode = DAM3000M_VOLT_N25_P25
  32.     gl_3039ADMode(8).lMode = DAM3000M_TMC_J
  33.     gl_3039ADMode(9).lMode = DAM3000M_TMC_K
  34.     gl_3039ADMode(10).lMode = DAM3000M_TMC_T
  35.     gl_3039ADMode(11).lMode = DAM3000M_TMC_E
  36.     gl_3039ADMode(12).lMode = DAM3000M_TMC_R
  37.     gl_3039ADMode(13).lMode = DAM3000M_TMC_S
  38.     gl_3039ADMode(14).lMode = DAM3000M_TMC_B
  39.     gl_3039ADMode(15).lMode = DAM3000M_TMC_N
  40.     gl_3039ADMode(16).lMode = DAM3000M_TMC_C
  41.     gl_3039ADMode(17).lMode = DAM3000M_TMC_WRE
  42.     
  43.     
  44.         
  45. End Sub

  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 "打开串口失败"
  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.     'bRet = DAM3000M_GetModeAD(hDevice, lDeviceID, ADMODE) ' 获得设备的输入模式
  77.     'lMode = ADMODE
  78.     'MsgBox "ADMODE " & ADMODE
  79.     
  80.     Combo_COM.Enabled = False '串口号 不可用
  81.     Combo_Baud.Enabled = False '波特率 不可用
  82.     Text_Timeout.Enabled = False '超时 不可用
  83.     Text_StartAddr.Enabled = False '设备地址 不可用
  84.     Command_CreateDevice.Enabled = False '创建设备 不可用
  85.     Command_ReleaseDevice.Enabled = True '释放设备 可用
  86.     
  87.     Command_Start.Enabled = True '开始采集 可用
  88.     Combo_ShowMode.Enabled = True '显示类型 可用
  89.     Combo_Volt_Mode.Enabled = True '输入类型 可用
  90.                 
  91. End Sub

  92. Private Sub Command_ReleaseDevice_Click()
  93.     If Not hDevice = INVALID_HANDLE_VALUE Then ' 释放设备
  94.         DAM3000M_ReleaseDevice (hDevice)
  95.         hDevice = INVALID_HANDLE_VALUE
  96.     End If
  97.     
  98.     Combo_COM.Enabled = True
  99.     Combo_Baud.Enabled = True
  100.     Text_Timeout.Enabled = True
  101.     Text_StartAddr.Enabled = True
  102.     Command_CreateDevice.Enabled = True
  103.     Command_ReleaseDevice.Enabled = False
  104.     Command_Start.Enabled = False
  105.     Combo_ShowMode.Enabled = False
  106.     Combo_Volt_Mode.Enabled = False
  107. End Sub




  108. Private Sub Command_Start_Click()

  109.     ' ----------------采集数据并保存到文件-----------------------
  110.     
  111.     ' ----------------采集数据
  112.     Dim strVal As String
  113.     Dim dfVal As Double
  114.     
  115.     Dim SelIndex As Long
  116.     SelIndex = Combo_Volt_Mode.ListIndex
  117.     ADMODE = gl_3039ADMode(SelIndex).lMode
  118.     
  119.        
  120.     If DAM3000M_SetModeAD(hDevice, 1, ADMODE) = False Then
  121.         Label5.Caption = "设置AD输入模式失败"
  122.         'MsgBox "设置AD输入模式失败"
  123.     End If
  124.     
  125.     bRet = DAM3000M_ReadDeviceAD(hDevice, lDeviceID, lADBuffer(0), 8, 0, 7)
  126.     If Combo_ShowMode.ListIndex = 0 Then
  127.       For nIndex = 0 To 7
  128.             ConvertData lADBuffer(nIndex), dfVal
  129.             strVal = dfVal
  130.             Label_value(nIndex).Caption = Format(strVal, "0.0000") ' 工作单位显示
  131.       Next nIndex
  132.     
  133.     Else
  134.       For nIndex = 0 To 7
  135.             Label_value(nIndex).Caption = lADBuffer(nIndex) ' 原码显示
  136.       Next nIndex
  137.     End If
  138.     
  139.     
  140.     ' ----------------保存到文件
  141.     Open "C:\Documents and Settings\lenovo\桌面\data.txt" For Append As #1 '打开数据文件,以追加方式
  142.     Print #1, Format(Now(), "yyyy-mm-dd hh:mm:ss"); Tab '写入当前时间
  143.     For nIndex = 0 To 7 '写入8个通道的电流值
  144.             Print #1, Label_value(nIndex).Caption; Tab
  145.     Next nIndex
  146.     Close #1

  147. End Sub

  148.     
  149. Private Sub ConvertData(ByVal lData As Long, ByRef dfVal As Double)
  150.     ' ---------------数据转换-------------------------------------
  151.     Select Case ADMODE
  152.     Case DAM3000M_CUR_N4_P20 ' 4~+20mA
  153.         dfVal = lData * 40# / 65536# + 4#
  154.     Case DAM3000M_VOLT_N15_P15 ' -15~+15mV
  155.         dfVal = lData * 30# / 65536# - 15#
  156.     Case DAM3000M_VOLT_N50_P50 ' -50~+50mV
  157.         dfVal = lData * 100 / 65536# - 50#
  158.     Case DAM3000M_VOLT_N100_P100 ' -100 ~ +100mV
  159.         dfVal = lData * 200 / 65536# - 100#
  160.     Case DAM3000M_VOLT_N150_P150 ' -150 ~ +150mV
  161.         dfVal = lData * 300 / 65536# - 150#
  162.     Case DAM3000M_VOLT_N500_P500 ' -500 ~ +500mV
  163.         dfVal = lData * 1000 / 65536# - 500#
  164.     Case DAM3000M_VOLT_N1_P1 ' -1 ~ +1V
  165.         dfVal = lData * 2# / 65536# - 1#
  166.     Case DAM3000M_VOLT_N25_P25 ' -2.5 ~ +2.5V
  167.         dfVal = lData * 5# / 65536# - 2.5
  168.     Case DAM3000M_TMC_J ' J型(0~1200℃)
  169.         dfVal = lData * 1200 / 65536#
  170.     Case DAM3000M_TMC_K ' K型(0~1300℃)
  171.         dfVal = lData * 1300 / 65536#
  172.     Case DAM3000M_TMC_T ' T型(-200~400℃)
  173.         dfVal = lData * 600 / 65536# - 200#
  174.     Case DAM3000M_TMC_E ' E型(0~1000℃)
  175.         dfVal = lData * 1000 / 65536#
  176.     Case DAM3000M_TMC_R ' R型(0~1700℃)
  177.         dfVal = lData * 1700 / 65536#
  178.     Case DAM3000M_TMC_S ' S型(0~1768℃)
  179.         dfVal = lData * 1768 / 65536#
  180.     Case DAM3000M_TMC_B ' B型(0~1800℃)
  181.         dfVal = lData * 1800 / 65536#
  182.     Case DAM3000M_TMC_N ' N型(0~1300℃)
  183.         dfVal = lData * 1300 / 65536#
  184.     Case DAM3000M_TMC_C ' C型(0~2090℃)
  185.         dfVal = lData * 2090 / 65536#
  186.     Case DAM3000M_TMC_WRE ' 钨铼5-钨铼26(0~2310℃)
  187.         dfVal = lData * 2310 / 65536#
  188.     Case Else
  189.         MsgBox "错误的输入模式"
  190.         dfVal = 8888.88
  191.     End Select

  192. End Sub

  193. Private Sub Form_Unload(Cancel As Integer)
  194.     If Not hDevice = INVALID_HANDLE_VALUE Then ' 释放设备
  195.         DAM3000M_ReleaseDevice (hDevice)
  196.         hDevice = INVALID_HANDLE_VALUE
  197.     End If
  198. End Sub

  199. '保存数据 2012年9月12日,聂添加

  200. Private Sub SaveData_Click()



  201. End Sub

  202. Private Sub Text2_Change()

  203. End Sub

  204. Private Sub moreopen_Click()
  205.     
  206.     ' ----------------创建设备--------------------------
  207.     openserial
  208.     

  209.     
  210.     '获得ADMODE的值
  211.     Dim SelIndex As Long
  212.     SelIndex = Combo_Volt_Mode.ListIndex
  213.     ADMODE = gl_3039ADMode(SelIndex).lMode
  214.     
  215.   
  216.     Open "C:\Documents and Settings\lenovo\桌面\data2.txt" For Append As #1 '打开数据文件,以追加方式
  217.     Print #1, Format(Now(), "yyyy-mm-dd hh:mm:ss"); '写入当前时间
  218.     Print #1, vbCrLf
  219.     Dim froma As Long
  220.     Dim toa As Long
  221.     
  222.     '获得起始地址和终止地址
  223.     froma = fromaddress.Text
  224.     toa = toaddress.Text
  225.     
  226.     Dim strVal As String
  227.     Dim dfVal As Double
  228.     
  229.     Dim nnindex As Long
  230.     For nnindex = froma To toa '在起始和终止地址之间循环
  231.       
  232.     bRet = DAM3000M_GetDeviceInfo(hDevice, nnindex, Info) '创建模块
  233.       
  234.     If bRet = False Then '创建模块失败
  235.         MsgBox "创建模块失败" & nnindex
  236.         Print #1, nnindex & "........."
  237.         Print #1, vbCtrlf
  238.     
  239.     Else '创建模块成功
  240.           
  241.         'bRet = DAM3000M_GetModeAD(hDevice, nnindex, ADMODE) ' 获得设备的输入模式 这句有问题
  242.           
  243.         bRet = DAM3000M_ReadDeviceAD(hDevice, nnindex, lADBuffer(0), 8, 0, 7)
  244.         Print #1, vbCrLf
  245.         Print #1, nnindex & "--------------------"
  246.         If Combo_ShowMode.ListIndex = 0 Then
  247.           
  248.           For nIndex = 0 To 7
  249.             ConvertData lADBuffer(nIndex), dfVal
  250.             strVal = dfVal
  251.             Print #1, Format(strVal, "0.0000") ' 写入工作单位显示
  252.           Next nIndex
  253.         Else
  254.           For nIndex = 0 To 7
  255.             Print #1, lADBuffer(nIndex) ' 写入原码显示
  256.           Next nIndex
  257.         End If
  258.         Print #1, "---------------------------"
  259.       End If
  260.           
  261.     Next nnindex
  262.     Close #1
  263.     
  264. End Sub

  265. Private Sub openserial()


  266.     COMID = Combo_COM.ListIndex + 1 ' 获得串口号
  267.     lBaud = Combo_Baud.ListIndex ' 获得波特率
  268.     lTimeOut = Text_Timeout.Text ' 获得超时时间
  269.     froma = fromaddress.Text ' 模块开始地址
  270.     toa = toaddress.Text ' 模块结束地址
  271.     
  272.     hDevice = DAM3000M_CreateDevice(COMID) ' 创建设备句柄
  273.     If hDevice = INVALID_HANDLE_VALUE Then
  274.         MsgBox "打开串口失败"
  275.         Exit Sub
  276.     End If
  277.     bRet = DAM3000M_InitDevice(hDevice, lBaud, True, lTimeOut) ' 初始化设备
  278.     If bRet = False Then
  279.         MsgBox "初始化设备失败"
  280.         Exit Sub
  281.     End If
  282.     
  283.     Command_ReleaseDevice.Enabled = True '释放设备 可用
  284. End Sub

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