- Dim hDevice As Long ' 设备句柄
- Dim lDeviceID As Long ' 设备地址
- Dim lADBuffer(0 To 7) As Long ' 采集数据缓冲区
- Dim lCOMID As Long ' 串口
- Dim lBaud As Long ' 波特率
- Dim bRet As Boolean
- Dim ADMODE As Long ' AD 模式
- Dim nIndex As Long
- Dim lTimeOut As Long ' 超时
- Dim Info As DAM3000M_DEVICE_INFO
- Dim gl_3039ADMode(0 To 17) As ADMODE
- Private Sub Form_Load()
- ' --------初始化变量、控件----------
- hDevice = INVALID_HANDLE_VALUE
- Combo_COM.ListIndex = 1 ' 默认 串口 2
- Combo_Baud.ListIndex = 3 ' 默认 波特率 2400
- Combo_ShowMode.ListIndex = 0 ' 默认 显示类型 工作单位
- Combo_Volt_Mode.ListIndex = 0 ' 默认 输入类型 4~ 20mA
- Command_Start.Enabled = False ' 采集 按钮不可用
- Command_ReleaseDevice.Enabled = False ' 释放 按钮不可用
-
- gl_3039ADMode(0).lMode = DAM3000M_CUR_N4_P20
- gl_3039ADMode(1).lMode = DAM3000M_VOLT_N15_P15
- gl_3039ADMode(2).lMode = DAM3000M_VOLT_N50_P50
- gl_3039ADMode(3).lMode = DAM3000M_VOLT_N100_P100
- gl_3039ADMode(4).lMode = DAM3000M_VOLT_N150_P150
- gl_3039ADMode(5).lMode = DAM3000M_VOLT_N500_P500
- gl_3039ADMode(6).lMode = DAM3000M_VOLT_N1_P1
- gl_3039ADMode(7).lMode = DAM3000M_VOLT_N25_P25
- gl_3039ADMode(8).lMode = DAM3000M_TMC_J
- gl_3039ADMode(9).lMode = DAM3000M_TMC_K
- gl_3039ADMode(10).lMode = DAM3000M_TMC_T
- gl_3039ADMode(11).lMode = DAM3000M_TMC_E
- gl_3039ADMode(12).lMode = DAM3000M_TMC_R
- gl_3039ADMode(13).lMode = DAM3000M_TMC_S
- gl_3039ADMode(14).lMode = DAM3000M_TMC_B
- gl_3039ADMode(15).lMode = DAM3000M_TMC_N
- gl_3039ADMode(16).lMode = DAM3000M_TMC_C
- gl_3039ADMode(17).lMode = DAM3000M_TMC_WRE
-
-
-
- End Sub
-
-
- Private Sub Command_CreateDevice_Click()
- ' ----------------创建设备--------------------------
- Dim lTimeOut As Long
- Dim lMode As Long
- Dim Info As DAM3000M_DEVICE_INFO
-
- COMID = Combo_COM.ListIndex + 1 ' 获得串口号
- lDeviceID = Text_StartAddr.Text ' 开始搜索地址
- lBaud = Combo_Baud.ListIndex ' 获得波特率
- lTimeOut = Text_Timeout.Text ' 获得超时时间
-
- hDevice = DAM3000M_CreateDevice(COMID) ' 创建设备句柄
- If hDevice = INVALID_HANDLE_VALUE Then
- MsgBox "打开串口失败22222"
- Exit Sub
- End If
- bRet = DAM3000M_InitDevice(hDevice, lBaud, True, lTimeOut) ' 初始化设备
- If bRet = False Then
- MsgBox "初始化设备失败"
- Exit Sub
- End If
-
- bRet = DAM3000M_GetDeviceInfo(hDevice, lDeviceID, Info)
- If bRet = False Then
- MsgBox "创建模块失败" & lDeviceID
- DAM3000M_ReleaseDevice (hDevice)
- hDevice = INVALID_HANDLE_VALUE
- Exit Sub
- End If
-
- If DAM3000M_SetModeAD(hDevice, 1, ADMODE) = False Then
- Label5.Caption = "设置AD输入模式失败"
- MsgBox "设置AD输入模式失败"
- End If
-
- Combo_COM.Enabled = False '串口号 不可用
- Combo_Baud.Enabled = False '波特率 不可用
- Text_Timeout.Enabled = False '超时 不可用
- Text_StartAddr.Enabled = False '设备地址 不可用
- Command_CreateDevice.Enabled = False '创建设备 不可用
- moreopen.Enabled = False '打开多个模块按钮 不可用
-
-
- Command_ReleaseDevice.Enabled = True '释放设备 可用
-
- Command_Start.Enabled = True '开始采集 可用
-
- End Sub
- Private Sub moreopen_Click()
-
- ' ----------------创建设备--------------------------
- openserial
-
- Command_CreateDevice.Enabled = False
- moreopen.Enabled = False
-
-
- '获得ADMODE的值
- Dim SelIndex As Long
- SelIndex = Combo_Volt_Mode.ListIndex
- ADMODE = gl_3039ADMode(SelIndex).lMode
-
- If DAM3000M_SetModeAD(hDevice, 1, ADMODE) = False Then
- Label5.Caption = "设置AD输入模式失败"
- MsgBox "设置AD输入模式失败"
- End If
-
-
-
- Open "C:\Documents and Settings\lenovo\桌面\data2.txt" For Append As #1 '打开数据文件,以追加方式
-
- Dim froma As Long
- Dim toa As Long
-
- '获得起始地址和终止地址
- froma = fromaddress.Text
- toa = toaddress.Text
-
- ProgressBar1.Max = toa
-
- Dim strVal As String
- Dim dfVal As Double
-
- Dim nnindex As Long
-
- For nnindex = froma To toa '在起始和终止地址之间循环
-
-
-
- bRet = DAM3000M_GetDeviceInfo(hDevice, nnindex, Info) '创建模块
- 'debuglabel.Caption = "模块 " & nnindex
-
- If bRet = False Then '创建模块失败
- MsgBox "创建模块失败 " & nnindex
- Print #1, "t" & Format(Now(), "yyyy-mm-dd hh:mm:ss"); '写入当前时间
- Print #1, vbCrLf
-
- Print #1, "n" & nnindex
- Print #1, vbCtrlf
-
- Else '创建模块成功
-
-
-
-
- 'bRet = DAM3000M_GetModeAD(hDevice, nnindex, ADMODE) ' 获得设备的输入模式 这句有问题
-
- bRet = DAM3000M_ReadDeviceAD(hDevice, nnindex, lADBuffer(0), 8, 0, 7)
- MsgBox "创建模块成功 " & nnindex
- Print #1, vbCrLf
- Print #1, "t" & Format(Now(), "yyyy-mm-dd hh:mm:ss"); '写入当前时间
- Print #1, vbCrLf
- Print #1, "n" & nnindex
- If Combo_ShowMode.ListIndex = 0 Then
-
- For nIndex = 0 To 7
- ConvertData lADBuffer(nIndex), dfVal
- strVal = dfVal
- Print #1, "d" & Format(strVal, "0.0000") ' 写入工作单位显示
- Next nIndex
- Else
- For nIndex = 0 To 7
- Print #1, "d" & lADBuffer(nIndex) ' 写入原码显示
- Next nIndex
- End If
- End If
- ProgressBar1.Value = nnindex
- Next nnindex
-
- Close #1
- MsgBox "扫描完毕!"
-
- End Sub
- Private Sub Command_ReleaseDevice_Click()
- If Not hDevice = INVALID_HANDLE_VALUE Then ' 释放设备
- DAM3000M_ReleaseDevice (hDevice)
- hDevice = INVALID_HANDLE_VALUE
- End If
-
- Combo_COM.Enabled = True
- Combo_Baud.Enabled = True
- Text_Timeout.Enabled = True
- Text_StartAddr.Enabled = True
- Command_CreateDevice.Enabled = True
- moreopen.Enabled = True
- Command_CreateDevice.Enabled = True
-
- Command_ReleaseDevice.Enabled = False
- Command_Start.Enabled = False
- End Sub
- Private Sub Command_Start_Click()
- ' ----------------采集数据并保存到文件-----------------------
-
- ' ----------------采集数据
- Dim strVal As String
- Dim dfVal As Double
-
- Dim SelIndex As Long
- SelIndex = Combo_Volt_Mode.ListIndex
- ADMODE = gl_3039ADMode(SelIndex).lMode
-
-
- If DAM3000M_SetModeAD(hDevice, 1, ADMODE) = False Then
- Label5.Caption = "设置AD输入模式失败"
- 'MsgBox "设置AD输入模式失败"
- End If
-
- bRet = DAM3000M_ReadDeviceAD(hDevice, lDeviceID, lADBuffer(0), 8, 0, 7)
- If Combo_ShowMode.ListIndex = 0 Then
- For nIndex = 0 To 7
- ConvertData lADBuffer(nIndex), dfVal
- strVal = dfVal
- Label_value(nIndex).Caption = Format(strVal, "0.0000") ' 工作单位显示
- Next nIndex
-
- Else
- For nIndex = 0 To 7
- Label_value(nIndex).Caption = lADBuffer(nIndex) ' 原码显示
- Next nIndex
- End If
-
-
- ' ----------------保存到文件
- Open "C:\Documents and Settings\lenovo\桌面\data.txt" For Append As #1 '打开数据文件,以追加方式
- Print #1, Format(Now(), "yyyy-mm-dd hh:mm:ss"); Tab '写入当前时间
- For nIndex = 0 To 7 '写入8个通道的电流值
- Print #1, Label_value(nIndex).Caption; Tab
- Next nIndex
- Close #1
- End Sub
-
- Private Sub ConvertData(ByVal lData As Long, ByRef dfVal As Double)
- ' ---------------数据转换-------------------------------------
- Select Case ADMODE
- Case DAM3000M_CUR_N4_P20 ' 4~+20mA
- dfVal = lData * 16# / 65536# + 4#
- Case DAM3000M_VOLT_N15_P15 ' -15~+15mV
- dfVal = lData * 30# / 65536# - 15#
- Case DAM3000M_VOLT_N50_P50 ' -50~+50mV
- dfVal = lData * 100 / 65536# - 50#
- Case DAM3000M_VOLT_N100_P100 ' -100 ~ +100mV
- dfVal = lData * 200 / 65536# - 100#
- Case DAM3000M_VOLT_N150_P150 ' -150 ~ +150mV
- dfVal = lData * 300 / 65536# - 150#
- Case DAM3000M_VOLT_N500_P500 ' -500 ~ +500mV
- dfVal = lData * 1000 / 65536# - 500#
- Case DAM3000M_VOLT_N1_P1 ' -1 ~ +1V
- dfVal = lData * 2# / 65536# - 1#
- Case DAM3000M_VOLT_N25_P25 ' -2.5 ~ +2.5V
- dfVal = lData * 5# / 65536# - 2.5
- Case DAM3000M_TMC_J ' J型(0~1200℃)
- dfVal = lData * 1200 / 65536#
- Case DAM3000M_TMC_K ' K型(0~1300℃)
- dfVal = lData * 1300 / 65536#
- Case DAM3000M_TMC_T ' T型(-200~400℃)
- dfVal = lData * 600 / 65536# - 200#
- Case DAM3000M_TMC_E ' E型(0~1000℃)
- dfVal = lData * 1000 / 65536#
- Case DAM3000M_TMC_R ' R型(0~1700℃)
- dfVal = lData * 1700 / 65536#
- Case DAM3000M_TMC_S ' S型(0~1768℃)
- dfVal = lData * 1768 / 65536#
- Case DAM3000M_TMC_B ' B型(0~1800℃)
- dfVal = lData * 1800 / 65536#
- Case DAM3000M_TMC_N ' N型(0~1300℃)
- dfVal = lData * 1300 / 65536#
- Case DAM3000M_TMC_C ' C型(0~2090℃)
- dfVal = lData * 2090 / 65536#
- Case DAM3000M_TMC_WRE ' 钨铼5-钨铼26(0~2310℃)
- dfVal = lData * 2310 / 65536#
- Case Else
- MsgBox "错误的输入模式"
- dfVal = 8888.88
- End Select
- End Sub
- Private Sub Form_Unload(Cancel As Integer)
- If Not hDevice = INVALID_HANDLE_VALUE Then ' 释放设备
- DAM3000M_ReleaseDevice (hDevice)
- hDevice = INVALID_HANDLE_VALUE
- End If
- End Sub
- Private Sub Text2_Change()
- End Sub
- Private Sub openserial()
- COMID = Combo_COM.ListIndex + 1 ' 获得串口号
- lBaud = Combo_Baud.ListIndex ' 获得波特率
- lTimeOut = Text_Timeout.Text ' 获得超时时间
-
- hDevice = DAM3000M_CreateDevice(COMID) ' 创建设备句柄
- If hDevice = INVALID_HANDLE_VALUE Then
- MsgBox "打开串口失败"
- Exit Sub
- End If
- bRet = DAM3000M_InitDevice(hDevice, lBaud, True, lTimeOut) ' 初始化设备
- If bRet = False Then
- MsgBox "初始化设备失败"
- Exit Sub
- End If
-
- Command_ReleaseDevice.Enabled = True '释放设备 可用
- End Sub
阅读(473) | 评论(0) | 转发(0) |