NI-DAQmx and NI-DAQmx Base examples: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | ## INTRODUCTION | |
| 2 | 2 | ||
| 3 | 3 | National Instruments produce a wide range of [DAQ cards](http://www.ni.com/data-acquisition/multifunction/), which are generally used for acquiring and generating signals. | |
| 4 | 4 | ||
| 5 | 5 | These cards usually have a few analog inputs/outputs, digital inputs/outputs, counters and a frequency generator with hardware/software timing. Exact features (type, number, parameters) depends on the card type. | |
| 6 | 6 | ||
| 7 | 7 | NIDAQmxBase.pas and NIDAQmx.pas are provided pascal bindings to National Instruments libraries and enable control of NI DAQ cards from programs written in Free Pascal. | |
| 8 | 8 | ||
| 9 | 9 | ### What National Instruments Hardware is supported | |
| 10 | 10 | ||
| 11 | 11 | [Supported hardware list](http://www.ni.com/white-paper/6910/en/) by NI-DAQmx and NI-DAQ Driver for different operating systems. | |
| 12 | 12 | ||
| 13 | 13 | ### NI-DAQmxBase library | |
| 14 | 14 | ||
| 15 | 15 | NI-DAQmxBase library or driver is multiplatform library for Windows (Windows XP 32 bit, Windows 7 32/64 bit, Windows Vista 32/64 bit, Windows 8 32/64 bit), macOS 32/64 bit and Linux 32/64 bit (SUSE, Scientific Linux, RedHat, CentOS), but doesn't implement all device features. For example, digital input/output ports speed is limited by computer speed ~100kHz, because DMA data transfer is not supported. | |
| 16 | 16 | ||
| 17 | 17 | Download library [from here](https://www.ni.com/en-au/support/downloads/drivers/download.ni-daqmx-base.html). Read [readme file](http://download.ni.com/support/softlib/multifunction_daq/nidaqmxbase/15.0/windows/readme.htm) to find the list of supported hardware and hardware features. | |
| 18 | 18 | ||
| 19 | 19 | ```text | |
| 20 | 20 | Latest Linux version 15.0 supports 64-bit OS, but you still need to install some 32-bit library. | |
| 21 | 21 | ||
| 22 | 22 | LIST OF LIBRARIES: | |
| 23 | 23 | ||
| 24 | 24 | compat-libstdc++.i686 | |
| 25 | 25 | expat.i686 | |
| 26 | 26 | glibc.i686 | |
| 27 | 27 | glibc-devel.i686 | |
| 28 | 28 | libdrm.i686 | |
| 29 | 29 | libgcc.i686 | |
| 30 | 30 | libselinux.i686 | |
| 31 | 31 | libstdc++.i686 | |
| 32 | 32 | libX11.i686 | |
| 33 | 33 | libXau.i686 | |
| 34 | 34 | libxcb.i686 | |
| 35 | 35 | libXdamage.i686 | |
| 36 | 36 | libXext.i686 | |
| 37 | 37 | libXinerama.i686 | |
| 38 | 38 | libXfixes.i686 | |
| 39 | 39 | libXxf86vm.i686 | |
| 40 | 40 | mesa-dri-drivers.i686 | |
| 41 | 41 | mesa-libGL.i686 | |
| 42 | 42 | nss-softokn-freebl.i686 | |
| 43 | 43 | zlib.i686 | |
| 44 | 44 | ``` | |
| 45 | 45 | ||
| 46 | 46 | ### NI-DAQmx library | |
| 47 | 47 | ||
| 48 | 48 | The NI-DAQmx library is only available for Windows 7 (SP1) 32/64 bit, Windows 8.1 32/64 bit, Windows 10 32/64 bit, Windows Embedded Standard 7 (SP1), Windows Server 2008 R2 (SP1) 32/64 bit). [Download here)](https://www.ni.com/en-au/support/downloads/drivers/download.ni-daqmx.html) | |
| 49 | 49 | ||
| 50 | 50 | Read [readme file](http://download.ni.com/support/softlib/multifunction_daq/nidaqmx/9.0/readme.html) to find the list of supported hardware and hardware features. | |
| 51 | 51 | ||
| 52 | 52 | ## HOW TO IDENTIFY PRESENT HARDWARE | |
| 53 | 53 | ||
| 54 | 54 | To get list of devices present in your system Under Linux operation system use | |
| 55 | 55 | ||
| 56 | - | ||
| 57 | - | ||
| 56 | + | ```text | |
| 57 | + | 1) nilsdev utility with NIDAQmx driver installed, | |
| 58 | + | 2) lsdaq utility with NIDAQmxBase driver installed. | |
| 59 | + | ``` | |
| 58 | 60 | ||
| 59 | 61 | Under Windows use **NIMAX** utility to get device list or set device alias etc. | |
| 60 | 62 | ||
| 61 | 63 | ## Library documentation | |
| 62 | 64 | ||
| 63 | 65 | You can find description of functions and properties in NI-DAQmx and NI-DAQmx Base C reference Help: | |
| 64 | 66 | ||
| 65 | 67 | - [NI-DAQmx](http://digital.ni.com/manuals.nsf/websearch/934F66A25EB3831586257AFD00741B13?OpenDocument&seen=1) | |
| 66 | 68 | - [NI-DAQmx Base](http://digital.ni.com/manuals.nsf/websearch/C4B5B92E74F160C6862574560065F8BC) | |
| 67 | 69 | ||
| 68 | 70 | ## PASCAL bindings | |
| 69 | 71 | ||
| 70 | 72 | Your can download pascal bindings nidaqmxbase.pas and nidaqmx.pas in this forum thread [1](http://forum.lazarus.freepascal.org/index.php/topic,7894.msg161883.html#msg161883) or as a [part of the LazMer](http://sourceforge.net/p/lazmer/code/HEAD/tree/trunk/daqlibs/) project. | |
| 71 | 73 | ||
| 72 | 74 | ## FUNCTION CALLING | |
| 73 | 75 | ||
| 74 | 76 | Generally, when you need to acquire or gerenate data, call functions from **nidaqmx.pas** as follow | |
| 75 | 77 | ||
| 76 | 78 | ```text | |
| 77 | 79 | 1. Create task: DAQmxCreateTask | |
| 78 | 80 | 2. Channel create: | |
| 79 | 81 | a)DAQmxCreateDIChan/DAQmxCreateDOChan (digital input(s)/output(s)) | |
| 80 | 82 | b)DAQmxCreateAIVoltageChan/DAQmxCreateAOVoltageChan (analog input(s)/output(s)) | |
| 81 | 83 | 3. Set timing if you need hardware timimg: DAQmxCfgSamplClkTiming | |
| 82 | 84 | 4. Start task: DAQmxStartTask | |
| 83 | 85 | 5. Read or write data from/to channel: | |
| 84 | 86 | a)read data from digital port: DAQmxReadDigitalU8(8-bit port),DAQmxReadDigitalU32 (32-bit port), | |
| 85 | 87 | b)write data to digital port: DAQmxWriteDigitalU8(8-bit port),DAQmxWriteDigitalU32 (32-bit port), | |
| 86 | 88 | c)read data from analog input(s): DAQmxReadAnalogF64, | |
| 87 | 89 | d)write data to analog output(s): DAQmxWriteAnalogF64. | |
| 88 | 90 | 6. Do job | |
| 89 | 91 | 7. End of program - stop and clear task: DAQmxStopTask and DAQmxClearTask | |
| 90 | 92 | ||
| 91 | 93 | For error handling create function like ErrorCheck. | |
| 92 | 94 | ``` | |
| 93 | 95 | ||
| 94 | 96 | from **nidaqmxbase.pas** as follow | |
| 95 | 97 | ||
| 96 | 98 | ```text | |
| 97 | 99 | 1. Create task: DAQmxBaseCreateTask | |
| 98 | 100 | 2. Channel create: | |
| 99 | 101 | a)DAQmxBaseCreateDIChan/DAQmxBaseCreateDOChan (digital input(s)/output(s)) | |
| 100 | 102 | b)DAQmxBaseCreateAIVoltageChan/DAQmxBaseCreateAOVoltageChan (analog input(s)/output(s)) | |
| 101 | 103 | 3. Set timing if you need hardware timimg: DAQmxBaseCfgSamplClkTiming | |
| 102 | 104 | 4. Start task: DAQmxBaseStartTask | |
| 103 | 105 | 5. Read or write data from/to channel: | |
| 104 | 106 | a)read data from digital port: DAQmxBaseReadDigitalU8(8-bit port),DAQmxBaseReadDigitalU32 (32-bit port), | |
| 105 | 107 | b)write data to digital port: DAQmxBaseWriteDigitalU8(8-bit port),DAQmxBaseWriteDigitalU32 (32-bit port), | |
| 106 | 108 | c)read data from analog input(s): DAQmxBaseReadAnalogF64, | |
| 107 | 109 | d)write data to analog output(s): DAQmxBaseWriteAnalogF64. | |
| 108 | 110 | 6. Do job | |
| 109 | 111 | 7. End of program - stop and clear task: DAQmxBaseStopTask and DAQmxBaseClearTask | |
| 110 | 112 | ||
| 111 | 113 | For error handling create function like ErrorCheck. | |
| 112 | 114 | ``` | |
| 113 | 115 | ||
| 114 | 116 | ```text | |
| 115 | 117 | Probably, some examples need to add @ before variable, which use as function parameter!!! | |
| 116 | 118 | ``` | |
| 117 | 119 | ||
| 118 | 120 | ## NI-DAQmx examples | |
| 119 | 121 | ||
| 120 | 122 | ### DEVICE INFO | |
| 121 | 123 | ||
| 122 | 124 | #### Get device list | |
| 123 | 125 | ||
| 124 | 126 | You can check device list names using NI MAX program under Windows or nilsdev utility under Linux. | |
| 125 | 127 | ||
| 126 | 128 | ``` pascal | |
| 127 | 129 | program devicelist; | |
| 128 | 130 | ||
| 129 | 131 | {$mode objfpc}{$H+} | |
| 130 | 132 | ||
| 131 | 133 | uses | |
| 132 | 134 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 133 | 135 | cthreads, | |
| 134 | 136 | {$ENDIF}{$ENDIF} | |
| 135 | 137 | Classes,nidaqmx,SysUtils | |
| 136 | 138 | { you can add units after this }; | |
| 137 | 139 | var | |
| 138 | 140 | devicenames:array of Char; | |
| 139 | 141 | infostr:string; | |
| 140 | 142 | DeviceNamesList:TStringList; | |
| 141 | 143 | buffersize:longint; | |
| 142 | 144 | product_type:array of char; | |
| 143 | 145 | devserialnum,is_simulated:longint; | |
| 144 | 146 | is_simulated_str:string; | |
| 145 | 147 | i:byte; | |
| 146 | 148 | begin | |
| 147 | 149 | DeviceNamesList:=TStringList.Create; | |
| 148 | 150 | try | |
| 149 | 151 | {************************************************************************** | |
| 150 | 152 | Get device names present in the system | |
| 151 | 153 | **************************************************************************} | |
| 152 | 154 | //get buffer size or length of string of device list | |
| 153 | 155 | buffersize:=DAQmxGetSystemInfoAttribute(DAQmx_Sys_DevNames,devicenames[0],[0]); | |
| 154 | 156 | //set array of Char lenght | |
| 155 | 157 | if Buffersize<>0 then begin | |
| 156 | 158 | writeln('-------------------------------------------------------------'); | |
| 157 | 159 | writeln('|Device name | Product Type | # Serial number | Is Simulated?'); | |
| 158 | 160 | writeln('-------------------------------------------------------------'); | |
| 159 | 161 | SetLength(devicenames,buffersize); | |
| 160 | 162 | //get the device list | |
| 161 | 163 | DAQmxGetSystemInfoAttribute(DAQmx_Sys_DevNames,devicenames[0],[buffersize]); | |
| 162 | 164 | //convert it to string: names are separated by comma | |
| 163 | 165 | SetString(infostr,PChar(@devicenames[0]),buffersize); | |
| 164 | 166 | //add names to TStringList | |
| 165 | 167 | DeviceNamesList.CommaText:=infostr; | |
| 166 | 168 | {************************************************************************ | |
| 167 | 169 | Get Product info | |
| 168 | 170 | ***********************************************************************} | |
| 169 | 171 | for i:=0 to DeviceNamesList.Count-1 do begin | |
| 170 | 172 | //get buffer size or length of string of product type | |
| 171 | 173 | buffersize:=DAQmxGetDeviceAttribute(@DeviceNamesList.Strings[i][1],DAQmx_Dev_ProductType,Nil); | |
| 172 | 174 | //set lenght of array of char | |
| 173 | 175 | SetLength(product_type,buffersize); | |
| 174 | 176 | //get product type info | |
| 175 | 177 | DAQmxGetDeviceAttribute(@DeviceNamesList.Strings[i][1],DAQmx_Dev_ProductType,product_type[0],[buffersize]); | |
| 176 | 178 | {*********************************************************************** | |
| 177 | 179 | Get Serial Number | |
| 178 | 180 | **********************************************************************} | |
| 179 | 181 | DAQmxGetDeviceAttribute(@DeviceNamesList.Strings[i][1],DAQmx_Dev_SerialNum,devserialnum,[1]); | |
| 180 | 182 | //DeviceInfoGrid.Cells[3,i+1]:=IntToHex(devserialnum,8); | |
| 181 | 183 | {*********************************************************************** | |
| 182 | 184 | Is simulated? | |
| 183 | 185 | ***********************************************************************} | |
| 184 | 186 | DAQmxGetDeviceAttribute(@DeviceNamesList.Strings[i][1],DAQmx_Dev_IsSimulated,is_simulated,[1]); | |
| 185 | 187 | if is_simulated = 0 then is_simulated_str:='No' | |
| 186 | 188 | else is_simulated_str:='Yes'; | |
| 187 | 189 | //Show info | |
| 188 | 190 | writeln('| ',DeviceNamesList.Strings[i],' | ',PChar(@product_type[0]),' | $',IntToHex(devserialnum,8),' |',is_simulated_str); | |
| 189 | 191 | end; | |
| 190 | 192 | end | |
| 191 | 193 | else writeln('any device found'); | |
| 192 | 194 | finally | |
| 193 | 195 | DeviceNamesList.Free; | |
| 194 | 196 | end; | |
| 195 | 197 | end. | |
| 196 | 198 | ``` | |
| 197 | 199 | ||
| 198 | 200 | Output example | |
| 199 | 201 | ||
| 200 | 202 | ```text | |
| 201 | 203 | ------------------------------------------------------------- | |
| 202 | 204 | |Device name | Product Type | # Serial number | Is Simulated? | |
| 203 | 205 | ------------------------------------------------------------- | |
| 204 | 206 | | Dev1 | PCI-6250 | $018D0B90 |No | |
| 205 | 207 | ``` | |
| 206 | 208 | ||
| 207 | 209 | #### Get channel names | |
| 208 | 210 | ||
| 209 | 211 | ``` pascal | |
| 210 | 212 | program chaninfo; | |
| 211 | 213 | ||
| 212 | 214 | {$mode objfpc}{$H+} | |
| 213 | 215 | ||
| 214 | 216 | uses | |
| 215 | 217 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 216 | 218 | cthreads, | |
| 217 | 219 | {$ENDIF}{$ENDIF} | |
| 218 | 220 | Classes,nidaqmx | |
| 219 | 221 | { you can add units after this }; | |
| 220 | 222 | var | |
| 221 | 223 | buffersize,infotype:longint; | |
| 222 | 224 | channelnames:array of char; | |
| 223 | 225 | MsgChannelNames:array [0..255] of char; | |
| 224 | 226 | MsgNoChannelsText:array [0..255] of char; | |
| 225 | 227 | channeltype:byte; | |
| 226 | 228 | DeviceName:string; | |
| 227 | 229 | begin | |
| 228 | 230 | {**********INPUT Device name and interested channel type ********************** | |
| 229 | 231 | **********WITHOUT CHEKING IF DEVICE EXITS IN THE SYSTEM**********************} | |
| 230 | 232 | write('Enter device name, for example Dev1, Dev2 etc.: '); | |
| 231 | 233 | readln(Devicename); | |
| 232 | 234 | writeln('0 - analog inputs'); | |
| 233 | 235 | writeln('1 - analog outputs'); | |
| 234 | 236 | writeln('2 - digital inputs'); | |
| 235 | 237 | writeln('3 - digital outputs'); | |
| 236 | 238 | writeln('4 - counter inputs'); | |
| 237 | 239 | writeln('5 - counter outputs'); | |
| 238 | 240 | write('choose the number of required channel type:'); | |
| 239 | 241 | readln(channeltype); | |
| 240 | 242 | {**********CHANNEL TYPE CHOSEN**********************************************} | |
| 241 | 243 | case channeltype of | |
| 242 | 244 | {********************ANALOG INPUTS NAMES*************************************} | |
| 243 | 245 | 0: begin | |
| 244 | 246 | infotype:=DAQmx_Dev_AI_PhysicalChans; | |
| 245 | 247 | MsgChannelNames:='Analog input names'; | |
| 246 | 248 | MsgNoChannelsText:='There are no analog inputs'; | |
| 247 | 249 | end; | |
| 248 | 250 | {********************ANALOG OUTPUTS NAMES************************************} | |
| 249 | 251 | 1: begin | |
| 250 | 252 | infotype:=DAQmx_Dev_AO_PhysicalChans; | |
| 251 | 253 | MsgChannelNames:='Analog output names'; | |
| 252 | 254 | MsgNoChannelsText:='There are no analog outputs'; | |
| 253 | 255 | end; | |
| 254 | 256 | {********************DIGITAL INPUT PORTS NAMES*******************************} | |
| 255 | 257 | 2: begin | |
| 256 | 258 | infotype:=DAQmx_Dev_DI_Ports; | |
| 257 | 259 | MsgChannelNames:='Digital input ports'; | |
| 258 | 260 | MsgNoChannelsText:='There are no digital input ports'; | |
| 259 | 261 | end; | |
| 260 | 262 | {********************DIGITAL OUTPUT PORTS NAMES******************************} | |
| 261 | 263 | 3:begin | |
| 262 | 264 | infotype:=DAQmx_Dev_DO_Ports; | |
| 263 | 265 | MsgChannelNames:='Digital output ports'; | |
| 264 | 266 | MsgNoChannelsText:='There are no digital output ports'; | |
| 265 | 267 | end; | |
| 266 | 268 | {********************COUNTER INPUT NAMES******************************} | |
| 267 | 269 | 4:begin | |
| 268 | 270 | infotype:= DAQmx_Dev_CI_PhysicalChans; | |
| 269 | 271 | MsgChannelNames:='Digital counter inputs'; | |
| 270 | 272 | MsgNoChannelsText:='There are no counter inputs '; | |
| 271 | 273 | end; | |
| 272 | 274 | {********************COUNTER OUTPUT NAMES******************************} | |
| 273 | 275 | 5:begin | |
| 274 | 276 | infotype:= DAQmx_Dev_CO_PhysicalChans; | |
| 275 | 277 | MsgChannelNames:='Digital counter outputs'; | |
| 276 | 278 | MsgNoChannelsText:='There are no counter outputs '; | |
| 277 | 279 | end; | |
| 278 | 280 | end; | |
| 279 | 281 | {************GET CHANNEL NAMES*********************************************} | |
| 280 | 282 | if (channeltype>=0) and (channeltype<6) then begin | |
| 281 | 283 | //get buffer size or length of channelnames | |
| 282 | 284 | buffersize:=DAQmxGetDeviceAttribute(@DeviceName[1],infotype,Nil); | |
| 283 | 285 | if buffersize<>0 then begin | |
| 284 | 286 | SetLength(channelnames,buffersize); | |
| 285 | 287 | //get analog channel names separated by comma | |
| 286 | 288 | DAQmxGetDeviceAttribute(@DeviceName[1],infotype,channelnames[0],[buffersize]); | |
| 287 | 289 | //show info | |
| 288 | 290 | writeln(MsgChannelNames+': '+PChar(@channelnames[0])); | |
| 289 | 291 | end | |
| 290 | 292 | else writeln(MsgNoChannelsText); | |
| 291 | 293 | end; | |
| 292 | 294 | end. | |
| 293 | 295 | ``` | |
| 294 | 296 | ||
| 295 | 297 | Output example | |
| 296 | 298 | ||
| 297 | 299 | ```text | |
| 298 | 300 | Enter device name, for example Dev1, Dev2 etc.: Dev1 | |
| 299 | 301 | 0 - analog inputs | |
| 300 | 302 | 1 - analog outputs | |
| 301 | 303 | 2 - digital inputs | |
| 302 | 304 | 3 - digital outputs | |
| 303 | 305 | 4 - counter inputs | |
| 304 | 306 | 5 - counter outputs | |
| 305 | 307 | choose the number of required channel type:0 | |
| 306 | 308 | Analog input names: Dev1/ai0, Dev1/ai1, Dev1/ai2, Dev1/ai3, Dev1/ai4, Dev1/ai5, Dev1/ai6, Dev1/ai7, Dev1/ai8, Dev1/ai9, Dev1/ai10, Dev1/ai11, Dev1/ai12, Dev1/ai13, Dev1/ai14, Dev1/ai15 | |
| 307 | 309 | ``` | |
| 308 | 310 | ||
| 309 | 311 | ### ANALOG INPUTS/OUTPUTS | |
| 310 | 312 | ||
| 311 | 313 | #### Acquire finite number of samples from one analog input | |
| 312 | 314 | ||
| 313 | 315 | ``` pascal | |
| 314 | 316 | program aichan; | |
| 315 | 317 | ||
| 316 | 318 | {$mode objfpc}{$H+} | |
| 317 | 319 | ||
| 318 | 320 | uses | |
| 319 | 321 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 320 | 322 | cthreads, | |
| 321 | 323 | {$ENDIF}{$ENDIF} | |
| 322 | 324 | Classes, nidaqmx | |
| 323 | 325 | { you can add units after this }; | |
| 324 | 326 | var | |
| 325 | 327 | AITaskHandle:TaskHandle; | |
| 326 | 328 | data:array[1..1000] of double; | |
| 327 | 329 | reads,i:longint; | |
| 328 | 330 | ||
| 329 | 331 | {************Error handling procedure ***************************************** | |
| 330 | 332 | Example: | |
| 331 | 333 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev1/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 332 | 334 | ||
| 333 | 335 | *******************************************************************************} | |
| 334 | 336 | ||
| 335 | 337 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 336 | 338 | var | |
| 337 | 339 | errBuffer:array[0..2048] of char; | |
| 338 | 340 | begin | |
| 339 | 341 | //get error description | |
| 340 | 342 | if Aerror<>0 then begin | |
| 341 | 343 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 342 | 344 | writeln(PChar(@ErrBuffer[0])); | |
| 343 | 345 | //stop and cleat task | |
| 344 | 346 | if Assigned(@ATaskHandle) then begin | |
| 345 | 347 | DAQmxStopTask(ATaskHandle); | |
| 346 | 348 | DAQmxClearTask(ATaskHandle); | |
| 347 | 349 | end; | |
| 348 | 350 | //stop program | |
| 349 | 351 | Halt; | |
| 350 | 352 | end; | |
| 351 | 353 | end; | |
| 352 | 354 | {*********************MAIN PROGRAM********************************************* | |
| 353 | 355 | ******************* without error handling************************************} | |
| 354 | 356 | begin | |
| 355 | 357 | //create task | |
| 356 | 358 | DAQmxCreateTask('',@AITaskHandle); | |
| 357 | 359 | //create analog input channel Dev1/ai0 (activate it) | |
| 358 | 360 | //voltage range -10 - 10 | |
| 359 | 361 | DAQmxCreateAIVoltageChan(AITaskHandle,'Dev1/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL); | |
| 360 | 362 | //set timing | |
| 361 | 363 | //Sample rate = 10000 (10 kHz), sample per channel =1000 | |
| 362 | 364 | //acquire finite samples (1000 samples) defined by DAQmx_Val_FiniteSamps | |
| 363 | 365 | DAQmxCfgSampClkTiming(AITaskHandle,'OnboardClock',10000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000); | |
| 364 | 366 | //start task | |
| 365 | 367 | DAQmxStartTask(AITaskHandle); | |
| 366 | 368 | //acquire 1000 samples | |
| 367 | 369 | DAQmxReadAnalogF64(AITaskHandle,1000,10.0,DAQmx_Val_GroupByChannel,@data[1],1000,@reads,NIL); | |
| 368 | 370 | //show first 10 samples | |
| 369 | 371 | writeln(reads,' samples were acquired'); | |
| 370 | 372 | writeln(''); | |
| 371 | 373 | writeln('Value of first 10 samples'); | |
| 372 | 374 | for i:=1 to 10 do writeln('sample[',i,'] = ',data[i]:4:2,' V'); | |
| 373 | 375 | //stop and clear task | |
| 374 | 376 | DAQmxStopTask(AITaskHandle); | |
| 375 | 377 | DAQmxClearTask(AITaskHandle); | |
| 376 | 378 | end. | |
| 377 | 379 | ``` | |
| 378 | 380 | ||
| 379 | 381 | Output example | |
| 380 | 382 | ||
| 381 | 383 | ```text | |
| 382 | 384 | 1000 samples were acquired | |
| 383 | 385 | ||
| 384 | 386 | Value of first 10 samples | |
| 385 | 387 | sample[1] = 0.23 V | |
| 386 | 388 | sample[2] = 0.23 V | |
| 387 | 389 | sample[3] = 0.23 V | |
| 388 | 390 | sample[4] = 0.23 V | |
| 389 | 391 | sample[5] = 0.23 V | |
| 390 | 392 | sample[6] = 0.23 V | |
| 391 | 393 | sample[7] = 0.23 V | |
| 392 | 394 | sample[8] = 0.23 V | |
| 393 | 395 | sample[9] = 0.23 V | |
| 394 | 396 | sample[10] = 0.23 V | |
| 395 | 397 | ``` | |
| 396 | 398 | ||
| 397 | 399 | #### Continuously acquire samples from three analog inputs | |
| 398 | 400 | ||
| 399 | 401 | ``` pascal | |
| 400 | 402 | program multiaichannel; | |
| 401 | 403 | ||
| 402 | 404 | {$mode objfpc}{$H+} | |
| 403 | 405 | ||
| 404 | 406 | uses | |
| 405 | 407 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 406 | 408 | cthreads, | |
| 407 | 409 | {$ENDIF}{$ENDIF} | |
| 408 | 410 | Classes, nidaqmx,crt | |
| 409 | 411 | { you can add units after this }; | |
| 410 | 412 | var | |
| 411 | 413 | AITaskHandle:TaskHandle; | |
| 412 | 414 | data:array of double; | |
| 413 | 415 | reads:longint; | |
| 414 | 416 | totalsamplereads:longint=0; | |
| 415 | 417 | i:longint; | |
| 416 | 418 | ||
| 417 | 419 | {************Error handling procedure ***************************************** | |
| 418 | 420 | Example: | |
| 419 | 421 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev1/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 420 | 422 | ||
| 421 | 423 | *******************************************************************************} | |
| 422 | 424 | ||
| 423 | 425 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 424 | 426 | var | |
| 425 | 427 | errBuffer:array[0..2048] of char; | |
| 426 | 428 | begin | |
| 427 | 429 | //get error description | |
| 428 | 430 | if Aerror<>0 then begin | |
| 429 | 431 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 430 | 432 | writeln(PChar(@ErrBuffer[0])); | |
| 431 | 433 | //stop and cleat task | |
| 432 | 434 | if Assigned(@ATaskHandle) then begin | |
| 433 | 435 | DAQmxStopTask(ATaskHandle); | |
| 434 | 436 | DAQmxClearTask(ATaskHandle); | |
| 435 | 437 | end; | |
| 436 | 438 | //stop program | |
| 437 | 439 | Halt; | |
| 438 | 440 | end; | |
| 439 | 441 | end; | |
| 440 | 442 | {*********************MAIN PROGRAM********************************************* | |
| 441 | 443 | ******************* without error handling************************************} | |
| 442 | 444 | begin | |
| 443 | 445 | //Set buffer size for data acquiring, sample per channel 1000 | |
| 444 | 446 | //buffer size(data -array variable) = 3 channel * 1000 samples per channel | |
| 445 | 447 | Setlength(data,3*1000); | |
| 446 | 448 | //create task | |
| 447 | 449 | DAQmxCreateTask('',@AITaskHandle); | |
| 448 | 450 | //create analog input channel Dev1/ai0:2 (activate 3 channels) | |
| 449 | 451 | //voltage range -10 - 10 | |
| 450 | 452 | DAQmxCreateAIVoltageChan(AITaskHandle,'Dev1/ai0:2','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL); | |
| 451 | 453 | //set timing | |
| 452 | 454 | //Sample rate = 10000 (10 kHz), sample per channel =1000 | |
| 453 | 455 | //acquire finite samples (1000 samples) defined by DAQmx_Val_Cont_Samps | |
| 454 | 456 | DAQmxCfgSampClkTiming(AITaskHandle,'OnboardClock',10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000); | |
| 455 | 457 | //start task | |
| 456 | 458 | DAQmxStartTask(AITaskHandle); | |
| 457 | 459 | //acquire 10000 samples for each channel | |
| 458 | 460 | while totalsamplereads<10000 do begin | |
| 459 | 461 | //1000- sample rate, 3000 - buffer size(data -array variable) = 3 channel * 1000 samples per channel | |
| 460 | 462 | DAQmxReadAnalogF64(AITaskHandle,1000,10.0,DAQmx_Val_GroupByChannel,@data[0],3000,@reads,NIL); | |
| 461 | 463 | totalsamplereads:=totalsamplereads+reads; | |
| 462 | 464 | writeln('aquired number of samples: ',totalsamplereads); | |
| 463 | 465 | end; | |
| 464 | 466 | //show first 10 samples from last 1000 samples acquired from each channel | |
| 465 | 467 | writeln(''); | |
| 466 | 468 | writeln('Value of first 10 samples of last 1000 samples for each channel'); | |
| 467 | 469 | writeln('| chan0 | chan1 | chan2 | '); | |
| 468 | 470 | for i:= 0 to 9 do writeln('|',data[i]:5:2,' V| ',data[i+1000]:5:2,' V| ',data[i+2000]:5:2,' V|'); | |
| 469 | 471 | //stop and clear task | |
| 470 | 472 | DAQmxStopTask(AITaskHandle); | |
| 471 | 473 | DAQmxClearTask(AITaskHandle); | |
| 472 | 474 | end. | |
| 473 | 475 | ``` | |
| 474 | 476 | ||
| 475 | 477 | Output example | |
| 476 | 478 | ||
| 477 | 479 | ```text | |
| 478 | 480 | acquired number of samples: 1000 | |
| 479 | 481 | aquired number of samples: 2000 | |
| 480 | 482 | acquired number of samples: 3000 | |
| 481 | 483 | acquired number of samples: 4000 | |
| 482 | 484 | acquired number of samples: 5000 | |
| 483 | 485 | acquired number of samples: 6000 | |
| 484 | 486 | acquired number of samples: 7000 | |
| 485 | 487 | acquired number of samples: 8000 | |
| 486 | 488 | acquired number of samples: 9000 | |
| 487 | 489 | acquired number of samples: 10000 | |
| 488 | 490 | ||
| 489 | 491 | Value of first 10 samples of last 1000 samples for each channel | |
| 490 | 492 | | chan0 | chan1 | chan2 | | |
| 491 | 493 | | 5.07 V| 5.07 V| 5.06 V| | |
| 492 | 494 | | 5.07 V| 5.07 V| 5.06 V| | |
| 493 | 495 | | 5.07 V| 5.06 V| 5.07 V| | |
| 494 | 496 | | 5.06 V| 5.06 V| 5.07 V| | |
| 495 | 497 | | 5.06 V| 5.07 V| 5.07 V| | |
| 496 | 498 | | 5.07 V| 5.07 V| 5.06 V| | |
| 497 | 499 | | 5.07 V| 5.07 V| 5.07 V| | |
| 498 | 500 | | 5.06 V| 5.06 V| 5.07 V| | |
| 499 | 501 | | 5.06 V| 5.06 V| 5.06 V| | |
| 500 | 502 | | 5.07 V| 5.07 V| 5.06 V| | |
| 501 | 503 | ``` | |
| 502 | 504 | ||
| 503 | 505 | #### Start acquisition on analog input by digital trigger | |
| 504 | 506 | ||
| 505 | 507 | This example demonstrates how to start acquisition by digital trigger. | |
| 506 | 508 | ||
| 507 | 509 | ``` pascal | |
| 508 | 510 | program aidigtrig; | |
| 509 | 511 | ||
| 510 | 512 | {$mode objfpc}{$H+} | |
| 511 | 513 | ||
| 512 | 514 | uses | |
| 513 | 515 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 514 | 516 | cthreads, | |
| 515 | 517 | {$ENDIF}{$ENDIF} | |
| 516 | 518 | Classes,nidaqmx,crt | |
| 517 | 519 | { you can add units after this }; | |
| 518 | 520 | var | |
| 519 | 521 | AITaskHandle:TaskHandle; | |
| 520 | 522 | channel:string = 'Dev1/ai0'; | |
| 521 | 523 | SampleRate:double = 10000; | |
| 522 | 524 | SamplePerChannel:longint = 1000; | |
| 523 | 525 | PretriggerSamples: longint= 100; //MUST be > 2 | |
| 524 | 526 | reads:longint = 0; | |
| 525 | 527 | i:longint; | |
| 526 | 528 | data:array of double; | |
| 527 | 529 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 528 | 530 | var | |
| 529 | 531 | errBuffer:array[0..2048] of char; | |
| 530 | 532 | begin | |
| 531 | 533 | //get error description | |
| 532 | 534 | if Aerror<>0 then begin | |
| 533 | 535 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 534 | 536 | writeln(ErrBuffer); | |
| 535 | 537 | //stop and cleat task | |
| 536 | 538 | if Assigned(@ATaskHandle) then begin | |
| 537 | 539 | DAQmxStopTask(ATaskHandle); | |
| 538 | 540 | DAQmxClearTask(ATaskHandle); | |
| 539 | 541 | end; | |
| 540 | 542 | //stop acquisition | |
| 541 | 543 | writeln('Program stoped'); | |
| 542 | 544 | Halt; | |
| 543 | 545 | end; | |
| 544 | 546 | end; | |
| 545 | 547 | ||
| 546 | 548 | begin | |
| 547 | 549 | //set buffer length | |
| 548 | 550 | SetLength(data,SamplePerChannel); | |
| 549 | 551 | //create take | |
| 550 | 552 | DAQmxCreateTask('',@AITaskHandle); | |
| 551 | 553 | //create ai channel | |
| 552 | 554 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,@channel[1],'',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,Nil),AITaskHandle); | |
| 553 | 555 | //configure sample clock | |
| 554 | 556 | ErrorCheck(DAQmxCfgSampClkTiming(AITaskHandle,'',SampleRate,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,SamplePerChannel),AITaskHandle); | |
| 555 | 557 | //configure triger with 100 pretriggered samples | |
| 556 | 558 | ErrorCheck(DAQmxCfgDigEdgeRefTrig(AITaskHandle,'/Dev1/PFI0',DAQmx_Val_Rising,PretriggerSamples),AITaskHandle); | |
| 557 | 559 | //start task | |
| 558 | 560 | ErrorCheck(DAQmxStartTask(AITaskHandle),AITaskHandle); | |
| 559 | 561 | //acquire data | |
| 560 | 562 | writeln('Press any key to stop acquisition'); | |
| 561 | 563 | i := 0; | |
| 562 | 564 | repeat | |
| 563 | 565 | ErrorCheck(DAQmxReadAnalogF64(AITaskHandle,SamplePerChannel,10.0,DAQmx_Val_GroupByChannel,@data[0],Length(data),@reads,Nil),AITaskHandle); | |
| 564 | 566 | i := i + reads; | |
| 565 | 567 | writeln('Number of acquired samples: ', i); | |
| 566 | 568 | until keypressed; | |
| 567 | 569 | //show last readed number of samples | |
| 568 | 570 | for i:=Low(data) to High(data) do writeln('data[',i,'] = ', data[i]:4:2); | |
| 569 | 571 | ||
| 570 | 572 | //stop and clear task | |
| 571 | 573 | if Assigned(@AITaskHandle) then begin | |
| 572 | 574 | DAQmxStopTask(AITaskHandle); | |
| 573 | 575 | DAQmxClearTask(AITaskHandle); | |
| 574 | 576 | end; | |
| 575 | 577 | end. | |
| 576 | 578 | ``` | |
| 577 | 579 | ||
| 578 | 580 | #### Analog output software controlled | |
| 579 | 581 | ||
| 580 | 582 | This example demonstrates sinusoidal waveform generation with software timed control analog output. | |
| 581 | 583 | ||
| 582 | 584 | ``` pascal | |
| 583 | 585 | program aochansw; | |
| 584 | 586 | ||
| 585 | 587 | {$mode objfpc}{$H+} | |
| 586 | 588 | ||
| 587 | 589 | uses | |
| 588 | 590 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 589 | 591 | cthreads, | |
| 590 | 592 | {$ENDIF}{$ENDIF} | |
| 591 | 593 | Classes,Crt,SysUtils,nidaqmx | |
| 592 | 594 | { you can add units after this }; | |
| 593 | 595 | var | |
| 594 | 596 | AOTaskHandle:TaskHandle; | |
| 595 | 597 | //data writted to analog output | |
| 596 | 598 | data:array [0..999] of double; | |
| 597 | 599 | writtensamples:integer; | |
| 598 | 600 | i:integer; | |
| 599 | 601 | ||
| 600 | 602 | {************Error handling procedure ***************************************** | |
| 601 | 603 | Example: | |
| 602 | 604 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev1/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 603 | 605 | *******************************************************************************} | |
| 604 | 606 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 605 | 607 | var | |
| 606 | 608 | errBuffer:array[0..2048] of char; | |
| 607 | 609 | begin | |
| 608 | 610 | //get error description | |
| 609 | 611 | if Aerror<>0 then begin | |
| 610 | 612 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 611 | 613 | writeln(PChar(@ErrBuffer[0])); | |
| 612 | 614 | //stop and cleat task | |
| 613 | 615 | if Assigned(@ATaskHandle) then begin | |
| 614 | 616 | DAQmxStopTask(ATaskHandle); | |
| 615 | 617 | DAQmxClearTask(ATaskHandle); | |
| 616 | 618 | end; | |
| 617 | 619 | //stop program | |
| 618 | 620 | Halt; | |
| 619 | 621 | end; | |
| 620 | 622 | end; | |
| 621 | 623 | ||
| 622 | 624 | begin | |
| 623 | 625 | //prepare data for analog output | |
| 624 | 626 | //sin waveform | |
| 625 | 627 | for i:=Low(data) to High(data) do data[i]:=9.95*sin(i*2.0*Pi/1000.0); | |
| 626 | 628 | //create task | |
| 627 | 629 | DAQmxCreateTask('',@AOTaskHandle); | |
| 628 | 630 | //configure analog output channel | |
| 629 | 631 | ErrorCheck(DAQmxCreateAOVoltageChan(AOTaskHandle,'Dev1/ao0','',-10.0,10.0,DAQmx_Val_Volts,Nil),AOTaskHandle); | |
| 630 | 632 | //start task | |
| 631 | 633 | ErrorCheck(DAQmxStartTask(AOTaskHandle),AOTaskHandle); | |
| 632 | 634 | ||
| 633 | 635 | writeln('Press any key to stop generation'); | |
| 634 | 636 | //continuosly waveform generation | |
| 635 | 637 | //if you call task start function (DAQmxStartTask) | |
| 636 | 638 | //before output write function (for example DAQmxWriteAnalogScalarF64) | |
| 637 | 639 | //set AutoStart parameter to 1 | |
| 638 | 640 | //if output function berofe start AutoStart = 0 | |
| 639 | 641 | i:=0; | |
| 640 | 642 | repeat | |
| 641 | 643 | sleep(1); | |
| 642 | 644 | ErrorCheck(DAQmxWriteAnalogScalarF64(AOTaskHandle,1,10.0,data[i],Nil),AOTaskHandle); | |
| 643 | 645 | inc(i); | |
| 644 | 646 | if i >=1000 then i:=0; | |
| 645 | 647 | until keypressed; | |
| 646 | 648 | ||
| 647 | 649 | //stop and clear task | |
| 648 | 650 | if Assigned(@AOTaskHandle) then begin | |
| 649 | 651 | DAQmxStopTask(AOTaskHandle); | |
| 650 | 652 | DAQmxClearTask(AOTaskHandle); | |
| 651 | 653 | end; | |
| 652 | 654 | end. | |
| 653 | 655 | ``` | |
| 654 | 656 | ||
| 655 | 657 | #### Analog output hardware controlled | |
| 656 | 658 | ||
| 657 | 659 | This example demonstrates how to generate finite number of samples on analog output channel. | |
| 658 | 660 | ||
| 659 | 661 | ``` pascal | |
| 660 | 662 | program aochannel; | |
| 661 | 663 | ||
| 662 | 664 | {$mode objfpc}{$H+} | |
| 663 | 665 | ||
| 664 | 666 | uses | |
| 665 | 667 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 666 | 668 | cthreads, | |
| 667 | 669 | {$ENDIF}{$ENDIF} | |
| 668 | 670 | Classes,nidaqmx | |
| 669 | 671 | { you can add units after this }; | |
| 670 | 672 | ||
| 671 | 673 | var | |
| 672 | 674 | AOTaskHandle:TaskHandle; | |
| 673 | 675 | //data writted to analog output | |
| 674 | 676 | data:array [0..3999] of double; | |
| 675 | 677 | writtensamples:integer; | |
| 676 | 678 | i:integer; | |
| 677 | 679 | ||
| 678 | 680 | {************Error handling procedure ***************************************** | |
| 679 | 681 | Example: | |
| 680 | 682 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev1/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 681 | 683 | ||
| 682 | 684 | *******************************************************************************} | |
| 683 | 685 | ||
| 684 | 686 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 685 | 687 | var | |
| 686 | 688 | errBuffer:array[0..2048] of char; | |
| 687 | 689 | begin | |
| 688 | 690 | //get error description | |
| 689 | 691 | if Aerror<>0 then begin | |
| 690 | 692 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 691 | 693 | writeln(PChar(@ErrBuffer[0])); | |
| 692 | 694 | //stop and cleat task | |
| 693 | 695 | if Assigned(@ATaskHandle) then begin | |
| 694 | 696 | DAQmxStopTask(ATaskHandle); | |
| 695 | 697 | DAQmxClearTask(ATaskHandle); | |
| 696 | 698 | end; | |
| 697 | 699 | //stop program | |
| 698 | 700 | Halt; | |
| 699 | 701 | end; | |
| 700 | 702 | end; | |
| 701 | 703 | ||
| 702 | 704 | {*********************MAIN PROGRAM********************************************* | |
| 703 | 705 | } | |
| 704 | 706 | begin | |
| 705 | 707 | //prepare data for analog output | |
| 706 | 708 | for i:=Low(data) to High(data) do data[i]:= 5.0*i/4000.0; | |
| 707 | 709 | //create task | |
| 708 | 710 | DAQmxCreateTask('',@AOTaskHandle); | |
| 709 | 711 | //configure analog output channel | |
| 710 | 712 | ErrorCheck(DAQmxCreateAOVoltageChan(AOTaskHandle,'Dev1/ao0','',-10.0,10.0,DAQmx_Val_Volts,Nil),AOTaskHandle); | |
| 711 | 713 | //configure clock | |
| 712 | 714 | ErrorCheck(DAQmxCfgSampClkTiming(AOTaskHandle,'',1000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,4000),AOTaskHandle); | |
| 713 | 715 | //write data to buffer | |
| 714 | 716 | ErrorCheck(DAQmxWriteAnalogF64(AOTaskHandle,4000,0,10.0,DAQmx_Val_GroupByChannel,data,@writtensamples,Nil),AOTaskHandle); | |
| 715 | 717 | //start task | |
| 716 | 718 | ErrorCheck(DAQmxStartTask(AOTaskHandle),AOTaskHandle); | |
| 717 | 719 | //wait until all samples are generated | |
| 718 | 720 | ErrorCheck(DAQmxWaitUntilTaskDone(AOTaskHandle,10.0),AOTaskHandle); | |
| 719 | 721 | //stop and clear task | |
| 720 | 722 | if Assigned(@AOTaskHandle) then begin | |
| 721 | 723 | DAQmxStopTask(AOTaskHandle); | |
| 722 | 724 | DAQmxClearTask(AOTaskHandle); | |
| 723 | 725 | end; | |
| 724 | 726 | end. | |
| 725 | 727 | ``` | |
| 726 | 728 | ||
| 727 | 729 | ### DIGITAL INPUTS/OUTPUTS | |
| 728 | 730 | ||
| 729 | 731 | Most of DAQ cards have two types of digital ports (input/output): correlated and static. Static ports are software timed, correlated are software or hardware timed by the external or internal clock for example couter output signal (**/Dev1/Ctr0InternalOutput** - couter0 as clock ). Check the documention to find it, for example NI-PCI6250 have three 8-bit ports: port0 - correlated, port1,port2 - static; NI-PCI6224 two ports: 32-bit port0 - correlated, 8-bit port1 - static. | |
| 730 | 732 | ||
| 731 | 733 | Read/Write data from/to 8-bit and 32-bit digital port use an appropriate function **DAQmxReadDigitalU8**, **DAQmxWriteDigitalU8** and **DAQmxReadDigitalU32**, **DAQmxWriteDigitalU32**. | |
| 732 | 734 | ||
| 733 | 735 | *Different behaviour of collerated port0, when it sets as input, occured under Linux and Windows: software timed works only under Windows, with external clock works under Windows and Linux!!!* | |
| 734 | 736 | ||
| 735 | 737 | #### Software timed digital input | |
| 736 | 738 | ||
| 737 | 739 | To check it, connected +5V(depend on hardware) to one of available port pins. | |
| 738 | 740 | ||
| 739 | 741 | ``` pascal | |
| 740 | 742 | program diport; | |
| 741 | 743 | ||
| 742 | 744 | {$mode objfpc}{$H+} | |
| 743 | 745 | ||
| 744 | 746 | uses | |
| 745 | 747 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 746 | 748 | cthreads, | |
| 747 | 749 | {$ENDIF}{$ENDIF} | |
| 748 | 750 | Classes,nidaqmx,SysUtils,StrUtils | |
| 749 | 751 | { you can add units after this }; | |
| 750 | 752 | var | |
| 751 | 753 | DITaskHandle:TaskHandle; | |
| 752 | 754 | data:array [0..9] of byte; | |
| 753 | 755 | reads:longint = 0; | |
| 754 | 756 | totalreads:longint = 0; | |
| 755 | 757 | i:longint ; | |
| 756 | 758 | {************Error handling procedure ***************************************** | |
| 757 | 759 | Example: | |
| 758 | 760 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev1/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 759 | 761 | ||
| 760 | 762 | *******************************************************************************} | |
| 761 | 763 | ||
| 762 | 764 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 763 | 765 | var | |
| 764 | 766 | errBuffer:array[0..2048] of char; | |
| 765 | 767 | begin | |
| 766 | 768 | //get error description | |
| 767 | 769 | if Aerror<>0 then begin | |
| 768 | 770 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 769 | 771 | writeln(PChar(@ErrBuffer[0])); | |
| 770 | 772 | //stop and cleat task | |
| 771 | 773 | if Assigned(@ATaskHandle) then begin | |
| 772 | 774 | DAQmxStopTask(ATaskHandle); | |
| 773 | 775 | DAQmxClearTask(ATaskHandle); | |
| 774 | 776 | end; | |
| 775 | 777 | //stop program | |
| 776 | 778 | Halt; | |
| 777 | 779 | end; | |
| 778 | 780 | end; | |
| 779 | 781 | {*******************MAIN PROGRAM WITH ERROR HANDLING*************************} | |
| 780 | 782 | begin | |
| 781 | 783 | //task create | |
| 782 | 784 | ErrorCheck(DAQmxCreateTask('',@DITaskHandle),DITaskHandle); | |
| 783 | 785 | //digital input port create | |
| 784 | 786 | ErrorCheck(DAQmxCreateDIChan(DITaskHandle,'Dev1/port1','',DAQmx_Val_ChanForAllLines),DITaskHandle); | |
| 785 | 787 | //Start Task | |
| 786 | 788 | ErrorCheck(DAQmxStartTask(DITaskHandle),DITaskHandle); | |
| 787 | 789 | //read 10 samples from 8-bit digital port (for example NI PCI-6250 card port1(pins P1.0-P1.7)) | |
| 788 | 790 | //for 32-bit digital port use DAQmxReadDigitalU32 function | |
| 789 | 791 | ErrorCheck(DAQmxReadDigitalU8(DITaskHandle,10,10.0,DAQmx_Val_GroupByChannel,@data,10,@reads,Nil),DITaskHandle); | |
| 790 | 792 | writeln('number of readed samples: ',reads); | |
| 791 | 793 | for i:=0 to 9 do writeln('data [',i,'] = %',IntToBin(data[i],8)); | |
| 792 | 794 | //stop and clear task | |
| 793 | 795 | if DITaskHandle<>0 then begin | |
| 794 | 796 | DAQmxStopTask(DITaskHandle); | |
| 795 | 797 | DAQmxClearTask(DITaskHandle); | |
| 796 | 798 | end; | |
| 797 | 799 | end. | |
| 798 | 800 | ``` | |
| 799 | 801 | ||
| 800 | 802 | Output example: +5V connected to pin P1.2 port1 on NI-PCI6250 card. | |
| 801 | 803 | ||
| 802 | 804 | ```text | |
| 803 | 805 | number of readed samples: 10 | |
| 804 | 806 | data [0] = %00000100 | |
| 805 | 807 | data [1] = %00000100 | |
| 806 | 808 | data [2] = %00000100 | |
| 807 | 809 | data [3] = %00000100 | |
| 808 | 810 | data [4] = %00000100 | |
| 809 | 811 | data [5] = %00000100 | |
| 810 | 812 | data [6] = %00000100 | |
| 811 | 813 | data [7] = %00000100 | |
| 812 | 814 | data [8] = %00000100 | |
| 813 | 815 | data [9] = %00000100 | |
| 814 | 816 | ``` | |
| 815 | 817 | ||
| 816 | 818 | #### Hardware timed digital input | |
| 817 | 819 | ||
| 818 | 820 | This example shows using of counter output signal as a clock for digital inputs port for hardware timed acquire data on digital inputs. This feature is supported only for correlated digital port. In this example 8-bit port0 on NI-PCI6250 card is used to check this function. | |
| 819 | 821 | ||
| 820 | 822 | To check it, connected +5V(depend on hardware) to one of available pins on correlated digital port. | |
| 821 | 823 | ||
| 822 | 824 | ``` pascal | |
| 823 | 825 | program diporttimed; | |
| 824 | 826 | ||
| 825 | 827 | {$mode objfpc}{$H+} | |
| 826 | 828 | ||
| 827 | 829 | uses | |
| 828 | 830 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 829 | 831 | cthreads, | |
| 830 | 832 | {$ENDIF}{$ENDIF} | |
| 831 | 833 | Classes,nidaqmx,SysUtils,StrUtils | |
| 832 | 834 | { you can add units after this }; | |
| 833 | 835 | var | |
| 834 | 836 | DITaskHandle,CtrTaskHandle:TaskHandle; | |
| 835 | 837 | data:array [0..9] of byte; | |
| 836 | 838 | reads:longint = 0; | |
| 837 | 839 | totalreads:longint = 0; | |
| 838 | 840 | i:longint ; | |
| 839 | 841 | {************Error handling procedure ***************************************** | |
| 840 | 842 | Example: | |
| 841 | 843 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev1/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 842 | 844 | ||
| 843 | 845 | *******************************************************************************} | |
| 844 | 846 | ||
| 845 | 847 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 846 | 848 | var | |
| 847 | 849 | errBuffer:array[0..2048] of char; | |
| 848 | 850 | begin | |
| 849 | 851 | //get error description | |
| 850 | 852 | if Aerror<>0 then begin | |
| 851 | 853 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 852 | 854 | writeln(PChar(@ErrBuffer[0])); | |
| 853 | 855 | //stop and cleat task | |
| 854 | 856 | if Assigned(@ATaskHandle) then begin | |
| 855 | 857 | DAQmxStopTask(ATaskHandle); | |
| 856 | 858 | DAQmxClearTask(ATaskHandle); | |
| 857 | 859 | end; | |
| 858 | 860 | //stop program | |
| 859 | 861 | Halt; | |
| 860 | 862 | end; | |
| 861 | 863 | end; | |
| 862 | 864 | {*******************MAIN PROGRAM WITH ERROR HANDLING*************************} | |
| 863 | 865 | begin | |
| 864 | 866 | {******CREATE COUNTER OUTPUT TO USE IT AS CLOCK FOR DIGITAL PORT INPUTS****** | |
| 865 | 867 | *******INTERNALLY CONNECTED ON M-SERIES as /Dev1/Ctr0InternalOutput**********} | |
| 866 | 868 | //create couter task | |
| 867 | 869 | ErrorCheck(DAQmxCreateTask('Counter Task',@CtrTaskHandle),CtrTaskHandle); | |
| 868 | 870 | //create counter output channel | |
| 869 | 871 | //Frequency 1000 Hz, duty 0.5 | |
| 870 | 872 | ErrorCheck(DAQmxCreateCOPulseChanFreq(CtrTaskHandle,'Dev1/ctr0','',DAQmx_Val_Hz,DAQmx_Val_Low,0,1000,0.5),CtrTaskHandle); | |
| 871 | 873 | //timing 1000 samples per second | |
| 872 | 874 | ErrorCheck(DAQmxCfgImplicitTiming(CtrTaskHandle,DAQmx_Val_ContSamps,1000),CtrTaskHandle); | |
| 873 | 875 | //start counter task | |
| 874 | 876 | ErrorCheck(DAQmxStartTask(CtrTaskHandle),CtrTaskHandle); | |
| 875 | 877 | ||
| 876 | 878 | //digital input task create | |
| 877 | 879 | ErrorCheck(DAQmxCreateTask('Digital input task',@DITaskHandle),DITaskHandle); | |
| 878 | 880 | //digital input port create | |
| 879 | 881 | ErrorCheck(DAQmxCreateDIChan(DITaskHandle,'Dev1/port0','',DAQmx_Val_ChanForAllLines),DITaskHandle); | |
| 880 | 882 | //timing use counter output as clock source internally connected | |
| 881 | 883 | //as /Dev1/Ctr0InternalOutput | |
| 882 | 884 | ErrorCheck(DAQmxCfgSampClkTiming(DITaskHandle,'/Dev1/Ctr0InternalOutput',1000.0,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000),DITaskHandle); | |
| 883 | 885 | //Start digital input Task | |
| 884 | 886 | ErrorCheck(DAQmxStartTask(DITaskHandle),DITaskHandle); | |
| 885 | 887 | //read 10 samples from 8-bit digital port for example correlated port0 | |
| 886 | 888 | //for 32-bit digital port use DAQmxReadDigitalU32 function | |
| 887 | 889 | ErrorCheck(DAQmxReadDigitalU8(DITaskHandle,10,10.0,DAQmx_Val_GroupByChannel,@data,10,@reads,Nil),DITaskHandle); | |
| 888 | 890 | writeln('number of readed samples: ',reads); | |
| 889 | 891 | for i:=0 to 9 do writeln('data [',i,'] = %',IntToBin(data[i],8)); | |
| 890 | 892 | //stop and clear task | |
| 891 | 893 | DAQmxStopTask(DITaskHandle); | |
| 892 | 894 | DAQmxClearTask(DITaskHandle); | |
| 893 | 895 | DAQmxStopTask(CtrTaskHandle); | |
| 894 | 896 | DAQmxClearTask(CtrTaskHandle); | |
| 895 | 897 | end. | |
| 896 | 898 | ``` | |
| 897 | 899 | ||
| 898 | 900 | Output example: +5V connected to pin P0.5 on port0 on NI-PCI6250 card. | |
| 899 | 901 | ||
| 900 | 902 | ```text | |
| 901 | 903 | number of readed samples: 10 | |
| 902 | 904 | data [0] = %00100000 | |
| 903 | 905 | data [1] = %00100000 | |
| 904 | 906 | data [2] = %00100000 | |
| 905 | 907 | data [3] = %00100000 | |
| 906 | 908 | data [4] = %00100000 | |
| 907 | 909 | data [5] = %00100000 | |
| 908 | 910 | data [6] = %00100000 | |
| 909 | 911 | data [7] = %00100000 | |
| 910 | 912 | data [8] = %00100000 | |
| 911 | 913 | data [9] = %00100000 | |
| 912 | 914 | ``` | |
| 913 | 915 | ||
| 914 | 916 | #### Software controlled digital output | |
| 915 | 917 | ||
| 916 | 918 | To check it connect digital output(s) to oscilloscope or to card analog input. | |
| 917 | 919 | ||
| 918 | 920 | ``` pascal | |
| 919 | 921 | program doportlpi; | |
| 920 | 922 | ||
| 921 | 923 | {$mode objfpc}{$H+} | |
| 922 | 924 | ||
| 923 | 925 | uses | |
| 924 | 926 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 925 | 927 | cthreads, | |
| 926 | 928 | {$ENDIF}{$ENDIF} | |
| 927 | 929 | Classes,nidaqmx,SysUtils,StrUtils | |
| 928 | 930 | { you can add units after this }; | |
| 929 | 931 | var | |
| 930 | 932 | DOTaskHandle:TaskHandle; | |
| 931 | 933 | data: byte; | |
| 932 | 934 | written:longint = 0; | |
| 933 | 935 | {************Error handling procedure ***************************************** | |
| 934 | 936 | Example: | |
| 935 | 937 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev0/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 936 | 938 | ||
| 937 | 939 | *******************************************************************************} | |
| 938 | 940 | ||
| 939 | 941 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 940 | 942 | var | |
| 941 | 943 | errBuffer:array[0..2048] of char; | |
| 942 | 944 | begin | |
| 943 | 945 | //get error description | |
| 944 | 946 | if Aerror<>0 then begin | |
| 945 | 947 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 946 | 948 | writeln(PChar(@ErrBuffer[0])); | |
| 947 | 949 | //stop and cleat task | |
| 948 | 950 | if Assigned(@ATaskHandle) then begin | |
| 949 | 951 | DAQmxStopTask(ATaskHandle); | |
| 950 | 952 | DAQmxClearTask(ATaskHandle); | |
| 951 | 953 | end; | |
| 952 | 954 | //stop program | |
| 953 | 955 | Halt; | |
| 954 | 956 | end; | |
| 955 | 957 | end; | |
| 956 | 958 | {*******************MAIN PROGRAM WITH ERROR HANDLING*************************} | |
| 957 | 959 | begin | |
| 958 | 960 | //task create | |
| 959 | 961 | ErrorCheck(DAQmxCreateTask('',@DOTaskHandle),DOTaskHandle); | |
| 960 | 962 | //digital input port create | |
| 961 | 963 | ErrorCheck(DAQmxCreateDOChan(DOTaskHandle,'Dev1/port0','',DAQmx_Val_ChanForAllLines),DOTaskHandle); | |
| 962 | 964 | //task start | |
| 963 | 965 | //no need to start task | |
| 964 | 966 | ErrorCheck(DAQmxStartTask(DOTaskHandle),DOTaskHandle); | |
| 965 | 967 | //for 32-bit digital port use DAQmxReadDigitalU32 function | |
| 966 | 968 | //set 1 on all lines of digital port | |
| 967 | 969 | //change data:=0 to set 0 or use DAQmxResetDevice function to | |
| 968 | 970 | data:=0; | |
| 969 | 971 | ErrorCheck(DAQmxWriteDigitalU8(DOTaskHandle,1,1,10.0,DAQmx_Val_GroupByChannel,@data,@written,Nil),DOTaskHandle); | |
| 970 | 972 | //use DAQmxResetDevice function here to set signal on Low state | |
| 971 | 973 | writeln('number of written samples: ',written); | |
| 972 | 974 | //stop and clear task | |
| 973 | 975 | if DOTaskHandle<>0 then begin | |
| 974 | 976 | DAQmxStopTask(DOTaskHandle); | |
| 975 | 977 | DAQmxClearTask(DOTaskHandle); | |
| 976 | 978 | end; | |
| 977 | 979 | end. | |
| 978 | 980 | ``` | |
| 979 | 981 | ||
| 980 | 982 | #### Hardware timed digital outputs with counter output used as source clock | |
| 981 | 983 | ||
| 982 | 984 | To check it connect digital output(s) to oscilloscope or to card analog input. | |
| 983 | 985 | ||
| 984 | 986 | ``` pascal | |
| 985 | 987 | program doporttimed; | |
| 986 | 988 | ||
| 987 | 989 | {$mode objfpc}{$H+} | |
| 988 | 990 | ||
| 989 | 991 | uses | |
| 990 | 992 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 991 | 993 | cthreads, | |
| 992 | 994 | {$ENDIF}{$ENDIF} | |
| 993 | 995 | Classes,nidaqmx,SysUtils,StrUtils,Crt | |
| 994 | 996 | { you can add units after this }; | |
| 995 | 997 | var | |
| 996 | 998 | DOTaskHandle,CtrTaskHandle:TaskHandle; | |
| 997 | 999 | data:array[0..7] of byte = (1,2,4,8,16,32,64,128); | |
| 998 | 1000 | written:longint = 0; | |
| 999 | 1001 | {************Error handling procedure ***************************************** | |
| 1000 | 1002 | Example: | |
| 1001 | 1003 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev0/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 1002 | 1004 | ||
| 1003 | 1005 | *******************************************************************************} | |
| 1004 | 1006 | ||
| 1005 | 1007 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 1006 | 1008 | var | |
| 1007 | 1009 | errBuffer:array[0..2048] of char; | |
| 1008 | 1010 | begin | |
| 1009 | 1011 | //get error description | |
| 1010 | 1012 | if Aerror<>0 then begin | |
| 1011 | 1013 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 1012 | 1014 | writeln(PChar(@ErrBuffer[0])); | |
| 1013 | 1015 | //stop and cleat task | |
| 1014 | 1016 | if Assigned(@ATaskHandle) then begin | |
| 1015 | 1017 | DAQmxStopTask(ATaskHandle); | |
| 1016 | 1018 | DAQmxClearTask(ATaskHandle); | |
| 1017 | 1019 | end; | |
| 1018 | 1020 | //stop program | |
| 1019 | 1021 | Halt; | |
| 1020 | 1022 | end; | |
| 1021 | 1023 | end; | |
| 1022 | 1024 | {*******************MAIN PROGRAM WITH ERROR HANDLING*************************} | |
| 1023 | 1025 | begin | |
| 1024 | 1026 | {******CREATE COUNTER OUTPUT TO USE IT AS CLOCK FOR DIGITAL PORT OUTPUTS****** | |
| 1025 | 1027 | *******INTERNALLY CONNECTED ON M-SERIES as /Dev1/Ctr0InternalOutput**********} | |
| 1026 | 1028 | //create couter task | |
| 1027 | 1029 | ErrorCheck(DAQmxCreateTask('Counter Task',@CtrTaskHandle),CtrTaskHandle); | |
| 1028 | 1030 | //create counter output channel | |
| 1029 | 1031 | //Frequency 1000 Hz, duty 0.5 | |
| 1030 | 1032 | ErrorCheck(DAQmxCreateCOPulseChanFreq(CtrTaskHandle,'Dev1/ctr0','',DAQmx_Val_Hz,DAQmx_Val_Low,0,10000,0.5),CtrTaskHandle); | |
| 1031 | 1033 | //timing 1000 samples per second | |
| 1032 | 1034 | ErrorCheck(DAQmxCfgImplicitTiming(CtrTaskHandle,DAQmx_Val_ContSamps,1000),CtrTaskHandle); | |
| 1033 | 1035 | //start task | |
| 1034 | 1036 | //start counter task | |
| 1035 | 1037 | ErrorCheck(DAQmxStartTask(CtrTaskHandle),CtrTaskHandle); | |
| 1036 | 1038 | ||
| 1037 | 1039 | //task create | |
| 1038 | 1040 | ErrorCheck(DAQmxCreateTask('',@DOTaskHandle),DOTaskHandle); | |
| 1039 | 1041 | //digital input port create | |
| 1040 | 1042 | ErrorCheck(DAQmxCreateDOChan(DOTaskHandle,'Dev1/port0','',DAQmx_Val_ChanForAllLines),DOTaskHandle); | |
| 1041 | 1043 | //timing | |
| 1042 | 1044 | //frequency 1000Hz | |
| 1043 | 1045 | ErrorCheck(DAQmxCfgSampClkTiming(DOTaskHandle,'/Dev1/Ctr0InternalOutput',1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000),DOTaskHandle); | |
| 1044 | 1046 | //for 32-bit digital port use DAQmxWriteDigitalU32 function | |
| 1045 | 1047 | //function sets to start task automaticly 3-rd parameter | |
| 1046 | 1048 | //write 8 samples | |
| 1047 | 1049 | ErrorCheck(DAQmxWriteDigitalU8(DOTaskHandle,8,1,10.0,DAQmx_Val_GroupByChannel,data,@written,Nil),DOTaskHandle); | |
| 1048 | 1050 | writeln('number of written samples: ',written); | |
| 1049 | 1051 | //stop and clear task | |
| 1050 | 1052 | DAQmxStopTask(DOTaskHandle); | |
| 1051 | 1053 | DAQmxClearTask(DOTaskHandle); | |
| 1052 | 1054 | DAQmxStopTask(CtrTaskHandle); | |
| 1053 | 1055 | DAQmxClearTask(CtrTaskHandle); | |
| 1054 | 1056 | end. | |
| 1055 | 1057 | ``` | |
| 1056 | 1058 | ||
| 1057 | 1059 | ### COUNTER INPUTS/OUTPUTS | |
| 1058 | 1060 | ||
| 1059 | 1061 | #### Pulse generation | |
| 1060 | 1062 | ||
| 1061 | 1063 | This example demonstrates pwm generaton with frequency 1000Hz and duty circle 0.5. | |
| 1062 | 1064 | ||
| 1063 | 1065 | ``` pascal | |
| 1064 | 1066 | program doporttimed; | |
| 1065 | 1067 | ||
| 1066 | 1068 | {$mode objfpc}{$H+} | |
| 1067 | 1069 | ||
| 1068 | 1070 | uses | |
| 1069 | 1071 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1070 | 1072 | cthreads, | |
| 1071 | 1073 | {$ENDIF}{$ENDIF} | |
| 1072 | 1074 | Classes,nidaqmx,SysUtils,StrUtils,Crt | |
| 1073 | 1075 | { you can add units after this }; | |
| 1074 | 1076 | var | |
| 1075 | 1077 | CtrTaskHandle:TaskHandle; | |
| 1076 | 1078 | {************Error handling procedure ***************************************** | |
| 1077 | 1079 | Example: | |
| 1078 | 1080 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev0/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 1079 | 1081 | ||
| 1080 | 1082 | *******************************************************************************} | |
| 1081 | 1083 | ||
| 1082 | 1084 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 1083 | 1085 | var | |
| 1084 | 1086 | errBuffer:array[0..2048] of char; | |
| 1085 | 1087 | begin | |
| 1086 | 1088 | //get error description | |
| 1087 | 1089 | if Aerror<>0 then begin | |
| 1088 | 1090 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 1089 | 1091 | writeln(PChar(@ErrBuffer[0])); | |
| 1090 | 1092 | //stop and cleat task | |
| 1091 | 1093 | if Assigned(@ATaskHandle) then begin | |
| 1092 | 1094 | DAQmxStopTask(ATaskHandle); | |
| 1093 | 1095 | DAQmxClearTask(ATaskHandle); | |
| 1094 | 1096 | end; | |
| 1095 | 1097 | //stop program | |
| 1096 | 1098 | Halt; | |
| 1097 | 1099 | end; | |
| 1098 | 1100 | end; | |
| 1099 | 1101 | {*******************MAIN PROGRAM WITH ERROR HANDLING*************************} | |
| 1100 | 1102 | begin | |
| 1101 | 1103 | //create couter task | |
| 1102 | 1104 | ErrorCheck(DAQmxCreateTask('Counter Task',@CtrTaskHandle),CtrTaskHandle); | |
| 1103 | 1105 | //create counter output channel | |
| 1104 | 1106 | //Frequency 1000 Hz, duty 0.5 | |
| 1105 | 1107 | ErrorCheck(DAQmxCreateCOPulseChanFreq(CtrTaskHandle,'Dev1/ctr0','',DAQmx_Val_Hz,DAQmx_Val_Low,0,10000,0.5),CtrTaskHandle); | |
| 1106 | 1108 | //timing 1000 samples per second | |
| 1107 | 1109 | ErrorCheck(DAQmxCfgImplicitTiming(CtrTaskHandle,DAQmx_Val_ContSamps,1000),CtrTaskHandle); | |
| 1108 | 1110 | //start task | |
| 1109 | 1111 | ErrorCheck(DAQmxStartTask(CtrTaskHandle),CtrTaskHandle); | |
| 1110 | 1112 | ||
| 1111 | 1113 | writeln('Press any key to stop generation'); | |
| 1112 | 1114 | repeat | |
| 1113 | 1115 | until keypressed; | |
| 1114 | 1116 | ||
| 1115 | 1117 | DAQmxStopTask(CtrTaskHandle); | |
| 1116 | 1118 | DAQmxClearTask(CtrTaskHandle); | |
| 1117 | 1119 | end. | |
| 1118 | 1120 | ``` | |
| 1119 | 1121 | ||
| 1120 | 1122 | #### Event counting | |
| 1121 | 1123 | ||
| 1122 | 1124 | In this example first counter(Dev1/ctr0) is configurated as counter output for frequency generation, second (Dev1/ctr1) as counter input for pulse counting. First counter also used as clock source for second one. | |
| 1123 | 1125 | ||
| 1124 | 1126 | ``` pascal | |
| 1125 | 1127 | program eventcounting; | |
| 1126 | 1128 | ||
| 1127 | 1129 | {$mode objfpc}{$H+} | |
| 1128 | 1130 | ||
| 1129 | 1131 | uses | |
| 1130 | 1132 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1131 | 1133 | cthreads, | |
| 1132 | 1134 | {$ENDIF}{$ENDIF} | |
| 1133 | 1135 | Classes,nidaqmx,SysUtils,StrUtils,Crt | |
| 1134 | 1136 | { you can add units after this }; | |
| 1135 | 1137 | var | |
| 1136 | 1138 | CtrOutTaskHandle,CtrInTaskHandle:TaskHandle; | |
| 1137 | 1139 | reads:cardinal=0; | |
| 1138 | 1140 | data:array [0..999] of longint; | |
| 1139 | 1141 | i:longint; | |
| 1140 | 1142 | {************Error handling procedure ***************************************** | |
| 1141 | 1143 | Example: | |
| 1142 | 1144 | ErrorCheck(DAQmxCreateAIVoltageChan(AITaskHandle,'Dev0/ai0','',DAQmx_Val_RSE,-10.0,10.0,DAQmx_Val_Volts,NIL),AITaskHandle); | |
| 1143 | 1145 | ||
| 1144 | 1146 | *******************************************************************************} | |
| 1145 | 1147 | procedure ErrorCheck(AError:longint;ATaskHandle:longint); | |
| 1146 | 1148 | var | |
| 1147 | 1149 | errBuffer:array[0..2048] of char; | |
| 1148 | 1150 | begin | |
| 1149 | 1151 | //get error description | |
| 1150 | 1152 | if Aerror<>0 then begin | |
| 1151 | 1153 | DAQmxGetExtendedErrorInfo(@ErrBuffer[0],2048); | |
| 1152 | 1154 | writeln(PChar(@ErrBuffer[0])); | |
| 1153 | 1155 | //stop and cleat task | |
| 1154 | 1156 | if Assigned(@ATaskHandle) then begin | |
| 1155 | 1157 | DAQmxStopTask(ATaskHandle); | |
| 1156 | 1158 | DAQmxClearTask(ATaskHandle); | |
| 1157 | 1159 | end; | |
| 1158 | 1160 | //stop program | |
| 1159 | 1161 | Halt; | |
| 1160 | 1162 | end; | |
| 1161 | 1163 | end; | |
| 1162 | 1164 | {*******************MAIN PROGRAM WITH ERROR HANDLING*************************} | |
| 1163 | 1165 | begin | |
| 1164 | 1166 | {****************CREATE COUNTER OUTPUT***************************************} | |
| 1165 | 1167 | //create couter output task | |
| 1166 | 1168 | ErrorCheck(DAQmxCreateTask('Counter output task',@CtrOutTaskHandle),CtrOutTaskHandle); | |
| 1167 | 1169 | //create counter output channel | |
| 1168 | 1170 | //Frequency 1000 Hz, duty 0.5 | |
| 1169 | 1171 | ErrorCheck(DAQmxCreateCOPulseChanFreq(CtrOutTaskHandle,'Dev1/ctr0','',DAQmx_Val_Hz,DAQmx_Val_Low,0,10000,0.5),CtrOutTaskHandle); | |
| 1170 | 1172 | //timing 1000 samples per second | |
| 1171 | 1173 | ErrorCheck(DAQmxCfgImplicitTiming(CtrOutTaskHandle,DAQmx_Val_ContSamps,1000),CtrOutTaskHandle); | |
| 1172 | 1174 | //start counter output task | |
| 1173 | 1175 | ErrorCheck(DAQmxStartTask(CtrOutTaskHandle),CtrOutTaskHandle); | |
| 1174 | 1176 | ||
| 1175 | 1177 | {*****************CREATE COUNTER INPUT***************************************} | |
| 1176 | 1178 | //create couter output task | |
| 1177 | 1179 | ErrorCheck(DAQmxCreateTask('Counter input task',@CtrInTaskHandle),CtrInTaskHandle); | |
| 1178 | 1180 | //create input counter for pulse counting | |
| 1179 | 1181 | ErrorCheck(DAQmxCreateCICountEdgesChan(CtrInTaskHandle,'Dev1/ctr1','',DAQmx_Val_Rising,0,DAQmx_Val_CountUp),CtrInTaskHandle); | |
| 1180 | 1182 | //timing | |
| 1181 | 1183 | ErrorCheck(DAQmxCfgSampClkTiming(CtrInTaskHandle,'/Dev1/Ctr0InternalOutput',1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000),CtrInTaskHandle); | |
| 1182 | 1184 | //start counter input task | |
| 1183 | 1185 | ErrorCheck(DAQmxStartTask(CtrInTaskHandle),CtrInTaskHandle); | |
| 1184 | 1186 | //counting pulses | |
| 1185 | 1187 | ErrorCheck(DAQmxReadCounterU32(CtrIntaskHandle,1000,10.0,@data,1000,@reads,Nil),CtrIntaskHandle); | |
| 1186 | 1188 | writeln('acquired samples: ',reads); | |
| 1187 | 1189 | for i:=0 to 10 do writeln('sample[',i,'] = ',data[i]); | |
| 1188 | 1190 | ||
| 1189 | 1191 | //stop and clear tasks | |
| 1190 | 1192 | DAQmxStopTask(CtrInTaskHandle); | |
| 1191 | 1193 | DAQmxClearTask(CtrInTaskHandle); | |
| 1192 | 1194 | DAQmxStopTask(CtrOutTaskHandle); | |
| 1193 | 1195 | DAQmxClearTask(CtrOutTaskHandle); | |
| 1194 | 1196 | end. | |
| 1195 | 1197 | ``` | |
| 1196 | 1198 | ||
| 1197 | 1199 | ### TESTED HARDWARE | |
| 1198 | 1200 | ||
| 1199 | 1201 | Examples were tested under Linux 32-bit (kernel 2.6.32) NI-DAQmx 8.0.2 and Windows 7 32-bit NI-DAQmx 14 on cards: | |
| 1200 | 1202 | ||
| 1201 | 1203 | - **NI PCI-6250** | |
| 1202 | 1204 | - **NI PCI-6224** | |
| 1203 | 1205 | ||
| 1204 | 1206 | - **NI PXIe-6612 (Windows only)** | |
| 1205 | 1207 | ||
| 1206 | 1208 | - **NI USB-6356 (Windows only)** | |
| 1207 | 1209 | ||
| 1208 | 1210 | - **NI USB-6003 (Windows only)** | |
| 1209 | 1211 | ||
| 1210 | 1212 | ## NI-DAQxmxBase examples | |
| 1211 | 1213 | ||
| 1212 | 1214 | Generally, NI-DAQmxBase is a light version of NI-DAQmx, but this library is **multiplatform**. You can use it under all supported operation systems without modification of NI-DAQmxBase function call. | |
| 1213 | 1215 | ||
| 1214 | 1216 | **Read readme file to check card supported features!!!** | |
| 1215 | 1217 | ||
| 1216 | 1218 | ### DEVICE INFO | |
| 1217 | 1219 | ||
| 1218 | 1220 | Functions, which provide device and channel information, are not implemented in this library, yet. | |
| 1219 | 1221 | ||
| 1220 | 1222 | ### ANALOG INPUTS/OUTPUTS | |
| 1221 | 1223 | ||
| 1222 | 1224 | #### Acquire finite number of samples from one analog channel | |
| 1223 | 1225 | ||
| 1224 | 1226 | ``` pascal | |
| 1225 | 1227 | program aichannel; | |
| 1226 | 1228 | ||
| 1227 | 1229 | {$mode objfpc}{$H+} | |
| 1228 | 1230 | ||
| 1229 | 1231 | uses | |
| 1230 | 1232 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1231 | 1233 | cthreads, | |
| 1232 | 1234 | {$ENDIF}{$ENDIF} | |
| 1233 | 1235 | Classes,nidaqmxbase; | |
| 1234 | 1236 | { you can add units after this } | |
| 1235 | 1237 | var | |
| 1236 | 1238 | AITaskHandle: PTaskHandle; | |
| 1237 | 1239 | NiBool:Pnibool; | |
| 1238 | 1240 | reads:longint=0; | |
| 1239 | 1241 | i:byte; | |
| 1240 | 1242 | data:array [1..1000] of double; | |
| 1241 | 1243 | procedure ErrorCheck(AError:longint;ATaskHandle:PTaskHandle); | |
| 1242 | 1244 | var | |
| 1243 | 1245 | errBuffer:array[0..2048] of char; | |
| 1244 | 1246 | errMessage:string; | |
| 1245 | 1247 | begin | |
| 1246 | 1248 | //get error description | |
| 1247 | 1249 | if DAQmxFailed(AError) = niTrue then begin | |
| 1248 | 1250 | DAQmxBaseGetExtendedErrorInfo(ErrBuffer,2048); | |
| 1249 | 1251 | writeln(ErrBuffer); | |
| 1250 | 1252 | //stop and cleat task | |
| 1251 | 1253 | if Assigned(ATaskHandle) then begin | |
| 1252 | 1254 | DAQmxBaseStopTask(ATaskHandle); | |
| 1253 | 1255 | DAQmxBaseClearTask(ATaskHandle); | |
| 1254 | 1256 | end; | |
| 1255 | 1257 | //stop program | |
| 1256 | 1258 | Halt; | |
| 1257 | 1259 | end; | |
| 1258 | 1260 | end; | |
| 1259 | 1261 | ||
| 1260 | 1262 | begin | |
| 1261 | 1263 | //create task | |
| 1262 | 1264 | ErrorCheck(DAQmxBaseCreateTask ('', AITaskHandle),AITaskHandle); | |
| 1263 | 1265 | //create analog input channel | |
| 1264 | 1266 | ErrorCheck(DAQmxBaseCreateAIVoltageChan(AITaskHandle,'Dev1/ai0','', DAQmx_Val_RSE,-10, 10,DAQmx_Val_Volts,''),AITaskHandle); | |
| 1265 | 1267 | //timing | |
| 1266 | 1268 | //clock frequency 1000 Hz, sample rate 1000 per second | |
| 1267 | 1269 | //if you use more than one active channel, generally sample rate = clock frequency/(active analog channles*10); | |
| 1268 | 1270 | ErrorCheck(DAQmxBaseCfgSampClkTiming (AITaskHandle,'OnboardClock',1000,DAQmx_Val_Rising,DAQmx_Val_FiniteSamps,1000),AITaskHandle); | |
| 1269 | 1271 | //start task | |
| 1270 | 1272 | ErrorCheck(DAQmxBaseStartTask(AITaskHandle),AITaskHandle); | |
| 1271 | 1273 | //read 1000 samples | |
| 1272 | 1274 | ErrorCheck(DAQmxBaseReadAnalogF64 (AITaskHandle, 1000, 10.0,DAQmx_Val_GroupByChannel,data,1000,reads,NIBool^),AITaskHandle); | |
| 1273 | 1275 | //acquired sample number | |
| 1274 | 1276 | writeln(reads,' samples were acqiured'); | |
| 1275 | 1277 | //show value of first ten samples | |
| 1276 | 1278 | for i:=1 to 10 do writeln('sample[',i,'] = ',data[i]:4:2,' V'); | |
| 1277 | 1279 | //stop and clear task | |
| 1278 | 1280 | DAQmxBaseStopTask(AITaskHandle); | |
| 1279 | 1281 | DAQmxBaseClearTask(AITaskHandle); | |
| 1280 | 1282 | end. | |
| 1281 | 1283 | ``` | |
| 1282 | 1284 | ||
| 1283 | 1285 | Output example +5V connected to pin 68 on NI PCI-6250 | |
| 1284 | 1286 | ||
| 1285 | 1287 | ```text | |
| 1286 | 1288 | 1000 samples were acqiured | |
| 1287 | 1289 | sample[1] = 5.07 V | |
| 1288 | 1290 | sample[2] = 5.06 V | |
| 1289 | 1291 | sample[3] = 5.06 V | |
| 1290 | 1292 | sample[4] = 5.06 V | |
| 1291 | 1293 | sample[5] = 5.06 V | |
| 1292 | 1294 | sample[6] = 5.06 V | |
| 1293 | 1295 | sample[7] = 5.06 V | |
| 1294 | 1296 | sample[8] = 5.06 V | |
| 1295 | 1297 | sample[9] = 5.06 V | |
| 1296 | 1298 | sample[10] = 5.07 V | |
| 1297 | 1299 | ``` | |
| 1298 | 1300 | ||
| 1299 | 1301 | #### Continuously acquire samples from three analog inputs | |
| 1300 | 1302 | ||
| 1301 | 1303 | ``` pascal | |
| 1302 | 1304 | program ai3contacq; | |
| 1303 | 1305 | ||
| 1304 | 1306 | {$mode objfpc}{$H+} | |
| 1305 | 1307 | ||
| 1306 | 1308 | uses | |
| 1307 | 1309 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1308 | 1310 | cthreads, | |
| 1309 | 1311 | {$ENDIF}{$ENDIF} | |
| 1310 | 1312 | Classes,nidaqmxbase; | |
| 1311 | 1313 | { you can add units after this } | |
| 1312 | 1314 | var | |
| 1313 | 1315 | AITaskHandle: PTaskHandle; | |
| 1314 | 1316 | NiBool:Pnibool; | |
| 1315 | 1317 | reads:longint=0; | |
| 1316 | 1318 | totalreads:longint=0; | |
| 1317 | 1319 | i:byte; | |
| 1318 | 1320 | data:array of double; | |
| 1319 | 1321 | acqnumsamples:longint; | |
| 1320 | 1322 | procedure ErrorCheck(AError:longint;ATaskHandle:PTaskHandle); | |
| 1321 | 1323 | var | |
| 1322 | 1324 | errBuffer:array[0..2048] of char; | |
| 1323 | 1325 | errMessage:string; | |
| 1324 | 1326 | begin | |
| 1325 | 1327 | //get error description | |
| 1326 | 1328 | if DAQmxFailed(AError) = niTrue then begin | |
| 1327 | 1329 | DAQmxBaseGetExtendedErrorInfo(ErrBuffer,2048); | |
| 1328 | 1330 | writeln(ErrBuffer); | |
| 1329 | 1331 | //stop and cleat task | |
| 1330 | 1332 | if Assigned(ATaskHandle) then begin | |
| 1331 | 1333 | DAQmxBaseStopTask(ATaskHandle); | |
| 1332 | 1334 | DAQmxBaseClearTask(ATaskHandle); | |
| 1333 | 1335 | end; | |
| 1334 | 1336 | //stop program | |
| 1335 | 1337 | Halt; | |
| 1336 | 1338 | end; | |
| 1337 | 1339 | end; | |
| 1338 | 1340 | ||
| 1339 | 1341 | begin | |
| 1340 | 1342 | //enter number of total samples for each channel to be acquired | |
| 1341 | 1343 | write('Enter a number of samples for each channel to be acquired: '); | |
| 1342 | 1344 | readln(acqnumsamples); | |
| 1343 | 1345 | //Set buffer size = length of data array:=number of active channels * sample per channel | |
| 1344 | 1346 | Setlength(data,3*1000); | |
| 1345 | 1347 | //create task | |
| 1346 | 1348 | ErrorCheck(DAQmxBaseCreateTask ('', AITaskHandle),AITaskHandle); | |
| 1347 | 1349 | //create analog input channel - 3 active analog inputs | |
| 1348 | 1350 | ErrorCheck(DAQmxBaseCreateAIVoltageChan(AITaskHandle,'Dev1/ai0:2','', DAQmx_Val_RSE,-10, 10,DAQmx_Val_Volts,''),AITaskHandle); | |
| 1349 | 1351 | //timing | |
| 1350 | 1352 | //clock frequency 10000 Hz, sample rate 1000 per second | |
| 1351 | 1353 | //if you use more than one active channel, generally sample rate = clock frequency/(active analog channles*10); | |
| 1352 | 1354 | ErrorCheck(DAQmxBaseCfgSampClkTiming (AITaskHandle,'OnboardClock',10000,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000),AITaskHandle); | |
| 1353 | 1355 | //start task | |
| 1354 | 1356 | ErrorCheck(DAQmxBaseStartTask(AITaskHandle),AITaskHandle); | |
| 1355 | 1357 | //read samples | |
| 1356 | 1358 | while totalreads<acqnumsamples do begin | |
| 1357 | 1359 | ErrorCheck(DAQmxBaseReadAnalogF64(AITaskHandle, 1000, 10.0, DAQmx_Val_GroupByChannel,data,Length(data),reads,NIBool^),AITaskHandle); | |
| 1358 | 1360 | totalreads:=totalreads+reads; | |
| 1359 | 1361 | writeln('total samples readed for each channel: ',totalreads); | |
| 1360 | 1362 | //show first five values for each channel | |
| 1361 | 1363 | for i:=0 to 5 do writeln('chan1[',i,'] = ',data[i]:4:2,' V ','chan2[',i,'] = ',data[i+1000]:4:2,' V ','chan3[',i,'] = ',data[i+2000]:4:2,' V '); | |
| 1362 | 1364 | end; | |
| 1363 | 1365 | //stop and clear task | |
| 1364 | 1366 | DAQmxBaseStopTask(AITaskHandle); | |
| 1365 | 1367 | DAQmxBaseClearTask(AITaskHandle); | |
| 1366 | 1368 | end. | |
| 1367 | 1369 | ``` | |
| 1368 | 1370 | ||
| 1369 | 1371 | Output example +5V connected to pin 68 on NI PCI-6250 | |
| 1370 | 1372 | ||
| 1371 | 1373 | ```text | |
| 1372 | 1374 | Enter a number of samples for each channel to be acquired: 2000 | |
| 1373 | 1375 | total samples readed for each channel: 1000 | |
| 1374 | 1376 | chan1[0] = 5.07 V chan2[0] = 0.00 V chan3[0] = 0.03 V | |
| 1375 | 1377 | chan1[1] = 5.06 V chan2[1] = -0.00 V chan3[1] = 0.03 V | |
| 1376 | 1378 | chan1[2] = 5.07 V chan2[2] = -0.00 V chan3[2] = 0.02 V | |
| 1377 | 1379 | chan1[3] = 5.07 V chan2[3] = -0.00 V chan3[3] = 0.02 V | |
| 1378 | 1380 | chan1[4] = 5.06 V chan2[4] = -0.00 V chan3[4] = 0.02 V | |
| 1379 | 1381 | chan1[5] = 5.06 V chan2[5] = -0.00 V chan3[5] = 0.01 V | |
| 1380 | 1382 | total samples readed for each channel: 2000 | |
| 1381 | 1383 | chan1[0] = 5.06 V chan2[0] = -0.00 V chan3[0] = -0.02 V | |
| 1382 | 1384 | chan1[1] = 5.07 V chan2[1] = -0.00 V chan3[1] = -0.02 V | |
| 1383 | 1385 | chan1[2] = 5.06 V chan2[2] = -0.00 V chan3[2] = -0.02 V | |
| 1384 | 1386 | chan1[3] = 5.07 V chan2[3] = -0.00 V chan3[3] = -0.02 V | |
| 1385 | 1387 | chan1[4] = 5.06 V chan2[4] = -0.00 V chan3[4] = -0.02 V | |
| 1386 | 1388 | chan1[5] = 5.06 V chan2[5] = -0.00 V chan3[5] = -0.02 V | |
| 1387 | 1389 | ``` | |
| 1388 | 1390 | ||
| 1389 | 1391 | ### DIGITAL INPUTS/OUTPUTS | |
| 1390 | 1392 | ||
| 1391 | 1393 | Read readme file to check supported card features by NI-DAQmxBase. | |
| 1392 | 1394 | ||
| 1393 | 1395 | In case of M-series cards, correlated digital input/output is limited to about 100kHz, because DMA data transfer is not supported. | |
| 1394 | 1396 | ||
| 1395 | 1397 | #### Software timed digital input | |
| 1396 | 1398 | ||
| 1397 | 1399 | ``` pascal | |
| 1398 | 1400 | program diport; | |
| 1399 | 1401 | ||
| 1400 | 1402 | {$mode objfpc}{$H+} | |
| 1401 | 1403 | ||
| 1402 | 1404 | uses | |
| 1403 | 1405 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1404 | 1406 | cthreads, | |
| 1405 | 1407 | {$ENDIF}{$ENDIF} | |
| 1406 | 1408 | Classes,nidaqmxbase,StrUtils | |
| 1407 | 1409 | { you can add units after this }; | |
| 1408 | 1410 | var | |
| 1409 | 1411 | DITaskHandle:PTaskHandle; | |
| 1410 | 1412 | NiBool:Pnibool; | |
| 1411 | 1413 | data:byte; | |
| 1412 | 1414 | reads:longint=0; | |
| 1413 | 1415 | procedure ErrorCheck(AError:longint;ATaskHandle:PTaskHandle); | |
| 1414 | 1416 | var | |
| 1415 | 1417 | errBuffer:array[0..2048] of char; | |
| 1416 | 1418 | begin | |
| 1417 | 1419 | //get error description | |
| 1418 | 1420 | if DAQmxFailed(AError) = niTrue then begin | |
| 1419 | 1421 | DAQmxBaseGetExtendedErrorInfo(ErrBuffer,2048); | |
| 1420 | 1422 | writeln(ErrBuffer); | |
| 1421 | 1423 | //stop and cleat task | |
| 1422 | 1424 | if Assigned(ATaskHandle) then begin | |
| 1423 | 1425 | DAQmxBaseStopTask(ATaskHandle); | |
| 1424 | 1426 | DAQmxBaseClearTask(ATaskHandle); | |
| 1425 | 1427 | end; | |
| 1426 | 1428 | //stop program | |
| 1427 | 1429 | Halt; | |
| 1428 | 1430 | end; | |
| 1429 | 1431 | end; | |
| 1430 | 1432 | begin | |
| 1431 | 1433 | //create task | |
| 1432 | 1434 | ErrorCheck(DAQmxBaseCreateTask('', DITaskHandle),DITaskHandle); | |
| 1433 | 1435 | //create digital input port | |
| 1434 | 1436 | ErrorCheck(DAQmxBaseCreateDIChan(DITaskHandle,'Dev1/port0','',DAQmx_Val_ChanForAllLines),DITaskHandle); | |
| 1435 | 1437 | //start task | |
| 1436 | 1438 | ErrorCheck(DAQmxBaseStartTask(DITaskHandle),DITaskHandle); | |
| 1437 | 1439 | //read dat form port | |
| 1438 | 1440 | //use DAQmxBaseReadDigitalU8 for 8 bit port | |
| 1439 | 1441 | //use DAQmxBaseReadDigitalU32 for 32 bit port | |
| 1440 | 1442 | ErrorCheck(DAQmxBaseReadDigitalU8(DITaskHandle,1,10.0,DAQmx_Val_GroupByChannel,data,1,reads,NiBool^),DITaskHandle); | |
| 1441 | 1443 | writeln('digital port value %',IntToBin(data,8)); | |
| 1442 | 1444 | //stop and clear task | |
| 1443 | 1445 | DAQmxBaseStopTask(DITaskHandle); | |
| 1444 | 1446 | DAQmxBaseClearTask(DITaskHandle); | |
| 1445 | 1447 | end. | |
| 1446 | 1448 | ``` | |
| 1447 | 1449 | ||
| 1448 | 1450 | Output example: +5V connected to line 1 of 8-bit digital port | |
| 1449 | 1451 | ||
| 1450 | 1452 | ```text | |
| 1451 | 1453 | digital port value %00000010 | |
| 1452 | 1454 | ``` | |
| 1453 | 1455 | ||
| 1454 | 1456 | #### Hardware timed digital input | |
| 1455 | 1457 | ||
| 1456 | 1458 | ``` pascal | |
| 1457 | 1459 | program diporthwtimed; | |
| 1458 | 1460 | ||
| 1459 | 1461 | {$mode objfpc}{$H+} | |
| 1460 | 1462 | ||
| 1461 | 1463 | uses | |
| 1462 | 1464 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1463 | 1465 | cthreads, | |
| 1464 | 1466 | {$ENDIF}{$ENDIF} | |
| 1465 | 1467 | Classes,nidaqmxbase,StrUtils | |
| 1466 | 1468 | { you can add units after this }; | |
| 1467 | 1469 | var | |
| 1468 | 1470 | DITaskHandle,CtrTaskHandle:PTaskHandle; | |
| 1469 | 1471 | NiBool:Pnibool; | |
| 1470 | 1472 | data:array of byte; | |
| 1471 | 1473 | reads:longint=0; | |
| 1472 | 1474 | i:longint; | |
| 1473 | 1475 | procedure ErrorCheck(AError:longint;ATaskHandle:PTaskHandle); | |
| 1474 | 1476 | var | |
| 1475 | 1477 | errBuffer:array[0..2048] of char; | |
| 1476 | 1478 | begin | |
| 1477 | 1479 | //get error description | |
| 1478 | 1480 | if DAQmxFailed(AError) = niTrue then begin | |
| 1479 | 1481 | DAQmxBaseGetExtendedErrorInfo(ErrBuffer,2048); | |
| 1480 | 1482 | writeln(ErrBuffer); | |
| 1481 | 1483 | //stop and cleat task | |
| 1482 | 1484 | if Assigned(ATaskHandle) then begin | |
| 1483 | 1485 | DAQmxBaseStopTask(ATaskHandle); | |
| 1484 | 1486 | DAQmxBaseClearTask(ATaskHandle); | |
| 1485 | 1487 | end; | |
| 1486 | 1488 | //stop program | |
| 1487 | 1489 | Halt; | |
| 1488 | 1490 | end; | |
| 1489 | 1491 | end; | |
| 1490 | 1492 | begin | |
| 1491 | 1493 | ||
| 1492 | 1494 | {********CONFIGURE COUNTER TO USE AS CLOCK FOR DIGITAL INPUT*****************} | |
| 1493 | 1495 | //create counter task | |
| 1494 | 1496 | ErrorCheck(DAQmxBaseCreateTask('', CtrTaskHandle),CtrTaskHandle); | |
| 1495 | 1497 | //create counter output channel | |
| 1496 | 1498 | //Frequency 10000 Hz, duty 0.5 | |
| 1497 | 1499 | ErrorCheck(DAQmxBaseCreateCOPulseChanFreq(CtrTaskHandle,'Dev1/ctr0','',DAQmx_Val_Hz,DAQmx_Val_Low,0,10000,0.5),CtrTaskHandle); | |
| 1498 | 1500 | //timing | |
| 1499 | 1501 | //last value is not important for continuesly sample mode, for finite sample mode = generate 10000 samples | |
| 1500 | 1502 | ErrorCheck(DAQmxBaseCfgImplicitTiming(CtrTaskHandle,DAQmx_Val_ContSamps,10000),CtrTaskHandle); | |
| 1501 | 1503 | //start counter task | |
| 1502 | 1504 | ErrorCheck(DAQmxBaseStartTask(CtrTaskHandle),CtrTaskHandle); | |
| 1503 | 1505 | ||
| 1504 | 1506 | {*******************HW TIMED DIGITAL INPUT*********************************** | |
| 1505 | 1507 | **********LIMITED TO 10kHz,DMA IS NOT IMPLEMENTED****************************} | |
| 1506 | 1508 | //configure buffer for 1000 samples | |
| 1507 | 1509 | SetLength(data,1000); | |
| 1508 | 1510 | //create task | |
| 1509 | 1511 | ErrorCheck(DAQmxBaseCreateTask('', DITaskHandle),DITaskHandle); | |
| 1510 | 1512 | //create digital input port | |
| 1511 | 1513 | ErrorCheck(DAQmxBaseCreateDIChan(DITaskHandle,'Dev1/port0','',DAQmx_Val_ChanForAllLines),DITaskHandle); | |
| 1512 | 1514 | //timing frequency 10000Hz, 1000 sample per channel | |
| 1513 | 1515 | ErrorCheck(DAQmxBaseCfgSampClkTiming(DITaskHandle,'/Dev1/Ctr0InternalOutput',10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,10000),DITaskHandle); | |
| 1514 | 1516 | //start task | |
| 1515 | 1517 | ErrorCheck(DAQmxBaseStartTask(DITaskHandle),DITaskHandle); | |
| 1516 | 1518 | //read dat form port | |
| 1517 | 1519 | //use DAQmxBaseReadDigitalU8 for 8 bit port | |
| 1518 | 1520 | //use DAQmxBaseReadDigitalU32 for 32 bit port | |
| 1519 | 1521 | ErrorCheck(DAQmxBaseReadDigitalU8(DITaskHandle,10000,10.0,DAQmx_Val_GroupByChannel,data,10000,reads,NiBool^),DITaskHandle); | |
| 1520 | 1522 | writeln(reads,' samples were readed'); | |
| 1521 | 1523 | //show first 10 samples | |
| 1522 | 1524 | for i:=0 to 9 do writeln('sample[',i,'] = %',IntToBin(data[i],8)); | |
| 1523 | 1525 | //stop and clear task | |
| 1524 | 1526 | DAQmxBaseStopTask(DITaskHandle); | |
| 1525 | 1527 | DAQmxBaseClearTask(DITaskHandle); | |
| 1526 | 1528 | DAQmxBaseStopTask(CtrTaskHandle); | |
| 1527 | 1529 | DAQmxBaseClearTask(CtrTaskHandle); | |
| 1528 | 1530 | end. | |
| 1529 | 1531 | ``` | |
| 1530 | 1532 | ||
| 1531 | 1533 | Output example: +5V connected to line 1 of 8-bit digital port, executed with time command under Linux. | |
| 1532 | 1534 | ||
| 1533 | 1535 | ```text | |
| 1534 | 1536 | 10000 samples were readed | |
| 1535 | 1537 | sample[0] = %00000010 | |
| 1536 | 1538 | sample[1] = %00000010 | |
| 1537 | 1539 | sample[2] = %00000010 | |
| 1538 | 1540 | sample[3] = %00000010 | |
| 1539 | 1541 | sample[4] = %00000010 | |
| 1540 | 1542 | sample[5] = %00000010 | |
| 1541 | 1543 | sample[6] = %00000010 | |
| 1542 | 1544 | sample[7] = %00000010 | |
| 1543 | 1545 | sample[8] = %00000010 | |
| 1544 | 1546 | sample[9] = %00000010 | |
| 1545 | 1547 | ||
| 1546 | 1548 | real 0m4.483s | |
| 1547 | 1549 | user 0m4.261s | |
| 1548 | 1550 | sys 0m0.100s | |
| 1549 | 1551 | ``` | |
| 1550 | 1552 | ||
| 1551 | 1553 | #### Software timed digital output | |
| 1552 | 1554 | ||
| 1553 | 1555 | ``` pascal | |
| 1554 | 1556 | program doport; | |
| 1555 | 1557 | ||
| 1556 | 1558 | {$mode objfpc}{$H+} | |
| 1557 | 1559 | ||
| 1558 | 1560 | uses | |
| 1559 | 1561 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1560 | 1562 | cthreads, | |
| 1561 | 1563 | {$ENDIF}{$ENDIF} | |
| 1562 | 1564 | Classes,nidaqmxbase,StrUtils,SysUtils | |
| 1563 | 1565 | { you can add units after this }; | |
| 1564 | 1566 | var | |
| 1565 | 1567 | DOTaskHandle,CtrTaskHandle:PTaskHandle; | |
| 1566 | 1568 | NiBool:Pnibool; | |
| 1567 | 1569 | data: byte; | |
| 1568 | 1570 | written:longint=0; | |
| 1569 | 1571 | i:longint; | |
| 1570 | 1572 | procedure ErrorCheck(AError:longint;ATaskHandle:PTaskHandle); | |
| 1571 | 1573 | var | |
| 1572 | 1574 | errBuffer:array[0..2048] of char; | |
| 1573 | 1575 | begin | |
| 1574 | 1576 | //get error description | |
| 1575 | 1577 | if DAQmxFailed(AError) = niTrue then begin | |
| 1576 | 1578 | DAQmxBaseGetExtendedErrorInfo(ErrBuffer,2048); | |
| 1577 | 1579 | writeln(ErrBuffer); | |
| 1578 | 1580 | //stop and cleat task | |
| 1579 | 1581 | if Assigned(ATaskHandle) then begin | |
| 1580 | 1582 | DAQmxBaseStopTask(ATaskHandle); | |
| 1581 | 1583 | DAQmxBaseClearTask(ATaskHandle); | |
| 1582 | 1584 | end; | |
| 1583 | 1585 | //stop program | |
| 1584 | 1586 | Halt; | |
| 1585 | 1587 | end; | |
| 1586 | 1588 | end; | |
| 1587 | 1589 | begin | |
| 1588 | 1590 | //write data to buffer, produce digital waveform on all port lines | |
| 1589 | 1591 | data:=255; | |
| 1590 | 1592 | //create task | |
| 1591 | 1593 | ErrorCheck(DAQmxBaseCreateTask('', DOTaskHandle),DOTaskHandle); | |
| 1592 | 1594 | //create digital input port | |
| 1593 | 1595 | ErrorCheck(DAQmxBaseCreateDOChan(DOTaskHandle,'Dev1/port0','',DAQmx_Val_ChanForAllLines),DOTaskHandle); | |
| 1594 | 1596 | //use DAQmxBaseWriteDigitalU8 for 8 bit port | |
| 1595 | 1597 | //use DAQmxBaseWriteDigitalU32 for 32 bit port | |
| 1596 | 1598 | //set third parametr to 1 to enable autostart or set to 0 and use DAQmxBaseStartTask before | |
| 1597 | 1599 | //set digital "1" | |
| 1598 | 1600 | writeln('set digital 1 on all port lines'); | |
| 1599 | 1601 | writeln('sleep 10s'); | |
| 1600 | 1602 | ErrorCheck(DAQmxBaseWriteDigitalU8(DOTaskHandle,1000,1,1000,DAQmx_Val_GroupByChannel,data,written,NiTrue),DOTaskHandle); | |
| 1601 | 1603 | sleep(10000); | |
| 1602 | 1604 | writeln(written,' samples were written'); | |
| 1603 | 1605 | //set digital "0" | |
| 1604 | 1606 | writeln('set digital 0 on all port lines'); | |
| 1605 | 1607 | writeln('sleep 10s'); | |
| 1606 | 1608 | sleep(10000); | |
| 1607 | 1609 | ErrorCheck(DAQmxBaseWriteDigitalU8(DOTaskHandle,1000,1,1000,DAQmx_Val_GroupByChannel,data,written,NiTrue),DOTaskHandle); | |
| 1608 | 1610 | writeln(written,' samples were written'); | |
| 1609 | 1611 | //stop and clear task | |
| 1610 | 1612 | DAQmxBaseStopTask(DOTaskHandle); | |
| 1611 | 1613 | DAQmxBaseClearTask(DOTaskHandle); | |
| 1612 | 1614 | DAQmxBaseStopTask(CtrTaskHandle); | |
| 1613 | 1615 | DAQmxBaseClearTask(CtrTaskHandle); | |
| 1614 | 1616 | end. | |
| 1615 | 1617 | ``` | |
| 1616 | 1618 | ||
| 1617 | 1619 | #### Hardware timed digital output | |
| 1618 | 1620 | ||
| 1619 | 1621 | Digital output generation frequency is limited to ~100kHz, because DMA is not supported by the driver . | |
| 1620 | 1622 | ||
| 1621 | 1623 | ``` pascal | |
| 1622 | 1624 | program doporthwtimed; | |
| 1623 | 1625 | ||
| 1624 | 1626 | {$mode objfpc}{$H+} | |
| 1625 | 1627 | ||
| 1626 | 1628 | uses | |
| 1627 | 1629 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1628 | 1630 | cthreads, | |
| 1629 | 1631 | {$ENDIF}{$ENDIF} | |
| 1630 | 1632 | Classes,nidaqmxbase,StrUtils | |
| 1631 | 1633 | { you can add units after this }; | |
| 1632 | 1634 | var | |
| 1633 | 1635 | DOTaskHandle,CtrTaskHandle:PTaskHandle; | |
| 1634 | 1636 | NiBool:Pnibool; | |
| 1635 | 1637 | data:array of byte; | |
| 1636 | 1638 | written:longint=0; | |
| 1637 | 1639 | i:longint; | |
| 1638 | 1640 | procedure ErrorCheck(AError:longint;ATaskHandle:PTaskHandle); | |
| 1639 | 1641 | var | |
| 1640 | 1642 | errBuffer:array[0..2048] of char; | |
| 1641 | 1643 | begin | |
| 1642 | 1644 | //get error description | |
| 1643 | 1645 | if DAQmxFailed(AError) = niTrue then begin | |
| 1644 | 1646 | DAQmxBaseGetExtendedErrorInfo(ErrBuffer,2048); | |
| 1645 | 1647 | writeln(ErrBuffer); | |
| 1646 | 1648 | //stop and cleat task | |
| 1647 | 1649 | if Assigned(ATaskHandle) then begin | |
| 1648 | 1650 | DAQmxBaseStopTask(ATaskHandle); | |
| 1649 | 1651 | DAQmxBaseClearTask(ATaskHandle); | |
| 1650 | 1652 | end; | |
| 1651 | 1653 | //stop program | |
| 1652 | 1654 | Halt; | |
| 1653 | 1655 | end; | |
| 1654 | 1656 | end; | |
| 1655 | 1657 | begin | |
| 1656 | 1658 | {********CONFIGURE COUNTER TO USE AS CLOCK FOR DIGITAL INPUT*****************} | |
| 1657 | 1659 | //create counter task | |
| 1658 | 1660 | ErrorCheck(DAQmxBaseCreateTask('', CtrTaskHandle),CtrTaskHandle); | |
| 1659 | 1661 | //create counter output channel | |
| 1660 | 1662 | //Frequency 10000 Hz, duty 0.5 | |
| 1661 | 1663 | ErrorCheck(DAQmxBaseCreateCOPulseChanFreq(CtrTaskHandle,'Dev1/ctr0','',DAQmx_Val_Hz,DAQmx_Val_Low,0,10000,0.5),CtrTaskHandle); | |
| 1662 | 1664 | //timing | |
| 1663 | 1665 | //last value is not important for continuesly sample mode, for finite sample mode = generate 10000 samples | |
| 1664 | 1666 | ErrorCheck(DAQmxBaseCfgImplicitTiming(CtrTaskHandle,DAQmx_Val_ContSamps,10000),CtrTaskHandle); | |
| 1665 | 1667 | //start counter task | |
| 1666 | 1668 | ErrorCheck(DAQmxBaseStartTask(CtrTaskHandle),CtrTaskHandle); | |
| 1667 | 1669 | {*******************HW TIMED DIGITAL INPUT*********************************** | |
| 1668 | 1670 | **********LIMITED TO 10kHz,DMA IS NOT IMPLEMENTED****************************} | |
| 1669 | 1671 | //configure buffer for 1000 samples | |
| 1670 | 1672 | SetLength(data,1000); | |
| 1671 | 1673 | //write data to buffer, produce digital waveform on all port lines | |
| 1672 | 1674 | for i:=0 to 999 do if (i mod 2 = 0) then data[i]:=255; | |
| 1673 | 1675 | //create task | |
| 1674 | 1676 | ErrorCheck(DAQmxBaseCreateTask('', DOTaskHandle),DOTaskHandle); | |
| 1675 | 1677 | //create digital input port | |
| 1676 | 1678 | ErrorCheck(DAQmxBaseCreateDOChan(DOTaskHandle,'Dev1/port0','',DAQmx_Val_ChanForAllLines),DOTaskHandle); | |
| 1677 | 1679 | //timing frequency 10000Hz, 1000 sample per channel | |
| 1678 | 1680 | ErrorCheck(DAQmxBaseCfgSampClkTiming(DOTaskHandle,'/Dev1/Ctr0InternalOutput',10000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000),DOTaskHandle); | |
| 1679 | 1681 | //use DAQmxBaseWriteDigitalU8 for 8 bit port | |
| 1680 | 1682 | //use DAQmxBaseWriteDigitalU32 for 32 bit port | |
| 1681 | 1683 | //set third parametr to 1 to enable autostart or set to 0 and use DAQmxBaseStartTask before | |
| 1682 | 1684 | ErrorCheck(DAQmxBaseWriteDigitalU8(DOTaskHandle,1000,1,1000,DAQmx_Val_GroupByChannel,data,written,NiTrue),DOTaskHandle); | |
| 1683 | 1685 | writeln(written,' samples were written'); | |
| 1684 | 1686 | //stop and clear task | |
| 1685 | 1687 | DAQmxBaseStopTask(DOTaskHandle); | |
| 1686 | 1688 | DAQmxBaseClearTask(DOTaskHandle); | |
| 1687 | 1689 | DAQmxBaseStopTask(CtrTaskHandle); | |
| 1688 | 1690 | DAQmxBaseClearTask(CtrTaskHandle); | |
| 1689 | 1691 | end. | |
| 1690 | 1692 | ``` | |
| 1691 | 1693 | ||
| 1692 | 1694 | ### COUNTER INPUTS/OUTPUTS | |
| 1693 | 1695 | ||
| 1694 | 1696 | Counter input of M-series cards is limited to computer speed. | |
| 1695 | 1697 | ||
| 1696 | 1698 | #### COUNTER OUTPUT | |
| 1697 | 1699 | ||
| 1698 | 1700 | Counter output can be timed by software or hardware. | |
| 1699 | 1701 | ||
| 1700 | 1702 | Example demontrate how to use couter ouput as frequency generator(clock signal for other hardware) or as basic pwm regulation. | |
| 1701 | 1703 | ||
| 1702 | 1704 | In this example counter output is hardware timed. | |
| 1703 | 1705 | ||
| 1704 | 1706 | ``` pascal | |
| 1705 | 1707 | program ctrout; | |
| 1706 | 1708 | ||
| 1707 | 1709 | {$mode objfpc}{$H+} | |
| 1708 | 1710 | ||
| 1709 | 1711 | uses | |
| 1710 | 1712 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1711 | 1713 | cthreads, | |
| 1712 | 1714 | {$ENDIF}{$ENDIF} | |
| 1713 | 1715 | Classes,nidaqmxbase,StrUtils,SysUtils | |
| 1714 | 1716 | { you can add units after this }; | |
| 1715 | 1717 | var | |
| 1716 | 1718 | CtrTaskHandle:PTaskHandle; | |
| 1717 | 1719 | NiBool:Pnibool; | |
| 1718 | 1720 | data:array of byte; | |
| 1719 | 1721 | reads:longint=0; | |
| 1720 | 1722 | procedure ErrorCheck(AError:longint;ATaskHandle:PTaskHandle); | |
| 1721 | 1723 | var | |
| 1722 | 1724 | errBuffer:array[0..2048] of char; | |
| 1723 | 1725 | begin | |
| 1724 | 1726 | //get error description | |
| 1725 | 1727 | if DAQmxFailed(AError) = niTrue then begin | |
| 1726 | 1728 | DAQmxBaseGetExtendedErrorInfo(ErrBuffer,2048); | |
| 1727 | 1729 | writeln(ErrBuffer); | |
| 1728 | 1730 | //stop and cleat task | |
| 1729 | 1731 | if Assigned(ATaskHandle) then begin | |
| 1730 | 1732 | DAQmxBaseStopTask(ATaskHandle); | |
| 1731 | 1733 | DAQmxBaseClearTask(ATaskHandle); | |
| 1732 | 1734 | end; | |
| 1733 | 1735 | //stop program | |
| 1734 | 1736 | Halt; | |
| 1735 | 1737 | end; | |
| 1736 | 1738 | end; | |
| 1737 | 1739 | begin | |
| 1738 | 1740 | ||
| 1739 | 1741 | {********CONFIGURE COUNTER TO USE AS CLOCK FOR DIGITAL INPUT*****************} | |
| 1740 | 1742 | //create counter task | |
| 1741 | 1743 | ErrorCheck(DAQmxBaseCreateTask('', CtrTaskHandle),CtrTaskHandle); | |
| 1742 | 1744 | //create counter output channel | |
| 1743 | 1745 | //Frequency 10000 Hz, duty 0.5 | |
| 1744 | 1746 | ErrorCheck(DAQmxBaseCreateCOPulseChanFreq(CtrTaskHandle,'Dev1/ctr0','',DAQmx_Val_Hz,DAQmx_Val_Low,0,10000,0.5),CtrTaskHandle); | |
| 1745 | 1747 | //timing | |
| 1746 | 1748 | //last value is not important for continuesly sample mode, for finite sample mode = generate 10000 samples | |
| 1747 | 1749 | ErrorCheck(DAQmxBaseCfgImplicitTiming(CtrTaskHandle,DAQmx_Val_ContSamps,10000),CtrTaskHandle); | |
| 1748 | 1750 | //start counter task | |
| 1749 | 1751 | ErrorCheck(DAQmxBaseStartTask(CtrTaskHandle),CtrTaskHandle); | |
| 1750 | 1752 | //generate frequency signal for 10 seconds | |
| 1751 | 1753 | sleep(10000); | |
| 1752 | 1754 | DAQmxBaseStopTask(CtrTaskHandle); | |
| 1753 | 1755 | DAQmxBaseClearTask(CtrTaskHandle); | |
| 1754 | 1756 | end. | |
| 1755 | 1757 | ``` | |
| 1756 | 1758 | ||
| 1757 | 1759 | #### COUNTER INPUT | |
| 1758 | 1760 | ||
| 1759 | 1761 | Counter input is only software operated. It is limited by hardware driver. | |
| 1760 | 1762 | ||
| 1761 | 1763 | ``` pascal | |
| 1762 | 1764 | program ctrin; | |
| 1763 | 1765 | ||
| 1764 | 1766 | {$mode objfpc}{$H+} | |
| 1765 | 1767 | ||
| 1766 | 1768 | uses | |
| 1767 | 1769 | {$IFDEF UNIX}{$IFDEF UseCThreads} | |
| 1768 | 1770 | cthreads, | |
| 1769 | 1771 | {$ENDIF}{$ENDIF} | |
| 1770 | 1772 | Classes,nidaqmxbase,StrUtils,SysUtils,Crt | |
| 1771 | 1773 | { you can add units after this }; | |
| 1772 | 1774 | var | |
| 1773 | 1775 | CtrInTaskHandle:PTaskHandle; | |
| 1774 | 1776 | NiBool:Pnibool; | |
| 1775 | 1777 | data:dword=0; | |
| 1776 | 1778 | reads:longint=0; | |
| 1777 | 1779 | procedure ErrorCheck(AError:longint;ATaskHandle:PTaskHandle); | |
| 1778 | 1780 | var | |
| 1779 | 1781 | errBuffer:array[0..2048] of char; | |
| 1780 | 1782 | begin | |
| 1781 | 1783 | //get error description | |
| 1782 | 1784 | if DAQmxFailed(AError) = niTrue then begin | |
| 1783 | 1785 | DAQmxBaseGetExtendedErrorInfo(ErrBuffer,2048); | |
| 1784 | 1786 | writeln(ErrBuffer); | |
| 1785 | 1787 | //stop and cleat task | |
| 1786 | 1788 | if Assigned(ATaskHandle) then begin | |
| 1787 | 1789 | DAQmxBaseStopTask(ATaskHandle); | |
| 1788 | 1790 | DAQmxBaseClearTask(ATaskHandle); | |
| 1789 | 1791 | end; | |
| 1790 | 1792 | //stop program | |
| 1791 | 1793 | Halt; | |
| 1792 | 1794 | end; | |
| 1793 | 1795 | end; | |
| 1794 | 1796 | begin | |
| 1795 | 1797 | {*****************CREATE COUNTER INPUT***************************************} | |
| 1796 | 1798 | //create couter input task | |
| 1797 | 1799 | ErrorCheck(DAQmxBaseCreateTask('Counter input task',CtrInTaskHandle),CtrInTaskHandle); | |
| 1798 | 1800 | //create input counter for pulse counting | |
| 1799 | 1801 | ErrorCheck(DAQmxBaseCreateCICountEdgesChan(CtrInTaskHandle,'Dev1/ctr1','',DAQmx_Val_Rising,0,DAQmx_Val_CountUp),CtrInTaskHandle); | |
| 1800 | 1802 | //timing | |
| 1801 | 1803 | //ErrorCheck(DAQmxBaseCfgSampClkTiming(CtrInTaskHandle,'/Dev1/Ctr0InternalOutput',1000.0,DAQmx_Val_Rising,DAQmx_Val_ContSamps,1000),CtrInTaskHandle); | |
| 1802 | 1804 | //start counter input task | |
| 1803 | 1805 | ErrorCheck(DAQmxBaseStartTask(CtrInTaskHandle),CtrInTaskHandle); | |
| 1804 | 1806 | //counting pulses | |
| 1805 | 1807 | writeln('press any key to stop counting'); | |
| 1806 | 1808 | repeat | |
| 1807 | 1809 | ErrorCheck(DAQmxBaseReadCounterU32(CtrIntaskHandle,1,10.0,data,1,reads,NiTrue),CtrIntaskHandle); | |
| 1808 | 1810 | writeln('counter value: ',data); | |
| 1809 | 1811 | until keypressed; | |
| 1810 | 1812 | //clear and stop task | |
| 1811 | 1813 | DAQmxBaseStopTask(CtrInTaskHandle); | |
| 1812 | 1814 | DAQmxBaseClearTask(CtrInTaskHandle); | |
| 1813 | 1815 | end. | |
| 1814 | 1816 | ``` | |
| 1815 | 1817 | ||
| 1816 | 1818 | ### TESTED HARDWARE | |
| 1817 | 1819 | ||
| 1818 | 1820 | Examples were tested under Scientific Linux 6.4, Mageia 2 32-bit and Windows 32-bit and should work under MacOS (**need test**) on cards: | |
| 1819 | 1821 | ||
| 1820 | 1822 | - **NI PCI-6250** | |
| 1821 | 1823 | - **NI PCI-6224** | |
| 1822 | 1824 | - **NI USB-6356** (Windows only) | |
| 1823 | 1825 | ||
| 1824 | 1826 | --- | |
| 1825 | 1827 | *From the Free Pascal wiki, page "NI-DAQmx and NI-DAQmx Base examples", as it stood on 2019-12-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 1826 | 1828 | Category: FPC in Science and Technology | |
| 1827 | 1829 | Category: Hardware | |
| 1828 | 1830 | Category: Measuring and Instrumentation |
