' LabJack_U12-first.bas -- (c) 2019-10-16, MrSwiss ' just a quick & dirty hack to show minimal use ' ' ' LabJackTest for LabJack U12. ' Code amended Digitasl I/O and AIUpdate Functions ' Adding scrolling plot to demo -- November-December, 2019, Alan J. Fridlund, ' with many thanks to MrSwiss on FreeBasic.Net forum ' and apologies for the inelegant code. Dim As Any Ptr LJ_lib ' the DLL's base address Dim as Integer GraphCol, I, LeftGraphCol, RightGraphCol, BottomScreen, TopScreen, ScreenX, ScreenY Dim As Integer MaxScans, NumChan, NumScans Dim As Integer EOState0, EOState1, EIState2, EIState3 Dim As Single LJ_Plot(8), LJ_Voltages(8), VoltTemp DIM As String FileNam, S 'Start a graphics screen, 480 x 640 here. ScreenX = 640: ScreenY = 480 SCREENRES ScreenX,ScreenY ' set size of graph for plot routine LeftGraphCol = 20 : BottomScreen = ScreenY - 10: TopScreen = 10: RightGraphCol = ScreenX - 10 # Ifdef __FB_64BIT__ LJ_lib = DylibLoad("C:\Windows\System32\ljackuw.dll") ' FBC 64 # Else LJ_lib = DylibLoad("C:\Windows\SysWOW64\ljackuw.dll") ' FBC 32 # EndIf If LJ_lib = 0 Then Print "ERROR: 'ljackuw.dll' load failed!" Beep : Sleep : End 1 End If '--------------------------------------- 'Get an analog input value. Dim LJ_AISample As Function( _ ' Declaration of Function ByVal idnum As Long Ptr, _ ' @I32 ByVal demo As Long, _ ' I32 ByVal stateIO As Long Ptr, _ ByVal updateIO As Long, _ ByVal ledOn As Long, _ ByVal numChannels As Long, _ ByVal channels As Long Ptr, _ ByVal gains As Long Ptr, _ ByVal disableCal As Long, _ ByVal overVoltage As Long Ptr, _ ByVal voltages As Single Ptr _ ' float (binary32) ) As Long LJ_AISample = DylibSymbol(LJ_lib, "AISample") ' Function Ptr If LJ_AISample = 0 Then Print "ERROR: 'LJ_AISample' load failed!" Beep : Sleep : End 2 End If '-------------------------------------- ' All these remaining functions set up following AISample example by MrSwiss - AJF. ' General Dig I/O function. Dim LJ_DigitalIO As Function( _ ' Declaration of Function ByVal Digidnum As Long Ptr, _ ' @I32 ByVal demo As Long, _ ' I32 ByVal trisIO As Long, _ ByVal stateD As Long Ptr, _ ByVal stateIO As Long Ptr, _ ByVal updateDig As Long, _ ByVal OutputD As Long Ptr _ ) As Long LJ_DigitalIO = DylibSymbol(LJ_lib, "DigitalIO") ' Function Ptr If LJ_DigitalIO = 0 Then Print "ERROR: 'LJ_DigitalIO' load failed!" Beep : Sleep : End 2 End If '-------------------------------------- ' Get state of Dig Port EIRead Dim LJ_EIDigitalIn As Function( _ ' Declaration of Function ByVal LJ_EIidnum As Long Ptr, _ ' @I32 ByVal LJ_EIdemo As Long, _ ' I32 ByVal LJ_EIchannel As Long, _ ByVal LJ_EIread As Long, _ ByVal LJ_EIstate As Long Ptr _ ) As Long LJ_EIDigitalIn = DylibSymbol(LJ_lib, "EDigitalIn") ' Function Ptr If LJ_EIDigitalIn = 0 Then Print "ERROR: 'LJ_EDigitalIn' load failed!" Beep : Sleep : End 2 End If '-------------------------------------- ' Set state of Dig Port EOWriteD Dim LJ_EODigitalOut As Function( _ ' Declaration of Function ByVal LJ_EOidnum As Long Ptr, _ ' @I32 ByVal LJ_EOdemo As Long, _ ' I32 ByVal LJ_EOchannel As Long, _ ByVal LJ_EOwriteD As Long, _ ByVal LJ_EOstate As Long _ ) As Long LJ_EODigitalOut = DylibSymbol(LJ_lib, "EDigitalOut") ' Function Ptr If LJ_EODigitalOut = 0 Then Print "ERROR: 'LJ_EODigitalOut0' load failed!" Beep : Sleep : End 2 End If '-------------------------------------- ' Nothing done with AOUpdate in the scrolling plot. - AJF Dim LJ_AOUpdate As Function( _ ' Declaration of Function ByVal AOidnum As Long Ptr, _ ' @I32 ByVal trisd As Long, _ ' I32 ByVal trisIO As Long, _ ' I32 ByVal stateD As Long Ptr, _ ByVal stateIO As Long Ptr, _ ByVal updateDig As Long, _ ByVal lngResCntr As Long, _ ByVal count As Long Ptr, _ ByVal gains As Long Ptr, _ ByVal disableCal As Long, _ ByVal AnalogOut0 As Single Ptr, _ ' float (binary32) ByVal AnalogOut1 As Single Ptr _ ' float (binary32) ) As Long LJ_AOUpdate = DylibSymbol(LJ_lib, "AOUpdate") ' Function Ptr If LJ_AOUpdate = 0 Then Print "ERROR: 'AOUpdate' load failed!" Beep : Sleep : End 2 End If 'Set up plotting screen. '------------------------------------- Color 0, 0: CLS ' Set up dark screen for scrolling plot. FileNam = "TestFile.dat" ' Just a test file name to write plotted data values. Line (LeftGraphCol - 1, TopScreen) - (RightGraphCol + 1, BottomScreen),12,B NumChan = 4 ' # of A/D Channels to sample. GraphCol = LeftGraphCol + 1 ' Set left of plot just inside box. For I = 0 to NumChan - 1 ' Channels go from 0-3. Locate I*14+8,1: Color 5-I,0 ' Color-code and label A/D Inputs 0-3. Print "Ch": Print i Next If FileNam <> "" THEN LOCATE 60, 3: Color 7,0: Print " Recording to "; color 14,0: Print Filenam; " "; OPEN Filenam for output as #1 ' Open the file for writing. End IF Locate 60,58:Color 7,0 ' Display the Dig inputs and outputs on plot. Print "O0: O1: I2: I3:" MaxScans = 1000 ' Max # of scans of A/D inputs and I/O ports before auto termination. NumScans = 0 ' Initialize loop counter for scans of A/D inputs and I/O ports. 'Begin plotting loop. '------------------------------------ Do Dim As Long LJ_idnum = -1, _ ' use @LJ_id in call (pointer) LJ_demo = 0, _ ' straight use (not a pointer) real (not demo) LJ_stateIO(0 To 3), _ ' digital pin's: 0 .. 3 (lo = 0, hi > 0) LJ_updateIO = 0, _ ' default: don't update digital IO's (output) LJ_ledOn = -1, _ ' default: ON LJ_numChannels = 4, _ ' get 4 analog channels (max.) LJ_channels(0 To 7) = { 0, 1, 2, 3, 4, 5, 6, 7 }, _ ' SE channels (2 call's needed!) LJ_gains(0 To 3), _ ' not used on SE channels only differential channels LJ_disableCal = 0, _ ' typically: OFF LJ_overVoltage(0 To 7), _ ' per call only 4 needed LJ_idx = 0 ' helper for channels selection (0 or 4) Dim As Single LJ_voltages(0 to 7) ' either @LJ_voltages(0) or @LJ_voltages(4) If LJ_AISample(@LJ_idnum, LJ_demo, @LJ_stateIO(1), LJ_updateIO, LJ_ledOn, _ LJ_numChannels, @LJ_channels(LJ_idx), @LJ_gains(0), LJ_disableCal, _ @LJ_overVoltage(LJ_idx), @LJ_voltages(LJ_idx)) > 0 Then Print "ERROR: 'LJ_AISample' call failed!" Beep : Sleep : End Else NumScans = NumScans + 1 ' Increment # of scans of A/D and Dig I/O ports. Locate 59, 33: Color 7,0: Print "Scans:";: Color 14,0: Print Numscans; Color 7,0: Print " of:";: Color 14,0: Print MaxScans; End If ' --------------------------------- ' Read Digital Ports IO2 and IO3. Dim as Long LJ_EIidnum = -1, _ LJ_EIdemo = 0, _ LJ_EIchannel = 2, _ LJ_EIread = 0, _ LJ_EIstate = 0 If LJ_EIDigitalIn(@LJ_EIidnum, LJ_EIdemo, LJ_EIchannel, LJ_EIread, @LJ_EIstate) > 0 Then Print "ERROR: 'LJ_EDigitalInCh2' load failed!" Beep : Sleep : End End If EIState2 = LJ_EIState LJ_EIChannel = 3 If LJ_EIDigitalIn(@LJ_EIidnum, LJ_EIdemo, LJ_EIchannel, LJ_EIread, @LJ_EIstate) > 0 Then Print "ERROR: 'LJ_EDigitalInCh3' load failed!" Beep : Sleep : End End If EIState3 = LJ_EIState ' --------------------------------- ' Write Digital Ports IO0 and IO1. Dim As Long LJ_EOidnum = -1, _ LJ_EOdemo = 0, _ LJ_EOchannel= 0, _ LJ_EOwriteD = 0, _ LJ_EOstate = 0 If GraphCol = 300 THEN ' Just for giggles, at middle of plot set Dig Output IO0 to 0, IO1 to 1 LJ_EOChannel = 0 LJ_EOstate = 0 If LJ_EODigitalOut(@LJ_EOidnum, LJ_EOdemo, LJ_EOchannel, LJ_EOwriteD, LJ_EOstate) > 0 Then Print "ERROR: 'LJ_EODigitalOutCh0' call failed!": Beep Else EOState0 = 0 End If LJ_EOChannel = 1 LJ_EOstate = 1 If LJ_EODigitalOut(@LJ_EOidnum, LJ_EOdemo, LJ_EOchannel, LJ_EOwriteD, LJ_EOstate) > 0 Then Print "ERROR: 'LJ_EODigitalOutCh1' call failed!": Beep Else EOState1 = 1 End If End IF If GraphCol = 21 THEN 'Just for giggles, at left of plot reset Dig Output IO0 to 1, IO1 to 0 LJ_EOChannel = 0 LJ_EOstate = 1 If LJ_EODigitalOut(@LJ_EOidnum, LJ_EOdemo, LJ_EOchannel, LJ_EOwriteD, LJ_EOstate) > 0 Then Print "ERROR: 'LJ_EODigitalOut3' call failed!": Beep Else EOState0 = 1 EndIf LJ_EOChannel = 1 LJ_EOstate = 0 If LJ_EODigitalOut(@LJ_EOidnum, LJ_EOdemo, LJ_EOchannel, LJ_EOwriteD, LJ_EOstate) > 0 Then Print "ERROR: 'LJ_EODigitalOut3' call failed!": Beep Else EOState1 = 0 EndIf End If ' Plot and optionally write values to disk. Line (Graphcol+1, 461) - (GraphCol+1, 3), 0 ' Wipe points ahead of current points to plot. If Filenam <>"" THEN If NumScans = 1 Then ' On first scan, write a file header describing data. Print #1, " Time Scan# AD0 AD1 AD2 AD3 O0 O1 I2 I3" End If Print #1, Time$; Print #1, Using "#######"; NumScans; EndIf For I = 0 TO NumChan - 1 Locate I*14+9,4: Color 5-I Print Using "+##.###";LJ_Voltages(I) If FileNam <> "" THEN Print #1, Using "+##.###"; LJ_Voltages(I); LJ_Plot (I) = LJ_Voltages(I) *275 - i*112 + 2' Fudged these gain/offset #'s for plot placement. VoltTemp = LJ_Plot(I) If VoltTemp < 11 Then VoltTemp = 11 ' Confine displayed points to plot area. If VoltTemp > 461 Then VoltTemp = 461 line (Graphcol, VoltTemp) - (GraphCol, VoltTemp-2), i+2 ' Draw data points with height*2 for thickness. Next i Color 14, 0 locate 59,61: Print Str(EOState0); ' Print Digital I/O values at bottom right of plot. locate 59,67: Print Str(EOState1); locate 59,73: Print Str(EIState2); Locate 59,79: Print Str(EIState3); If FileNam <> "" Then Print #1, Using "###"; EOState0; Print #1, Using "###"; EOState1; Print #1, Using "###"; EIState2; Print #1, Using "###"; EIState3 EndIf 'Reset to left of plot when plot reaches right side. GraphCol = GraphCol + 1: If GraphCol >= RightGraphCol then GraphCol = LeftGraphCol + 1 S = Inkey ' ----------------------------------- 'End of plotting loop. Loop Until NumScans >= MaxScans or S = chr(27) 'Keep scanning Until MaxScans or Esc keypress. ' ----------------------------------- ' close file & release loaded DLL. Close #1 DylibFree(LJ_lib) End