VERSION 5.00 Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX" Begin VB.Form FormMain AutoRedraw = -1 'True BackColor = &H00C0C0C0& Caption = "PhotoFinish" ClientHeight = 5760 ClientLeft = 165 ClientTop = 450 ClientWidth = 10830 Icon = "PhotoFinish.frx":0000 KeyPreview = -1 'True LinkTopic = "Form1" LockControls = -1 'True ScaleHeight = 5760 ScaleWidth = 10830 StartUpPosition = 3 'Windows Default Begin VB.Frame Frame10 Caption = "Singles" Height = 800 Left = 7800 TabIndex = 76 Top = 120 Width = 1200 Begin VB.Label lblSinglesB AutoSize = -1 'True Caption = "B" Height = 195 Left = 100 TabIndex = 78 ToolTipText = "Hits per second for channel B" Top = 480 Width = 105 End Begin VB.Label lblSinglesA AutoSize = -1 'True Caption = "A" Height = 195 Left = 100 TabIndex = 77 ToolTipText = "Hits per second for channel A" Top = 240 Width = 105 End End Begin VB.Timer tmrHb Enabled = 0 'False Interval = 1000 Left = 10335 Top = 0 End Begin VB.Frame Frame3 Caption = "Versions" Height = 1050 Left = 1890 TabIndex = 69 Top = 50 Width = 1380 Begin VB.Label lblGui Caption = "GUI ???" Height = 225 Left = 105 TabIndex = 72 Top = 750 Width = 1170 End Begin VB.Label lblRabbit Caption = "Rabbit ???" Height = 225 Left = 105 TabIndex = 71 Top = 500 Width = 1170 End Begin VB.Label lblFpga Caption = "FPGA: n/a" Enabled = 0 'False Height = 225 Left = 105 TabIndex = 70 Top = 250 Width = 1170 End End Begin VB.Frame Frame8 Caption = "Data Handling" Height = 1000 Left = 7710 TabIndex = 61 Top = 1575 Width = 3030 Begin VB.CommandButton cmdPath Caption = "..." BeginProperty Font Name = "Arial Narrow" Size = 12 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 300 Left = 2625 TabIndex = 67 Top = 630 Width = 300 End Begin VB.TextBox txtPath Height = 300 Left = 100 Locked = -1 'True TabIndex = 66 TabStop = 0 'False Text = "C:\" ToolTipText = "The directory to save the data files" Top = 650 Width = 2520 End Begin VB.CheckBox chkRaw Caption = "Raw (file)" Height = 255 Left = 1470 TabIndex = 65 Top = 400 Width = 1200 End Begin VB.CheckBox chkVerbose Caption = "Verbose" Height = 255 Left = 1470 TabIndex = 64 Top = 200 Width = 975 End Begin VB.CheckBox chkFile Caption = "File" Height = 225 Left = 100 TabIndex = 63 Top = 400 Width = 855 End Begin VB.CheckBox chkDisplay Caption = "Display" Height = 225 Left = 100 TabIndex = 62 Top = 200 Value = 1 'Checked Width = 855 End End Begin VB.Timer tmrKeepAlive Interval = 4500 Left = 5250 Top = 945 End Begin MSWinsockLib.Winsock Sock Left = 4830 Top = 945 _ExtentX = 741 _ExtentY = 741 _Version = 393216 End Begin VB.CommandButton cmdClear Caption = "C&lear" BeginProperty Font Name = "Arial" Size = 9.75 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 350 Left = 9075 TabIndex = 49 Top = 450 Width = 750 End Begin VB.CommandButton cmdCopy Caption = "Co&py" BeginProperty Font Name = "Arial" Size = 9.75 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 350 Left = 9075 TabIndex = 48 Top = 50 Width = 750 End Begin VB.Frame Frame7 Caption = "Tau (nS)" Height = 855 Left = 4830 TabIndex = 46 Top = 50 Width = 1110 Begin VB.TextBox TauB Alignment = 2 'Center Appearance = 0 'Flat BackColor = &H8000000F& BorderStyle = 0 'None Height = 200 Left = 650 TabIndex = 50 Text = "9.49" ToolTipText = "DAC readback: 0 to 4095" Top = 525 Width = 345 End Begin VB.TextBox TauA Alignment = 2 'Center Appearance = 0 'Flat BackColor = &H8000000F& BorderStyle = 0 'None Height = 200 Left = 150 TabIndex = 47 Text = "9.47" ToolTipText = "DAC readback: 0 to 4095" Top = 525 Width = 345 End Begin VB.Label Label4 Caption = "A B" Height = 225 Left = 255 TabIndex = 51 Top = 255 Width = 645 End End Begin VB.Frame Frame6 Caption = "Coincidences" Height = 1450 Left = 5985 TabIndex = 45 Top = 50 Width = 1740 Begin VB.CheckBox chkRun Caption = "&Run" Height = 330 Left = 105 TabIndex = 58 ToolTipText = "F5" Top = 210 Width = 960 End Begin VB.Label Label7 Alignment = 1 'Right Justify Caption = "Range" Height = 225 Left = 105 TabIndex = 60 Top = 1155 Width = 495 End Begin VB.Label lblRange Height = 225 Left = 750 TabIndex = 59 Top = 1155 Width = 540 End Begin VB.Label lblMax Height = 225 Left = 750 TabIndex = 57 Top = 945 Width = 540 End Begin VB.Label lblMin Height = 225 Left = 750 TabIndex = 56 Top = 735 Width = 540 End Begin VB.Label lblCount BeginProperty DataFormat Type = 1 Format = "#,##0" HaveTrueFalseNull= 0 FirstDayOfWeek = 0 FirstWeekOfYear = 0 LCID = 4105 SubFormatType = 1 EndProperty Height = 225 Left = 750 TabIndex = 55 Top = 525 Width = 855 End Begin VB.Label lblMaxName Alignment = 1 'Right Justify Caption = "Max" Height = 225 Left = 105 TabIndex = 54 Top = 945 Width = 495 End Begin VB.Label lblMinName Alignment = 1 'Right Justify Caption = "Min" Height = 225 Left = 105 TabIndex = 53 Top = 735 Width = 495 End Begin VB.Label Label5 Alignment = 1 'Right Justify Caption = "Counts" Height = 225 Left = 105 TabIndex = 52 Top = 525 Width = 495 End End Begin VB.Frame Frame5 Caption = "Calibrate" Height = 1485 Left = 3360 TabIndex = 36 Top = 50 Width = 1380 Begin VB.CommandButton cmdCalibrate Caption = "&Calibrate" Height = 330 Left = 210 TabIndex = 37 Top = 210 Width = 960 End Begin VB.Label Label3 AutoSize = -1 'True Caption = "B" Height = 195 Left = 100 TabIndex = 44 Top = 1155 Width = 105 End Begin VB.Label Label2 AutoSize = -1 'True Caption = "A" Height = 195 Left = 100 TabIndex = 43 Top = 945 Width = 105 End Begin VB.Label Label1 Caption = "Low High" Height = 225 Left = 420 TabIndex = 42 Top = 630 Width = 750 End Begin VB.Label lblCalBh Caption = "???" Height = 225 Left = 840 TabIndex = 41 Top = 1155 Width = 435 End Begin VB.Label lblCalBl Caption = "???" Height = 225 Left = 420 TabIndex = 40 Top = 1155 Width = 435 End Begin VB.Label lblCalAh Caption = "???" Height = 225 Left = 840 TabIndex = 39 Top = 945 Width = 435 End Begin VB.Label lblCalAl Caption = "???" Height = 225 Left = 420 TabIndex = 38 Top = 945 Width = 435 End End Begin VB.Frame Frame4 Caption = "FPGA Registers" Height = 3050 Left = 7710 TabIndex = 12 Top = 2650 Width = 3030 Begin VB.CheckBox chkAuto Caption = "&Auto" Height = 225 Left = 1100 TabIndex = 28 Top = 210 Width = 700 End Begin VB.Timer tmrAuto Enabled = 0 'False Interval = 250 Left = 1890 Top = 105 End Begin VB.CommandButton cmdReg Caption = "&Update" Height = 330 Left = 105 TabIndex = 13 Top = 210 Width = 800 End Begin VB.Label lblRegName Alignment = 1 'Right Justify Caption = "Singles, MS" Height = 195 Index = 7 Left = 105 TabIndex = 75 Top = 2700 Width = 1275 End Begin VB.Label lblReg Caption = "???" Height = 195 Index = 7 Left = 1785 TabIndex = 74 Top = 2700 Width = 450 End Begin VB.Label lblRegHex Caption = "???" Height = 195 Index = 7 Left = 2400 TabIndex = 73 Top = 2700 Width = 555 End Begin VB.Label lblRegHex Caption = "???" Height = 195 Index = 6 Left = 2400 TabIndex = 35 Top = 2400 Width = 550 End Begin VB.Label lblRegHex Caption = "???" Height = 195 Index = 5 Left = 2400 TabIndex = 34 Top = 2100 Width = 550 End Begin VB.Label lblRegHex Caption = "???" Height = 195 Index = 4 Left = 2400 TabIndex = 33 Top = 1800 Width = 550 End Begin VB.Label lblRegHex Caption = "???" Height = 195 Index = 3 Left = 2400 TabIndex = 32 Top = 1500 Width = 550 End Begin VB.Label lblRegHex Caption = "???" Height = 195 Index = 2 Left = 2400 TabIndex = 31 Top = 1200 Width = 550 End Begin VB.Label lblRegHex Caption = "???" Height = 195 Index = 1 Left = 2400 TabIndex = 30 Top = 900 Width = 550 End Begin VB.Label lblRegHex Caption = "???" Height = 195 Index = 0 Left = 2400 TabIndex = 29 Top = 600 Width = 550 End Begin VB.Label lblReg Caption = "???" Height = 195 Index = 6 Left = 1785 TabIndex = 27 Top = 2400 Width = 450 End Begin VB.Label lblRegName Alignment = 1 'Right Justify Caption = "Singles B, LS" Height = 195 Index = 6 Left = 105 TabIndex = 26 Top = 2400 Width = 1275 End Begin VB.Label lblReg Caption = "???" Height = 195 Index = 5 Left = 1785 TabIndex = 25 Top = 2100 Width = 450 End Begin VB.Label lblRegName Alignment = 1 'Right Justify Caption = "Singles A, LS" Height = 195 Index = 5 Left = 105 TabIndex = 24 Top = 2100 Width = 1275 End Begin VB.Label lblReg Caption = "???" Height = 195 Index = 4 Left = 1785 TabIndex = 23 Top = 1800 Width = 450 End Begin VB.Label lblRegName Alignment = 1 'Right Justify Caption = "ADC B" Height = 195 Index = 4 Left = 105 TabIndex = 22 Top = 1800 Width = 1275 End Begin VB.Label lblReg Caption = "???" Height = 195 Index = 3 Left = 1785 TabIndex = 21 Top = 1500 Width = 450 End Begin VB.Label lblRegName Alignment = 1 'Right Justify Caption = "ADC A" Height = 195 Index = 3 Left = 105 TabIndex = 20 Top = 1500 Width = 1275 End Begin VB.Label lblReg Caption = "???" Height = 195 Index = 2 Left = 1785 TabIndex = 19 Top = 1200 Width = 450 End Begin VB.Label lblRegName Alignment = 1 'Right Justify Caption = "Offset, OR" Height = 195 Index = 2 Left = 105 TabIndex = 18 Top = 1200 Width = 1275 End Begin VB.Label lblReg Caption = "???" Height = 195 Index = 1 Left = 1785 TabIndex = 17 Top = 900 Width = 450 End Begin VB.Label lblRegName Alignment = 1 'Right Justify Caption = "Coincidence B" Height = 195 Index = 1 Left = 105 TabIndex = 16 Top = 900 Width = 1275 End Begin VB.Label lblReg Caption = "???" Height = 195 Index = 0 Left = 1785 TabIndex = 15 Top = 600 Width = 450 End Begin VB.Label lblRegName Alignment = 1 'Right Justify Caption = "Coincidence A" Height = 200 Index = 0 Left = 105 TabIndex = 14 Top = 600 Width = 1275 End End Begin VB.CheckBox chkOnTop BackColor = &H00C0C0C0& Caption = "On Top" Height = 255 Left = 9900 TabIndex = 9 Top = 420 Width = 1020 End Begin VB.Frame Frame9 Caption = "IP Address" Height = 630 Left = 9075 TabIndex = 7 Top = 840 Width = 1365 Begin VB.TextBox txtIP Alignment = 2 'Center BackColor = &H008080FF& Height = 285 Left = 50 MaxLength = 15 TabIndex = 8 Text = "192.168.1.123" Top = 240 Width = 1200 End End Begin VB.TextBox txtMsg BeginProperty Font Name = "Fixedsys" Size = 9 Charset = 0 Weight = 400 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty Height = 3945 Left = 105 Locked = -1 'True MultiLine = -1 'True ScrollBars = 2 'Vertical TabIndex = 6 Top = 1680 Width = 7380 End Begin VB.Frame Frame2 Caption = "DAC B" Enabled = 0 'False Height = 750 Left = 105 TabIndex = 3 Top = 840 Width = 1680 Begin VB.TextBox txtVDacB Appearance = 0 'Flat BackColor = &H8000000F& BorderStyle = 0 'None Height = 200 Left = 100 Locked = -1 'True TabIndex = 11 Text = "?" ToolTipText = "DAC readback: 0 to 4095" Top = 525 Width = 765 End Begin VB.TextBox txtDacB Alignment = 2 'Center Height = 285 Left = 100 MaxLength = 4 TabIndex = 5 Text = "1500" ToolTipText = "0 to 4095" Top = 210 Width = 600 End Begin VB.CommandButton cmdDacB Caption = "Update" Enabled = 0 'False Height = 465 Left = 840 TabIndex = 4 Top = 210 Width = 735 End End Begin VB.Frame Frame1 Caption = "DAC A" Enabled = 0 'False Height = 750 Left = 105 TabIndex = 0 Top = 50 Width = 1680 Begin VB.TextBox txtVDacA Appearance = 0 'Flat BackColor = &H8000000F& BorderStyle = 0 'None Height = 200 Left = 100 Locked = -1 'True TabIndex = 10 Text = "?" ToolTipText = "DAC readback: 0 to 4095" Top = 525 Width = 765 End Begin VB.CommandButton cmdDacA Caption = "Update" Enabled = 0 'False Height = 495 Left = 840 TabIndex = 2 Top = 210 Width = 735 End Begin VB.TextBox txtDacA Alignment = 2 'Center Height = 285 Left = 100 MaxLength = 4 TabIndex = 1 Text = "1500" ToolTipText = "0 to 4095" Top = 240 Width = 600 End End Begin VB.Label lblHB AutoSize = -1 'True BackColor = &H00C0C0C0& Caption = "HB" Height = 195 Left = 10125 TabIndex = 68 Top = 105 Width = 225 End End Attribute VB_Name = "FormMain" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option Explicit DefVar A-Z #Const TEST_VERSION = False ''True 'to enable histogram test with same cable lengths. Const IP_PORT = 37829 Const KEY_SCOPE = KEY_CURRENT_USER 'select KEY_CURRENT_USER or KEY_LOCAL_MACHINE. Const KEY_PATH = "SOFTWARE\SD\PhotoFinish" 'the key name (AKA subkey path). Const ADCMIN = 650 'below this, the ADC is too close to the 10nS boundry to trust. Const ADCMAX = 2350 'above this, the ADC is too close to the 10nS boundry to trust. Const CAL_L_MIN = 450 'minimum acceptable low calibration value. Const CAL_L_MAX = 550 'maximum acceptable low calibration value. Const CAL_H_MIN = 3550 'minimum acceptable high calibration value. Const CAL_H_MAX = 3650 'maximum acceptable high calibration value. Dim LastIncoming, StartWidth, StartHeight 'minimum height and width, as set at design time. Dim DataFile #If TEST_VERSION Then Dim Td(-100 To 100) #End If Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long Private Sub chkAuto_Click() tmrAuto.Enabled = chkAuto End Sub Private Sub chkDisplay_Click() KeySave KEY_SCOPE, KEY_PATH, "Display", chkDisplay End Sub Private Sub chkFile_Click() KeySave KEY_SCOPE, KEY_PATH, "File", chkFile End Sub Private Sub chkOnTop_Click() If chkOnTop Then SetWindowPos hwnd, -1, 0, 0, 0, 0, &H3 'bring window to foreground. Else SetWindowPos hwnd, 1, 0, 0, 0, 0, &H3 'float window. ZOrder End If KeySave KEY_SCOPE, KEY_PATH, "On Top", chkOnTop End Sub Private Sub chkRaw_Click() KeySave KEY_SCOPE, KEY_PATH, "Raw data", chkRaw End Sub Private Sub chkVerbose_Click() KeySave KEY_SCOPE, KEY_PATH, "Verbose", chkVerbose End Sub Private Sub cmdCalibrate_Click() 'where the cmdCalibrate tag means: _ 0: not calibrating _ 1: calibrating low level _ 2: calibrating high level. Dim Paws, TxtTmp MousePointer = vbHourglass tmrKeepAlive.Enabled = False TxtTmp = txtMsg txtMsg = "Calibrating..." lblCalAl = "???": lblCalAh = "???": lblCalBl = "???": lblCalBh = "???" Sock.SendData "R" + Chr(0) 'stop running. DoEvents cmdCalibrate.Tag = 1 'CALIBRATE LOW VALUE. Sock.SendData "C" + Chr(1) 'calibrate low level. Sock.SendData "3" 'request register 3. Sock.SendData "4" 'request register 4. Paws = Timer While Timer < Paws + 2 And (lblCalAl = "???" Or lblCalBl = "???") DoEvents Wend cmdCalibrate.Tag = 2 'CALIBRATE HIGH VALUE. Sock.SendData "C" + Chr(3) + "34" 'calibrate high level and request registers 3 and 4. Paws = Timer While Timer < Paws + 2 And (lblCalAh = "???" Or lblCalBh = "???") DoEvents Wend cmdCalibrate.Tag = 0 'END CALIBRATION MODE. Sock.SendData "C" + Chr(0) txtMsg = txtMsg + "Done." + vbCrLf + TxtTmp MousePointer = vbDefault tmrKeepAlive.Enabled = True End Sub Private Sub cmdDacA_Click() 'Send the requested value to DAC "A". The packet's first 'byte is the command "A", the LS nibble of the second byte is data bits 11-8, (MSnibble 'of this byte is ignored), the third byte is data bits 7-0. On Local Error Resume Next 'ignore all errors, such as if lost IP connection. Dim Txt Txt = "A" 'Command to set DAC "A". Txt = Txt + Chr(Int(Val(txtDacA) / 256)) '4 MS bits. Txt = Txt + Chr(Val(txtDacA) Mod 256) 'LS byte. Sock.SendData Txt Refresh End Sub Private Sub cmdDacA_GotFocus() cmdDacA.Default = True End Sub Private Sub cmdDacA_LostFocus() cmdDacA.Default = False End Sub Private Sub cmdDacB_GotFocus() cmdDacB.Default = True End Sub Private Sub cmdPath_Click() On Local Error Resume Next 'causes error if invalid path. frmBrowseFolders.lstFolders.Path = txtPath 'get the path. frmBrowseFolders.Show vbModal On Local Error GoTo Oops If Len(frmBrowseFolders.lstFolders.Tag) > 0 Then txtPath = frmBrowseFolders.lstFolders.Path 'Use the good path. KeySave KEY_SCOPE, KEY_PATH, "File path", txtPath Unload frmBrowseFolders Else Unload frmBrowseFolders Exit Sub End If If Right(txtPath, 1) <> "\" Then txtPath = txtPath + "\" Oops: End Sub Private Sub cmdReg_Click() 'Request all FPGA registers. Dim Scan On Local Error Resume Next 'ignore all errors, such as if lost IP connection. For Scan = 0 To 7 Sock.SendData Trim(Str(Scan)) Refresh Next End Sub Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyF5 Then chkRun = (chkRun + 1) Mod 2 If Chr(KeyCode) = "R" Then chkRun = (chkRun + 1) Mod 2 If Chr(KeyCode) = "C" Then cmdCalibrate_Click If Chr(KeyCode) = "P" Then cmdCopy_Click If Chr(KeyCode) = "L" Then cmdClear_Click If Chr(KeyCode) = "U" Then cmdReg_Click If Chr(KeyCode) = "A" Then chkAuto = (chkAuto + 1) Mod 2 End Sub Private Sub Form_Load() Dim Txt As String If App.PrevInstance Then 'ONLY ONE GUI AT A TIME. MsgBox "Another PhotoFinish test GUI is already running!", vbOKCancel + vbCritical, "Oops!" End End If txtMsg = "Checking for updates" Debug.Print formVbUpdate.UpdateThisApp(0, _ "http://www.physics.utoronto.ca/~astummer/pub/mirror/Projects/Util/") txtDacA_Change 'HOUSEKEEPING. txtDacB_Change StartWidth = Width StartHeight = Height Sock.Connect txtIP, IP_PORT txtMsg = "Opening connection." + vbCrLf txtMsg.SelStart = Len(txtMsg) 'move cursor and display to end of text. cmdCalibrate.Tag = 0 'not calibrating. If KeyGet(KEY_SCOPE, KEY_PATH, "Top", Txt) Then If Val(Txt) >= 0 And Val(Txt) <= DisplaysHeight - Height Then Top = Val(Txt) End If If KeyGet(KEY_SCOPE, KEY_PATH, "Left", Txt) Then If Val(Txt) >= 0 And Val(Txt) <= DisplaysWidth - Width Then Left = Val(Txt) End If If KeyGet(KEY_SCOPE, KEY_PATH, "Height", Txt) Then If Val(Txt) >= StartHeight And Val(Txt) <= Screen.Height - Top Then Height = Val(Txt) End If On Local Error Resume Next If KeyGet(KEY_SCOPE, KEY_PATH, "On Top", Txt) Then chkOnTop = Txt If KeyGet(KEY_SCOPE, KEY_PATH, "Verbose", Txt) Then chkVerbose = Txt If KeyGet(KEY_SCOPE, KEY_PATH, "Display", Txt) Then chkDisplay = Txt If KeyGet(KEY_SCOPE, KEY_PATH, "File", Txt) Then chkFile = Txt If KeyGet(KEY_SCOPE, KEY_PATH, "Raw data", Txt) Then chkRaw = Txt If KeyGet(KEY_SCOPE, KEY_PATH, "File path", Txt) Then txtPath = Trim(Txt) lblGui = "GUI: " + Right(RevisionLabel, Len(RevisionLabel) - 4) End Sub Private Sub Form_Resize() If WindowState <> vbNormal Then Exit Sub If WindowState = vbNormal And Width <> StartWidth Then Width = StartWidth If Height < StartHeight Then Height = StartHeight txtMsg.Height = ScaleHeight - txtMsg.Top - 100 End Sub Private Sub Form_Unload(Cancel As Integer) chkRun = 0 'if running, stop. Sock.Close KeySave KEY_SCOPE, KEY_PATH, "Top", Top KeySave KEY_SCOPE, KEY_PATH, "Left", Left KeySave KEY_SCOPE, KEY_PATH, "Height", Height End End Sub Private Sub Frame6_Click() lblMinName_Click End Sub Private Sub Label5_Click() lblMinName_Click End Sub Private Sub Label7_Click() lblMinName_Click End Sub Private Sub lblCalAh_Change() lblCalAl_Change End Sub Private Sub lblCalAl_Change() ' If Val(lblCalAl) > CAL_L_MIN And Val(lblCalBl) > CAL_L_MIN _ And Val(lblCalAl) < CAL_L_MAX And Val(lblCalBl) < CAL_L_MAX _ And Val(lblCalAh) > CAL_H_MIN And Val(lblCalBh) > CAL_H_MIN _ And Val(lblCalAh) < CAL_H_MAX And Val(lblCalBh) < CAL_H_MAX Then ' chkRun.Enabled = True ' Else ' chkRun.Enabled = False ' End If End Sub Private Sub lblCalBh_Change() lblCalAl_Change End Sub Private Sub lblCalBl_Change() lblCalAl_Change End Sub Private Sub lblCount_Click() lblMinName_Click End Sub Private Sub lblGui_Click() 'Force a check for updates. Debug.Print formVbUpdate.UpdateThisApp(1, _ "http://www.physics.utoronto.ca/~astummer/pub/mirror/Projects/Util/") End Sub Private Sub lblMax_Change() lblRange = Abs(Val(lblMax) - Val(lblMin)) End Sub Private Sub lblMax_Click() lblMinName_Click End Sub Private Sub lblMaxName_Click() lblMinName_Click End Sub Private Sub lblMin_Change() lblRange = Abs(Val(lblMax) - Val(lblMin)) End Sub Private Sub lblMin_Click() lblMinName_Click End Sub Private Sub lblMinName_Click() If chkRun Then lblMin = Sock.Tag lblMax = Sock.Tag End If End Sub Private Sub lblRange_Click() lblMinName_Click End Sub Private Sub lblRegName_Click(Index As Integer) Sock.SendData Trim(Str(Index)) Refresh End Sub Private Sub cmdClear_Click() txtMsg = "" End Sub Private Sub cmdCopy_Click() Clipboard.Clear Clipboard.SetText txtMsg End Sub Private Sub chkRun_Click() 'Send the requested value to the RunEnable line. 'To calibrate, the first byte is "C", the second byte bit 0 true sets in calibration mode, 'bit 1 sets the calibration high or low (bit 1 ignored when not calibrating). On Local Error Resume Next 'ignore all errors, such as if lost IP connection. Dim Txt, P If chkRun Then Sock.SendData "R" + Chr(1) 'Set in Run mode, to collect coincidences. lblCount.Tag = 0 lblCount = 0 lblMin = "" lblMax = "" lblRange = "" If chkFile Then On Local Error GoTo BadFile Txt = "\Coincidences " + Str(Date + Time) + ".txt" While InStr(Txt, "/"): Mid(Txt, InStr(Txt, "/"), 1) = "-": Wend While InStr(Txt, ":"): Mid(Txt, InStr(Txt, ":"), 1) = ";": Wend DataFile = FreeFile Open txtPath + Txt For Output As DataFile On Local Error Resume Next End If Else Sock.SendData "R" + Chr(0) 'Stop running. Close DataFile = 0 End If #If TEST_VERSION And Not chkRun Then 'transfer the histogram to the clipboard. For P = -100 To 100 Txt = Txt + Str(Td(P)) + vbCrLf Td(P) = 0 Clipboard.Clear Clipboard.SetText Txt Next #End If Refresh chkRun.Tag = False Exit Sub BadFile: MsgBox "Oops! Bad path for saving the data files!", , Caption + " - Oops!" Close End Sub Private Sub Sock_Connect() txtMsg = "Connected." + vbCrLf + txtMsg txtMsg.SelStart = Len(txtMsg) 'move cursor to end of text. txtIP.BackColor = &H80000005 'white tmrKeepAlive.Enabled = True LastIncoming = Timer cmdCalibrate_Click Sock.SendData "V" 'get version numbers. End Sub Private Sub Sock_DataArrival(ByVal bytesTotal As Long) Dim DataIn As String, A, B, Off, DeltaT As Long, tA, tB, Coin, Singles As Long Dim Tmp As Long LastIncoming = Timer 'Update time of last incoming data. DataIn = Space(bytesTotal) Sock.GetData DataIn, , bytesTotal If Left(DataIn, 1) = "H" Then 'heartbeat, blink the "HB" label. lblHB.ForeColor = &H0 tmrHb.Enabled = True Exit Sub End If If Left(DataIn, 1) = "P" Then DataIn = Right(DataIn, Len(DataIn) - 2) 'Ignore "P" headers. While Left(DataIn, 1) = "R" And Len(DataIn) > 6 And cmdCalibrate.Tag = 0 _ And Val(lblCalAl) > CAL_L_MIN And Val(lblCalBl) > CAL_L_MIN _ And Val(lblCalAl) < CAL_L_MAX And Val(lblCalBl) < CAL_L_MAX _ And Val(lblCalAh) > CAL_H_MIN And Val(lblCalBh) > CAL_H_MIN _ And Val(lblCalAh) < CAL_H_MAX And Val(lblCalBh) < CAL_H_MAX 'COINCIDENCE REPORT. On Local Error GoTo BadData A = Str(Asc(Mid(DataIn, 2, 1)) * 256 + Asc(Mid(DataIn, 3, 1))) 'ADC A. B = Str((Asc(Mid(DataIn, 4, 1)) Mod 16) * 256 + Asc(Mid(DataIn, 5, 1))) 'ADC B. If A < ADCMAX And B < ADCMAX And A > ADCMIN And B > ADCMIN Then 'Reasonable data? tA = TauA * -1 * Log(1 - ((A - lblCalAl) / (lblCalAh - lblCalAl))) 'time A. tB = TauB * -1 * Log(1 - ((B - lblCalBl) / (lblCalBh - lblCalBl))) 'time B. Off = Str(Asc(Mid(DataIn, 6, 1))) 'offset. If Asc(Mid(DataIn, 7, 1)) = 4 Then 'A or B first? DeltaT = (tA + 10 * Off - tB) * 1000 'A before B (pS). Else DeltaT = (tB + 10 * Off - tA) * -1000 'B before A (pS). End If Sock.Tag = DeltaT 'save the latest DeltaT. #If TEST_VERSION Then Td(DeltaT) = Td(DeltaT) + 1 'bin for the histogram. If Val(lblCount.Tag) >= 100000# Then chkRun = 0 'run 100,000 coincidences then stop. #End If If chkVerbose Then Coin = Format(DeltaT, "###,##0") + " A=" + Trim(A) + " B=" + Trim(B) _ + " Offset=" + Trim(Off) _ + " A>B=" + Trim(Str(Asc(Mid(DataIn, 7, 1)) Mod 16)) Else Coin = Right(" " + Str(DeltaT), 9) '' Coin = Format(DeltaT, "###,##0") End If If (Asc(Mid(DataIn, 7, 1)) And 1) > 0 Then Coin = Coin + ", Overrange A!" 'overrange A. If (Asc(Mid(DataIn, 7, 1)) And 2) > 0 Then Coin = Coin + ", Overrange B!" If chkFile And DataFile > 0 Then If chkRaw Then Print #DataFile, Left(DataIn, 7); Else Print #DataFile, Coin End If End If If chkDisplay Then txtMsg = Coin + vbCrLf + txtMsg lblCount.Tag = lblCount.Tag + 1 lblCount = Format(lblCount.Tag, "#,###,###,##0") If lblCount.Tag = 1 Then lblMinName_Click If DeltaT < lblMin Then lblMin = DeltaT If DeltaT > lblMax Then lblMax = DeltaT End If DataIn = Right(DataIn, Len(DataIn) - 7) Wend While Left(DataIn, 1) = "V" And Len(DataIn) > 15 'FPGA AND RABBIT VERSION NUMBERS. '' lblFpga = "FPGA: " + Trim(Str(Asc(Mid(DataIn, 2, 1)))) + "." _ + Trim(Str(Int(Asc(Mid(DataIn, 3, 1)) / 16))) + "." _ + Trim(Str(Asc(Mid(DataIn, 3, 1)) Mod 16)) 'not used. lblRabbit = "Rabbit: " + Trim(Mid(DataIn, 4, 13)) DataIn = Right(DataIn, Len(DataIn) - 16) Wend While Left(DataIn, 1) = "S" And Len(DataIn) > 6 'SINGLES REPORT. Tmp = Asc(Mid(DataIn, 2, 1)) 'MS Singles = Tmp * 65536 Tmp = Asc(Mid(DataIn, 3, 1)) Singles = Singles + Tmp * 256 Singles = Singles + Asc(Mid(DataIn, 4, 1)) lblSinglesA = "A: " + Trim(Str(Singles)) Singles = Asc(Mid(DataIn, 5, 1)) Singles = Singles * 65536 Tmp = Asc(Mid(DataIn, 6, 1)) Singles = Singles + Tmp * 256 Tmp = Asc(Mid(DataIn, 7, 1)) Singles = Singles + Tmp lblSinglesB = "B: " + Trim(Str(Singles)) DataIn = Right(DataIn, Len(DataIn) - 5) Wend 'UPDATE FPGA REGISTER DISPLAY. While Left(DataIn, 1) >= "0" And Left(DataIn, 1) <= "7" And Len(DataIn) > 2 lblReg(Val(Left(DataIn, 1))) = (Asc(Mid(DataIn, 2, 1)) Mod 16) * 256 _ + Asc(Mid(DataIn, 3, 1)) 'decimal value. lblRegHex(Val(Left(DataIn, 1))) = "0x" + Hex(lblReg(Val(Left(DataIn, 1)))) 'hex value. If Val(Left(DataIn, 1)) = 3 And cmdCalibrate.Tag = 1 Then lblCalAl = lblReg(3) 'calibrations. If Val(Left(DataIn, 1)) = 3 And cmdCalibrate.Tag = 2 Then lblCalAh = lblReg(3) If Val(Left(DataIn, 1)) = 4 And cmdCalibrate.Tag = 1 Then lblCalBl = lblReg(4) If Val(Left(DataIn, 1)) = 4 And cmdCalibrate.Tag = 2 Then lblCalBh = lblReg(4) DataIn = Right(DataIn, Len(DataIn) - 3) Wend Exit Sub BadData: txtMsg = "Doh! (data) " + Err.Description + vbCrLf + txtMsg End Sub Private Sub Sock_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean) txtMsg = " Doh! (sock) " + Description + vbCrLf + txtMsg txtMsg.SelStart = Len(txtMsg) 'move cursor and display to end of text. End Sub Private Sub tmrAuto_Timer() Static Scan On Local Error Resume Next 'ignore all errors, such as if lost IP connection. Scan = Scan + 1 If Scan > 7 Then Scan = 0 Sock.SendData Trim(Str(Scan)) Refresh End Sub Private Sub tmrHb_Timer() tmrHb.Enabled = False lblHB.ForeColor = &HA0A0A0 End Sub Private Sub tmrKeepAlive_Timer() Static Msg If Sock.State = sckConnected Then Sock.SendData "H" 'CONNECTED, SEND KEEP-ALIVE "H". If LastIncoming + 5 < Timer Then 'CONNECTION TIMED OUT, NO INCOMING STATUS REPORTS. LastIncoming = 0 If Sock.State <> sckClosed And Sock.State <> sckConnecting Then 'CLOSE SOCKET. txtMsg = " Lost connection, closed. " + vbCrLf + txtMsg txtMsg.SelStart = Len(txtMsg) 'move cursor and display to end of text. Msg = True txtIP.BackColor = &H8080FF 'red lblCalAl = "???" lblCalAh = "???" lblCalBl = "???" lblCalBh = "???" chkRun = 0 chkAuto = 0 End If Sock.Close chkRun = 0 If Sock.State = sckClosed Then 'SOCKET CLOSED, TRY TO REOPEN. Sock.Connect txtIP, IP_PORT If Msg Then txtMsg = " Reopening..." + vbCrLf + txtMsg txtMsg.SelStart = Len(txtMsg) 'move cursor and display to end of text. Msg = False End If End If End If End Sub Private Sub txtDacA_Change() If Val(txtDacA) < 0 Then txtDacA = 0 If Val(txtDacA) > 2 ^ 12 - 1 Then txtDacA = 2 ^ 12 - 1 txtVDacA = Trim(Str(Int(2.5 / 5 * 1000 * 10 * Val(txtDacA) / 2 ^ 12) / 10)) + "mV" ' 2.5V, /5 attenuation, to mV, *10 for decimal place, / 12-bits, /10 back to mV. End Sub Private Sub txtDacA_GotFocus() cmdDacA.Default = True End Sub Private Sub txtDacB_Change() If Val(txtDacB) < 0 Then txtDacB = 0 If Val(txtDacB) > 2 ^ 12 - 1 Then txtDacB = 2 ^ 12 - 1 txtVDacB = Trim(Str(Int(2.5 / 5 * 1000 * 10 * Val(txtDacB) / 2 ^ 12) / 10)) + "mV" ' 2.5V, /5 attenuation, to mV, *10 for decimal place, / 12-bits, /10 back to mV. End Sub Private Sub txtDacB_GotFocus() cmdDacB.Default = True End Sub Private Sub txtMsg_KeyDown(KeyCode As Integer, Shift As Integer) If KeyCode = vbKeyEscape Then txtMsg = "" 'ESCape key clears text messages. End Sub