Option Strict Off Option Explicit On Imports VB = Microsoft.VisualBasic Imports System.Runtime.InteropServices Friend Class frmDP5 Inherits System.Windows.Forms.Form '========================================================= 'Purpose:
' Demonstrates Amptek DPP WinUSB communications to multiple DPP devices. Private Declare Sub InitCommonControls Lib "comctl32.dll" () Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Integer Public pnlStatus As StatusBarPanel 'displays status Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Integer) Private DppSerNum As Integer Private DppSnLst(10) As Integer Enum MultiUSBOpType musbt_0_SetPresets musbt_1_StartAcquisition musbt_2_MonitoringAcquisition musbt_3_SaveSpectra musbt_4_MultiUSBTestDone End Enum Private Sub cmdArmTrigger_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdArmTrigger.Click SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_ARM_DIGITAL_OSCILLOSCOPE) End Sub Private Sub cmdClearData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdClearData.Click SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_CLEAR_SPECTRUM_STATUS) End Sub Private Sub cmdClearMiscData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdClearMiscData.Click txtMiscData.Text = "" End Sub Private Function GetMcaFileCfg() As String GetMcaFileCfg = "" Dim bDelay As Boolean s.HwCfgDP5 = "" s.SpectrumCfg = True SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_FULL_READ_CONFIG_PACKET) bDelay = msDelay(50) GetMcaFileCfg = s.HwCfgDP5 End Function Private Sub SetOptMultiTest(ByVal Index As MultiUSBOpType) Dim idxTest As MultiUSBOpType For idxTest = MultiUSBOpType.musbt_0_SetPresets To MultiUSBOpType.musbt_4_MultiUSBTestDone optMultiTest(idxTest).Enabled = False optMultiTest(idxTest).Checked = False Next optMultiTest(Index).Enabled = True optMultiTest(Index).Checked = True End Sub Private Sub cmdMultiUsbTest_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdMultiUsbTest.Click Dim idxDevice As Integer Dim dtsStart As Date Dim dtsEnd As Date Dim dtsNow As Date Dim bDelay As Boolean Dim lAcqDone As Integer Dim strFilename As String = "" Dim strCfg As String = "" Dim strTag As String = "" Dim strDescription As String = "" Dim dblTimeOutDelay As Double = 0.0 cmdMultiUsbTest.Enabled = False cmdMultiUsbTest.BackColor = Color.LightSalmon LogEvent("Starting MultiUSB Test", True) LogEvent("MultiUSB Test Devices " & NumUSBDevices) For idxDevice = 1 To NumUSBDevices LogEvent("Device " & idxDevice & " SerNum:" & DppSnLst(idxDevice)) Next SetOptMultiTest(MultiUSBOpType.musbt_0_SetPresets) dtsStart = Now lblMultiUSBTestDTS.Text = (dtsStart).ToString("yyyymmdd_hhmmss") dblTimeOutDelay = Convert.ToDouble(txtPresetTime.Text) If (dblTimeOutDelay > 120.0) Then dblTimeOutDelay = 120.0 ElseIf (dblTimeOutDelay <= 0.0) Then dblTimeOutDelay = 5.0 End If dtsEnd = DateAdd("s", (Math.Truncate(Convert.ToDouble(txtPresetTime.Text)) + dblTimeOutDelay), dtsStart) If (NumUSBDevices = 0) Then Exit Sub If (CurrentUSBDevice = 0) Then Exit Sub fraSpectrumFunctions.Enabled = False 'disable controls tmrAcquisition.Enabled = False 'stop acquistion timer x1 chkDeltaMode.Checked = CheckState.Unchecked For idxDevice = 1 To NumUSBDevices If (idxDevice > NumUSBDevices) Then Exit Sub If (cmdConnectDevice(idxDevice).Enabled And Len(lblUnit(idxDevice).Text) > 0) Then cmdConnectDevice_Click((idxDevice - 1)) 'control index is one less than device index End If If (Not (DppSnLst(idxDevice) = DppSerNum)) Then cmdConnectDevice_Click((idxDevice - 1)) 'control index is one less than device index End If If (Not (DppSnLst(idxDevice) = DppSerNum)) Then LogEvent("Device Error: " & DppSnLst(idxDevice) & " != " & DppSerNum) End If LogEvent(DppSnLst(idxDevice) & " Preset Setup") SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_DISABLE_MCA_MCS) 'pause mca SendConfigToDpp("PRET=" & Trim(txtPresetTime.Text) & ";") 'send presets SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_CLEAR_SPECTRUM_STATUS) 'clear data Next SetOptMultiTest(MultiUSBOpType.musbt_1_StartAcquisition) bDelay = msDelay(1000) For idxDevice = 1 To NumUSBDevices If (idxDevice > NumUSBDevices) Then Exit Sub If (cmdConnectDevice(idxDevice).Enabled And Len(lblUnit(idxDevice).Text) > 0) Then cmdConnectDevice_Click((idxDevice - 1)) End If If (Not (DppSnLst(idxDevice) = DppSerNum)) Then cmdConnectDevice_Click((idxDevice - 1)) 'control index is one less than device index End If If (Not (DppSnLst(idxDevice) = DppSerNum)) Then LogEvent("Device Error: " & DppSnLst(idxDevice) & " != " & DppSerNum) End If LogEvent(DppSnLst(idxDevice) & " Verify Spectrum Data Cleared") cmdSingleUpdate_Click() Next bDelay = msDelay(2000) tmrAcquisition.Enabled = True 'start acquistion timer x1 For idxDevice = 1 To NumUSBDevices If (idxDevice > NumUSBDevices) Then Exit Sub If (cmdConnectDevice(idxDevice).Enabled And Len(lblUnit(idxDevice).Text) > 0) Then cmdConnectDevice_Click((idxDevice - 1)) End If If (Not (DppSnLst(idxDevice) = DppSerNum)) Then cmdConnectDevice_Click((idxDevice - 1)) 'control index is one less than device index End If If (Not (DppSnLst(idxDevice) = DppSerNum)) Then LogEvent("Device Error: " & DppSnLst(idxDevice) & " != " & DppSerNum) End If LogEvent(DppSnLst(idxDevice) & " Start Acquisition") SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_ENABLE_MCA_MCS) 'start acquisitions Next SetOptMultiTest(MultiUSBOpType.musbt_2_MonitoringAcquisition) lAcqDone = NumUSBDevices LogEvent("Monitoring Devices") Do 'monitor data until all units done or timeout cmdNextDevice_Click() bDelay = msDelay(1000) If (STATUS.MCA_EN) Then lAcqDone = NumUSBDevices LogEvent(DppSerNum & " Acquiring Data") Else lAcqDone -= 1 LogEvent(DppSerNum & " Preset Done") End If dtsNow = Now Loop While ((dtsNow < dtsEnd) And (lAcqDone > 0)) If (dtsNow.ToOADate > dtsEnd.ToOADate) Then LogEvent("Test timed out") Else LogEvent("Data Acquisition Done") End If SetOptMultiTest(MultiUSBOpType.musbt_3_SaveSpectra) If (lAcqDone = 0) Then 'save the spectrum data For idxDevice = 1 To NumUSBDevices If (idxDevice > NumUSBDevices) Then Exit Sub If (cmdConnectDevice(idxDevice).Enabled And Len(lblUnit(idxDevice).Text) > 0) Then cmdConnectDevice_Click((idxDevice - 1)) End If If (Not (DppSnLst(idxDevice) = DppSerNum)) Then bDelay = msDelay(20) cmdConnectDevice_Click((idxDevice - 1)) 'control index is one less than device index End If If (Not (DppSnLst(idxDevice) = DppSerNum)) Then LogEvent("Device Error: " & DppSnLst(idxDevice) & " != " & DppSerNum) End If LogEvent(DppSnLst(idxDevice) & " Saving Spectrum Data") bDelay = msDelay(2000) cmdSingleUpdate_Click() 'get final acquisition strFilename = (STATUS.SerialNumber).ToString() & "_" & lblMultiUSBTestDTS.Text & ".mca" strCfg = GetMcaFileCfg() If (InStr(strCfg, vbNewLine) = 0) Then ' TRY AGAIN strCfg = GetMcaFileCfg() End If strTag = Trim(txtSpectrumTag.Text) strDescription = Trim(txtSpectrumDescription.Text) SaveSpectrum(strFilename, SPECTRUM, strCfg, STATUS, strTag, strDescription, dtsStart) Next End If SetOptMultiTest(MultiUSBOpType.musbt_4_MultiUSBTestDone) LogEvent("MultiUSB Test Done") cmdMultiUsbTest.Enabled = True cmdMultiUsbTest.BackColor = Color.LightGreen End Sub Public Sub cmdNextDevice_Click() cmdNextDevice_Click(cmdNextDevice, New System.EventArgs()) End Sub Private Sub cmdNextDevice_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdNextDevice.Click Dim idxDevice As Integer Dim NextDevice As Integer If (NumUSBDevices = 0) Then Exit Sub If (CurrentUSBDevice = 0) Then Exit Sub NextDevice = CurrentUSBDevice NextDevice += 1 If (NextDevice > NumUSBDevices) Then NextDevice = 1 idxDevice = NextDevice - 1 If (cmdConnectDevice(idxDevice).Enabled And Len(lblUnit(idxDevice).Text) > 0) Then cmdConnectDevice_Click((idxDevice)) End If End Sub Private Sub SelectDevice(ByVal NextDevice As Integer) Dim idxDevice As Integer If (NumUSBDevices = 0) Then Exit Sub If (CurrentUSBDevice = 0) Then Exit Sub If (NextDevice > NumUSBDevices) Then Exit Sub idxDevice = NextDevice - 1 If (cmdConnectDevice(idxDevice).Enabled And Len(lblUnit(idxDevice).Text) > 0) Then cmdConnectDevice_Click((idxDevice)) End If End Sub Private Sub cmdReadMiscData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdReadMiscData.Click SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_512_BYTE_MISC_DATA) End Sub Private Sub cmdShowConfiguration_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdShowConfiguration.Click If (s.isDppConnected) Then s.DisplayCfg = True SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_FULL_READ_CONFIG_PACKET) End If End Sub Private Sub cmdStartAcquisition_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStartAcquisition.Click SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_ENABLE_MCA_MCS) tmrAcquisition.Enabled = True End Sub Private Sub cmdColor_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdColor.Click PlotColor = RgbToQbColor(PlotColor) End Sub Private Sub cmdDiagnostics_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdDiagnostics.Click SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_DIAGNOSTIC_DATA) End Sub Public Sub cmdSingleUpdate_Click() cmdSingleUpdate_Click(cmdSingleUpdate, New System.EventArgs()) End Sub Private Sub cmdSingleUpdate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSingleUpdate.Click tmrAcquisition.Enabled = False cmdSingleUpdate.Enabled = False If (IIf(chkDeltaMode.Checked, 1, 0) = CheckState.Checked) Then SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_CLEAR_SPECTRUM_STATUS) Else SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_SPECTRUM_STATUS) End If cmdSingleUpdate.Enabled = True End Sub Public Sub cmdUSBDeviceTest() SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_NETFINDER_PACKET) End Sub Private Sub cmdStopAcquisition_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdStopAcquisition.Click SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_DISABLE_MCA_MCS) End Sub Private Sub cmdWriteMiscData_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdWriteMiscData.Click SendCommandData(TRANSMIT_PACKET_TYPE.XMTPT_WRITE_512_BYTE_MISC_DATA, txtMiscData.Text) End Sub Private Sub Form_Initialize() On Error Resume Next LoadLibrary("shell32.dll") InitCommonControls() On Error GoTo 0 End Sub Private Sub frmDP5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load LoadApplicationSettings(Me) DppSerNum = 0 cmdMultiUsbTest.BackColor = Color.LightGreen 'InitAllEntries NfPktEntries CurrentUSBDevice = 1 NumUSBDevices = 0 s.CfgReadBack = False s.DisplayCfg = False s.SpectrumCfg = False s.Dp5CmdList = New Collection MakeDp5CmdList(s.Dp5CmdList) s.HwCfgDP5 = "" EnableDppCmdControls(False) USBDeviceTest = False lblVersion.BorderStyle = FormBorderStyle.None pnlStatus = StatusBar1.Panels.Item(1 - 1) 'set the status panel to the status bar panel Whitespace = Chr(0) + vbTab + vbLf + Chr(11) + Chr(12) + vbCr + Chr(32) PlotColor = &HFF USBDevicePathName = "" USBDeviceConnected = False pnlStatus.Text = "DP5 test application ready..." lblVersion.Text = "DP5 SDK vbDP5 v" + VB.Right(Str(My.Application.Info.Version.Major), 1) + "." + VB.Right(Str(My.Application.Info.Version.Minor), 2) cmdEnumerateDevices_Click() 'Search for devices End Sub Private Sub Form_Unload(ByRef Cancel As Short) Dim bStatusDone As Boolean If (tmrAcquisition.Enabled) Then tmrAcquisition.Enabled = False bStatusDone = msDelay(500) End If SaveApplicationSettings(Me) CloseDeviceHandle(DppWinUSB) 'CloseProgram() End Sub Private Sub frmDP5_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing Dim Cancel As Short = 0 Form_Unload(Cancel) If Cancel <> 0 Then e.Cancel = True End Sub 'Public Sub CloseProgram() ' Dim frm As Form ' For Each frm In Application.OpenForms ' frm.Close() ' Next frm 'End Sub '1440 Private Function RightJustify(ByVal strParam As String, ByVal sTxtWidth As Single) As Object Dim sTxtLenSp As Single Dim sTxtLen As Single Dim sTxtFill As Single Dim strText As String = "" Dim lSpace As Integer sTxtLen = TextWidth(Me, strParam) If (sTxtLen < sTxtWidth) Then strText = strParam sTxtLenSp = TextWidth(Me, " ") sTxtFill = sTxtWidth - sTxtLen lSpace = sTxtFill \ sTxtLenSp strText = Space(lSpace) & strText RightJustify = strText Else RightJustify = strParam End If End Function 'can also use TextWidth to format display Private Sub UpdateStatusDisplay(ByVal STATUS As Stat) Dim H As Integer, m As Integer, s As Short Dim strStatus As String = "" Dim strParam As String = "" Dim strValue As String = "" Dim sTxtWidth As Single strStatus = "" sTxtWidth = lblStatusDisplay(0).Width - TextWidth(Me, " ") strParam = strParam & RightJustify("Device Type: ", sTxtWidth) & vbNewLine strValue = STATUS.strDeviceID strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("Serial Number: ", sTxtWidth) & vbNewLine strValue = (STATUS.SerialNumber).ToString() strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("Firmware: ", sTxtWidth) & vbNewLine strValue = "v" + (Fix(STATUS.Firmware / 16)).ToString("0") + "." + (STATUS.Firmware And 15).ToString("00") strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("FPGA: ", sTxtWidth) & vbNewLine strValue = "v" + (Fix(STATUS.FPGA / 16)).ToString("0") + "." + (STATUS.FPGA And 15).ToString("00") strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("Fast Count: ", sTxtWidth) & vbNewLine strValue = (STATUS.FastCount).ToString("#,###,###,##0") strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("SlowCount: ", sTxtWidth) & vbNewLine strValue = (STATUS.SlowCount).ToString("#,###,###,##0") strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("Accumulation Time: ", sTxtWidth) & vbNewLine If STATUS.AccumulationTime < 1000 Then strValue = (STATUS.AccumulationTime).ToString("##0.000s") Else H = Fix(STATUS.AccumulationTime / 3600) m = Fix((STATUS.AccumulationTime - (H * 3600)) / 60) s = STATUS.AccumulationTime - H * 3600 - m * 60 strValue = (H).ToString("###0h ") + (m).ToString("#0m ") + (s).ToString("#0.0s") End If strStatus = strStatus & strValue & vbNewLine If (STATUS.DEVICE_ID <> DP5_DPP_TYPES.dppDP5G) Then strParam = strParam & RightJustify("Detector HV: ", sTxtWidth) & vbNewLine strValue = (STATUS.HV).ToString("###0V") ' round to nearest volt strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("Detector Temp: ", sTxtWidth) & vbNewLine strValue = (STATUS.DET_TEMP).ToString("##0°C") ' round to nearest degree strStatus = strStatus & strValue & vbNewLine End If strParam = strParam & RightJustify("DP5 Temp: ", sTxtWidth) & vbNewLine strValue = STATUS.DP5_TEMP.ToString() & "°C" strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("G.P. Counter: ", sTxtWidth) & vbNewLine strValue = STATUS.GP_COUNTER.ToString() strStatus = strStatus & strValue & vbNewLine If (STATUS.DEVICE_ID = DP5_DPP_TYPES.dppDP5) Then strParam = strParam & RightJustify("PC5 Present: ", sTxtWidth) & vbNewLine strValue = STATUS.PC5_PRESENT.ToString() strStatus = strStatus & strValue & vbNewLine If STATUS.PC5_PRESENT Then strParam = strParam & RightJustify("PC5 HV Polarity: ", sTxtWidth) & vbNewLine If (STATUS.PC5_HV_POL) Then strValue = "Positive" Else strValue = "Negative" End If strStatus = strStatus & strValue & vbNewLine strParam = strParam & RightJustify("PC5 Preamp: ", sTxtWidth) & vbNewLine If (STATUS.PC5_8_5V) Then strValue = "+/-8.5V" Else strValue = "+/-5V" End If strStatus = strStatus & strValue & vbNewLine End If strParam = strParam & RightJustify("DP5 Temp: ", sTxtWidth) & vbNewLine strValue = STATUS.DP5_TEMP.ToString() & "°C" strStatus = strStatus & strValue & vbNewLine ElseIf (STATUS.DEVICE_ID = DP5_DPP_TYPES.dppDP5G) Then strParam = strParam & RightJustify("PC5G Present: ", sTxtWidth) & vbNewLine strValue = STATUS.PC5_PRESENT.ToString() strStatus = strStatus & strValue & vbNewLine ElseIf (STATUS.DEVICE_ID = DP5_DPP_TYPES.dppPX5) Then ' STATUS.DPP_options = 1 ' STATUS.HPGe_HV_INH = True ' STATUS.HPGe_HV_INH_POL = True If STATUS.DPP_options > 0 Then '===============PX5 Options================== strParam = strParam & RightJustify("PX5 Options: ", sTxtWidth) & vbNewLine If (STATUS.DPP_options = 1) Then strValue = "HPGe HVPS" Else strValue = "Unknown" End If strStatus = strStatus & strValue & vbNewLine '===============HPGe HVPS HV Status================== strParam = strParam & RightJustify("HPGe HV: ", sTxtWidth) & vbNewLine If STATUS.HPGe_HV_INH Then strValue = "not inhibited" Else strValue = "inhibited" End If strStatus = strStatus & strValue & vbNewLine '===============HPGe HVPS Inhibit Status================== strParam = strParam & RightJustify("INH Polarity: ", sTxtWidth) & vbNewLine If STATUS.HPGe_HV_INH_POL Then strValue = "high" Else strValue = "low" End If strStatus = strStatus & strValue & vbNewLine Else strParam = strParam & RightJustify("PX5 Options: ", sTxtWidth) & vbNewLine strValue = "No Options Installed" strStatus = strStatus & strValue & vbNewLine End If End If lblStatusDisplay(0).Text = strParam lblStatusDisplay(1).Text = strStatus End Sub Private Sub picScope_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles picScope.DoubleClick picScope.Refresh() End Sub Private Sub tmrAcquisition_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrAcquisition.Tick If (IIf(chkDeltaMode.Checked, 1, 0) = CheckState.Checked) Then SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_CLEAR_SPECTRUM_STATUS) Else SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_SPECTRUM_STATUS) End If End Sub Public Sub RemCallParsePacket(ByRef PacketIn() As Byte) DppState.ReqProcess = ParsePacket(PacketIn, PIN) ParsePacketEx(PIN, DppState) End Sub Private Sub ParsePacketEx(ByVal PIN As Packet_In, ByVal DppState As DppStateType) Select Case DppState.ReqProcess Case preqProcessStatus ProcessStatusEx(PIN, DppState) Case preqProcessSpectrum ProcessSpectrumEx(PIN, DppState) Case preqProcessScopeData ProcessScopeDataEx(PIN, DppState) Case preqProcessTextData ProcessTextDataEx(PIN, DppState) '==================================================================== ' Case preqProcessScopeDataOverFlow ' ProcessScopeDataEx PIN, DppState ' 'ProcessScopeDataOverFlowEx PIN, DppState ' Case preqProcessInetSettings ' ProcessInetSettingsEx PIN, DppState Case preqProcessDiagData ProcessDiagDataEx(PIN, DppState) ' Case preqProcessHwDesc ' ProcessHwDescEx PIN, DppState Case preqProcessCfgRead ProcessCfgReadEx(PIN, DppState) Case preqProcessNetFindRead ProcessNetFindReadEx(PIN, DppState) ' Case preqProcessSCAData ' ProcessSCADataEx PIN, DppState '==================================================================== Case preqProcessAck ProcessAck(PIN.PID2) Case preqProcessError DisplayError(PIN, DppState) Case Else 'do nothing End Select End Sub Private Sub ProcessNetFindReadEx(ByVal PIN As Packet_In, ByVal DppState As DppStateType) Dim Buffer(4095) As Byte '0-4095, 4096 bytes Dim strDisplay As String = "" Dim idxNfInfo As Short For idxNfInfo = 0 To (PIN.LEN_Renamed - 1) Buffer(idxNfInfo) = PIN.DATA(idxNfInfo) Next If (CurrentUSBDevice < 1) Then Exit Sub InitEntry(NfPktEntries(CurrentUSBDevice - 1)) AddEntry(NfPktEntries(CurrentUSBDevice - 1), Buffer, 0) If (USBDeviceTest) Then USBDeviceTest = False strDisplay = EntryToStrUSB(NfPktEntries(CurrentUSBDevice - 1), "USB Device " & CurrentUSBDevice & " of " & NumUSBDevices) DppSerNum = NfPktEntries(CurrentUSBDevice - 1).SerNum rtbStatus.Text = "" rtbStatus.Text = strDisplay strNetFinder = strDisplay isNetFinderReady = True isDppFound = True pnlStatus.Text = "USB NetFind Found" + vbCrLf End If End Sub Private Sub ProcessCfgReadEx(ByVal PIN As Packet_In, ByVal DppState As DppStateType) Dim cstrRawCfgIn As String = "" 'Dim cstrCh As String Dim cstrCmdData As String = "" Dim cstrCmdD As String = "" Dim strCfg As String = "" Dim cstrDisplayCfgOut As String = "" Dim idxCfg As Integer Dim varCmd As Object Dim strHwCfgDP5 As String = "" cstrRawCfgIn = "" strHwCfgDP5 = "" ' ============================= ' === Create Raw Configuration Buffer From Hardware === For idxCfg = 0 To PIN.LEN_Renamed - 1 cstrRawCfgIn += Chr(PIN.DATA(idxCfg)) strHwCfgDP5 += Chr(PIN.DATA(idxCfg)) If (PIN.DATA(idxCfg) = Asc(";")) Then cstrRawCfgIn += vbNewLine End If Next 'MsgBox cstrRawCfgIn ' ============================= If (s.DisplayCfg) Then s.DisplayCfg = False strCfg = cstrRawCfgIn cstrDisplayCfgOut = cstrRawCfgIn For Each varCmd In s.Dp5CmdList cstrCmdD = (varCmd).ToString() If (Len(cstrCmdD) > 0) Then cstrDisplayCfgOut = ReplaceCmdDesc(cstrCmdD, cstrDisplayCfgOut) End If Next ' CopyDp5CfgToClip(strCfg,cstrDisplayCfgOut); frmDppConfigDisplay.m_strMessage = cstrDisplayCfgOut 'frmDppConfigDisplay.m_strMessage = cstrRawCfgIn frmDppConfigDisplay.m_strDelimiter = ";" LoadUnUsed(frmDppConfigDisplay) frmDppConfigDisplay.m_strTitle = "DPP Configuration" frmDppConfigDisplay.ShowDialog() 'Exit Sub ElseIf (s.CfgReadBack) Then s.CfgReadBack = False s.HwCfgDP5 = strHwCfgDP5 s.cstrRawCfgIn = cstrRawCfgIn s.HwCfgReady = True ElseIf (s.SpectrumCfg) Then s.SpectrumCfg = False cstrDisplayCfgOut = cstrRawCfgIn For Each varCmd In s.Dp5CmdList cstrCmdD = (varCmd).ToString() If (Len(cstrCmdD) > 0) Then cstrDisplayCfgOut = AppendCmdDesc(cstrCmdD, cstrDisplayCfgOut) End If Next s.HwCfgDP5 = cstrDisplayCfgOut cstrRawCfgIn = cstrRawCfgIn s.HwCfgReady = True End If s.DisplayCfg = False s.CfgReadBack = False s.SpectrumCfg = False End Sub Private Sub DisplayError(ByVal PIN As Packet_In, ByVal DppState As DppStateType) pnlStatus.Text = PID2_TextToString("Received packet", CByte(PIN.STATUS)) ' bad PID, assigned by ParsePacket If (PIN.STATUS = PID2_ACK_TYPE.PID2_ACK_PID_ERROR) Then pnlStatus.Text = "Received packet: PID1=0x" + FmtHex(PIN.PID1, 2) + ", PID2=0x" + FmtHex(PIN.PID2, 2) + ", LEN=" + PIN.LEN_Renamed.ToString() + vbCrLf End If End Sub Public Sub LoadUnUsed(ByVal frm As Control) 'placejholder End Sub Public Function TextWidth(ByVal C As Control, ByVal S As String) As Single Dim sf As SizeF = C.CreateGraphics().MeasureString(S, C.Font) 'Return sf.ToSize().Width Return sf.Width End Function Private Sub ProcessAck(ByVal PID2 As Byte) pnlStatus.Text = PID2_TextToString("ACK", PID2) End Sub Private Sub ProcessStatusEx(ByVal PIN As Packet_In, ByVal DppState As DppStateType) Dim X As Short STATUS.Initialize() For X = 0 To 63 STATUS.RAW(X) = PIN.DATA(X) Next X Process_Status(STATUS) RequestScopeData(STATUS.SCOPE_DR) UpdateStatusDisplay(STATUS) pnlStatus.Text = "Status Received" + vbCrLf End Sub Private Sub ProcessSpectrumEx(ByVal PIN As Packet_In, ByVal DppState As DppStateType) Dim X As Short SPECTRUM.Channels = 256 * (2 ^ (((PIN.PID2 - 1) And 14) \ 2)) ReDim SPECTRUM.DATA(SPECTRUM.Channels) For X = 0 To SPECTRUM.Channels - 1 SPECTRUM.DATA(X) = CLng(PIN.DATA(X * 3)) + CLng(PIN.DATA(X * 3 + 1)) * 256 + CLng(PIN.DATA(X * 3 + 2)) * 65536 Next X If (PIN.PID2 And 1) = 0 Then ' spectrum + status For X = 0 To 63 STATUS.RAW(X) = PIN.DATA(X + SPECTRUM.Channels * 3) Next X Process_Status(STATUS) RequestScopeData(STATUS.SCOPE_DR) End If Plot_Spectrum(picMCAPlot, SPECTRUM, optScaleType(0).Checked, optPlotType(1).Checked) UpdateStatusDisplay(STATUS) End Sub Private Sub ProcessScopeDataEx(ByVal PIN As Packet_In, ByVal DppState As DppStateType) Dim NextPlotColor As Color Dim X As Short For X = 0 To 2047 Scope(X) = PIN.DATA(X) Next X 'Scope_Packet_Received = True NextPlotColor = System.Drawing.Color.FromArgb(PlotColor) Plot_Scope(picScope, Scope, NextPlotColor) ArmScope() End Sub Private Sub ProcessTextDataEx(ByVal PIN As Packet_In, ByVal DppState As DppStateType) Dim X As Short txtMiscData.Text = "" For X = 0 To 511 MiscData(X) = PIN.DATA(X) txtMiscData.Text = txtMiscData.Text + Chr(MiscData(X)) Next X End Sub Private Sub ProcessDiagDataEx(ByVal PIN As Packet_In, ByVal DppState As DppStateType) Dim dd As New DiagDataType Dim strDiag As String = "" dd.Initialize() Process_Diagnostics(PIN, dd) strDiag = DiagnosticsToString(dd) txtMiscData.Text = strDiag End Sub Private Sub ArmScope() If IIf(chkAutoRearm.Checked, 1, 0) = CheckState.Checked Then SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_ARM_DIGITAL_OSCILLOSCOPE) 'Call btnArmTrigger_Click End Sub Private Sub RequestScopeData(ByVal ScopeDataReady As Boolean) If ScopeDataReady Then SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_SCOPE_DATA) End Sub Public Sub SendCommand(ByRef XmtCmd As modDP5_Protocol.TRANSMIT_PACKET_TYPE) Dim HaveBuffer As Boolean Dim SentPkt As Boolean HaveBuffer = DP5_CMD(BufferOUT, XmtCmd) If (HaveBuffer) Then SentPkt = SendPacketUSB(DppWinUSB, BufferOUT, PacketIn) If (SentPkt) Then RemCallParsePacket(PacketIn) Else tmrAcquisition.Enabled = False End If End If End Sub Public Sub SendCommandData(ByVal XmtCmd As TRANSMIT_PACKET_TYPE, ByVal DataOut As String) Dim HaveBuffer As Boolean Dim SentPkt As Boolean HaveBuffer = DP5_CMD_Data(BufferOUT, XmtCmd, DataOut) If (HaveBuffer) Then SentPkt = SendPacketUSB(DppWinUSB, BufferOUT, PacketIn) If (SentPkt) Then RemCallParsePacket(PacketIn) Else tmrAcquisition.Enabled = False End If End If End Sub Private Sub EnableDppCmdControls(ByVal EnableCmd As Boolean) '---- Communications ---------------------------- 'cmdSelectCommunications.Enabled = EnableCmd '---- Configuration ----------------------------- 'cmdEditConfiguration.Enabled = EnableCmd cmdShowConfiguration.Enabled = EnableCmd '---- Acquisition ------------------------------- cmdStartAcquisition.Enabled = EnableCmd cmdStopAcquisition.Enabled = EnableCmd cmdSingleUpdate.Enabled = EnableCmd cmdClearData.Enabled = EnableCmd 'cmdColor.Enabled = EnableCmd '---- Digital Oscilloscope ---------------------- cmdArmTrigger.Enabled = EnableCmd '---- DPP Diagnostic Data ----------------------- cmdDiagnostics.Enabled = EnableCmd '---- 512-Byte Miscellaneous Data Block --------- cmdReadMiscData.Enabled = EnableCmd cmdWriteMiscData.Enabled = EnableCmd cmdClearMiscData.Enabled = EnableCmd s.isDppConnected = EnableCmd End Sub '======================================================== Public Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click Dim strSelectedComm As String = "" strSelectedComm = "Select Communications" If (isDppFound) Then strSelectedComm = "USB - " & "WinUSB" s.isDppConnected = True Else s.isDppConnected = False End If SaveApplicationSettings(Me, True, False, True) Close() End Sub Public Sub CloseDevice() If (USBDeviceConnected) Then CloseDeviceHandle(DppWinUSB) USBDeviceConnected = False End If End Sub Public Sub cmdConnectDevice_Click(ByVal Index As Integer) cmdConnectDevice_Click(Index, cmdConnectDevice(Index), New System.EventArgs()) End Sub Private Sub cmdConnectDevice_Click(ByVal Index As Integer, ByVal sender As Object, ByVal e As System.EventArgs) If (Index < NumUSBDevices) Then CurrentUSBDevice = Index + 1 lblCurrentDevice.Text = CurrentUSBDevice FindDevice() End If End Sub Private Sub cmdConnectDevice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdConnectDevice.Click Dim Index As Short = cmdConnectDevice.GetIndex(sender) cmdConnectDevice_Click(Index, sender, e) End Sub Private Sub cmdCopyDevice_Click(ByVal Index As Integer, ByVal sender As Object, ByVal e As System.EventArgs) Clipboard.Clear() ' Clear Clipboard. Clipboard.SetText(lblUnit(Index).Text) End Sub Private Sub cmdCopyDevice_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCopyDevice.Click Dim Index As Short = cmdCopyDevice.GetIndex(sender) cmdCopyDevice_Click(Index, sender, e) End Sub Private Sub CountDevices() NumUSBDevices = CountDP5WinusbDevices() lblNumUSBDevices.Text = NumUSBDevices If (NumUSBDevices > 1) Then If (CurrentUSBDevice > NumUSBDevices) Then CurrentUSBDevice = NumUSBDevices End If Else 'disable spin and device selection CurrentUSBDevice = 1 End If End Sub Public Sub cmdEnumerateDevices_Click() cmdEnumerateDevices_Click(cmdEnumerateDevices, New System.EventArgs()) End Sub Public Sub cmdEnumerateDevices_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdEnumerateDevices.Click Dim idxDevice As Integer Dim SerNum As Integer Array.Clear(DppSnLst, 0, DppSnLst.Length) For idxDevice = 0 To (lblUnit.Count - 1) lblUnit(idxDevice).Text = "" Next rtbStatus.Text = "" For idxDevice = 1 To cmdConnectDevice.Count cmdConnectDevice(idxDevice - 1).Visible = False Next CountDevices() For idxDevice = 1 To NumUSBDevices CloseDevice() isNetFinderReady = False strNetFinder = "" CurrentUSBDevice = idxDevice lblCurrentDevice.Text = CurrentUSBDevice FindDevice() If ((idxDevice > 0) And (idxDevice < 6)) Then lblUnit(idxDevice - 1).Text = strNetFinder ' & vbNewLine ' & isNetFinderReady SerNum = NfPktEntries(idxDevice - 1).SerNum cmdConnectDevice(idxDevice - 1).Visible = True DppSnLst(idxDevice) = SerNum ''''DppSerNum = DppWinUSB.SerNum End If Next End Sub '#Const defUse_GetDeviceBySN = True #If defUse_GetDeviceBySN Then Public Sub GetDeviceBySN(ByVal DppSerNum As Integer) Dim idxDevice As Integer Dim SerNum As Integer CountDevices() For idxDevice = 1 To NumUSBDevices CloseDevice() isNetFinderReady = False strNetFinder = "" CurrentUSBDevice = idxDevice lblCurrentDevice.Text = CurrentUSBDevice FindDevice() If ((idxDevice>0) And (idxDevice<4)) Then SerNum = NfPktEntries(idxDevice-1).SerNum If (DppSerNum=SerNum) Then Exit Sub 'lblUnit(idxDevice - 1) = strNetFinder ' & vbNewLine ' & isNetFinderReady End If Next End Sub #End If ' open a specific device by index. '#Const defUse_GetDeviceByIndex = True #If defUse_GetDeviceByIndex Then Public Sub GetDeviceByIndex(ByVal idxDevice As Integer, ByVal idxMUSB As Integer) Dim SerNum As Integer Dim isDetected As Boolean isNetFinderReady = False strNetFinder = "" CurrentUSBDevice = idxDevice lblCurrentDevice.Text = CurrentUSBDevice CopyWinUsbDevice(EmptyWinUSB, DppWinUSB) USBDeviceConnected = False USBDevicePathName = "" isDetected = OpenDevice(Me.Handle.ToInt32, DppWinUSB, USBDeviceConnected, USBDevicePathName, idxDevice-1) DeviceConnectedDisplay(rtbStatus, USBDeviceConnected) DeviceConnectedDisplay(Me.rtbStatus, USBDeviceConnected) If (USBDeviceConnected) Then isDppFound = True USBDeviceTest = True Me.cmdUSBDeviceTest() SerNum = NfPktEntries(idxDevice-1).SerNum lblUnit(idxDevice-1).Text = strNetFinder ' & vbNewLine ' & isNetFinderReady DppWinUSB.idxMUSB = idxMUSB DppWinUSB.idxUSB = idxDevice DppWinUSB.SerNum = SerNum CopyWinUsbDevice(DppWinUSB, MultiWinUSB(idxMUSB)) Else isDppFound = False End If End Sub #End If '#Const defUse_Connect_Form_Load = True #If defUse_Connect_Form_Load Then Private Sub Connect_Form_Load() Dim idxCommType As Integer Dim byteSockAddr As Object Dim idxIp As Integer LoadApplicationSettings(Me, True, False, False) strCommStatus = "" isDppFound = False CountDevices() USBDeviceTest = False strCommStatus = "USB" FindDevice() DppState.mInterface = CommType.commUSB End Sub #End If '#Const defUse_Connect_Form_Paint = True #If defUse_Connect_Form_Paint Then Private Sub Connect_Form_Paint() Application.DoEvents() End Sub #End If '#Const defUse_Connect_Form_Unload = True #If defUse_Connect_Form_Unload Then Private Sub Connect_Form_Unload(ByVal Cancel As Short) btnOK_Click() End Sub #End If Public Sub FindDevice() ' Search for a specific device. Dim isDetected As Boolean 'On Error GoTo FindDeviceErr CloseDevice() isDetected = OpenDevice(DppWinUSB, USBDeviceConnected, USBDevicePathName, CurrentUSBDevice - 1) 'DeviceConnectedDisplay(rtbStatus, USBDeviceConnected) If (USBDeviceConnected) Then isDppFound = True USBDeviceTest = True cmdUSBDeviceTest() SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_STATUS) EnableDppCmdControls(True) Else DppSerNum = 0 isDppFound = False EnableDppCmdControls(False) End If Exit Sub FindDeviceErr: ProcessError(Err) End Sub 'sends a specified configuration to dpp Public Sub SendConfigToDpp(ByVal strCfg As String) s.CfgReadBack = True s.HwCfgDP5Out = strCfg If (Len(strCfg) > 0) Then SendCommand(TRANSMIT_PACKET_TYPE.XMTPT_SEND_CONFIG_PACKET_EX) End If End Sub Private Function CountDP5WinusbDevices2() As Integer Dim hKey As Integer Dim retCode As Integer Dim lRet As Integer Dim idxDP5 As Integer Dim DevicePath(MAXREGBUFFER - 1) As Byte Dim cbDevicePath As Integer Dim KeyName As String = "" Dim cbKeyName As Integer Dim ErrMsg As String Dim cbErrMsg As Integer Dim strDevicePath As String = "" Dim strMsg As String = "" Dim strValueName As String = "" Dim iRemNulls As Integer = 0 Dim strTest As String = "" TextBox1.Text = "" NumUSBDevices = 0 CountDP5WinusbDevices2 = 0 retCode = RegOpenKeyEx(HKEY_LOCAL_MACHINE, WinUSBService, 0, KEY_QUERY_VALUE, hKey) If (retCode <> ERROR_SUCCESS) Then CountDP5WinusbDevices2 = 0 Exit Function End If KeyName = Space(MAX_PATH) ' Test ALL Keys (0,1,... are device paths, Count,NextInstance,(Default) have other info) For idxDP5 = 0 To (MAXDP5S + 3) - 1 'devs + 3 other keys cbKeyName = MAX_PATH cbDevicePath = MAXREGBUFFER retCode = RegEnumValue(hKey, idxDP5, KeyName, cbKeyName, 0, REG_NONE, DevicePath(0), cbDevicePath) If (retCode = ERROR_SUCCESS) Then strValueName = Trim(KeyName) strDevicePath = ByteArrayToString(DevicePath) If (Len(strValueName) = 0) Then 'do nothing ElseIf (StrComp(VB.Left(KeyName, 5), "Count", CompareMethod.Text) = 0) Then 'do nothing ElseIf (StrComp(VB.Left(KeyName, 12), "NextInstance", CompareMethod.Text) = 0) Then 'do nothing ElseIf (StrComp(VB.Left(strDevicePath, WinUsbDP5Size), WinUsbDP5, CompareMethod.Text) = 0) Then ' DP5 device path found iRemNulls = InStr(strValueName, Chr(0)) If (iRemNulls > 0) Then strValueName = VB.Left(strValueName, iRemNulls - 1) End If strMsg = strMsg & "KeyName " & strValueName & " " iRemNulls = InStr(strDevicePath, Chr(0)) If (iRemNulls > 0) Then strDevicePath = VB.Left(strDevicePath, iRemNulls - 1) End If strMsg = strMsg & strDevicePath & vbNewLine 'TRACE("DP5 device [%d]: %s=%s\r\n", (NumUSBDevices + 1), KeyName, DevicePath); NumUSBDevices = NumUSBDevices + 1 End If ElseIf (retCode = ERROR_NO_MORE_ITEMS) Then ' no more values to read strMsg = strMsg & CStr(idxDP5) & " : " & CStr((MAXDP5S + 3) - 1) & " ERROR_NO_MORE_ITEMS" & vbNewLine Exit For Else ' error reading values cbErrMsg = MAXERRBUFFER ErrMsg = Space(MAXERRBUFFER) lRet = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, 0, retCode, 0, ErrMsg, cbErrMsg, 0) If (lRet > 0) Then ErrMsg = VB.Left(ErrMsg, lRet) Else ErrMsg = "" strMsg = strMsg & "ErrMsg " & ErrMsg & vbNewLine Exit For End If Next RegCloseKey(hKey) TextBox1.Text = strMsg & "NumUSBDevices " & NumUSBDevices & vbNewLine CountDP5WinusbDevices2 = NumUSBDevices '/* return number of devices */ End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click CountDP5WinusbDevices2() End Sub End Class