-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathljm.py
2703 lines (2077 loc) · 95.1 KB
/
ljm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Cross-platform wrapper for the LJM library.
"""
import ctypes
import sys
import constants
import errorcodes
STREAM_READ_CALLBACK = ctypes.CFUNCTYPE(None, ctypes.POINTER(ctypes.c_int))
class CallbackData:
def __init__(self, handle, callback):
self.callbackUser = callback
self.callbackWrapper = lambda arg: self.callbackUser(arg[0])
self.callbackLjm = STREAM_READ_CALLBACK(self.callbackWrapper)
self.argInner = ctypes.c_int(handle)
self.argRef = ctypes.byref(self.argInner)
# We need to keep references for the duration that stream is
# running, otherwise the garbage collector will delete them
# causing a segfault LJM tries to call our callback.
_g_callbackData = {}
class LJMError(Exception):
"""Custom exception class for LJM specific errors."""
def __init__(self, errorCode=None, errorAddress=None, errorString=None):
self._errorCode = errorCode
self._errorAddress = errorAddress
if errorString is None:
self._errorString = ""
try:
if self._errorCode is not None:
self._errorString = errorToString(self._errorCode)
except:
pass
else:
self._errorString = str(errorString)
@property
def errorCode(self):
return self._errorCode
@property
def errorAddress(self):
return self._errorAddress
@property
def errorString(self):
return self._errorString
def __str__(self):
addrStr = ""
errorCodeStr = ""
if self._errorAddress is not None:
addrStr = "Address " + str(self._errorAddress) + ", "
if self._errorCode is not None:
errorCodeStr = "LJM library "
if errorcodes.WARNINGS_BEGIN <= self._errorCode <= errorcodes.WARNINGS_END:
errorCodeStr += "warning"
else:
errorCodeStr += "error"
errorCodeStr += " code " + str(self._errorCode) + " "
return addrStr + errorCodeStr + self._errorString
def _loadLibrary():
"""Returns a ctypes pointer to the LJM library."""
try:
libraryName = None
try:
if(sys.platform.startswith("win32") or sys.platform.startswith("cygwin")):
# Windows
libraryName = "LabJackM.dll"
if(sys.platform.startswith("linux")):
# Linux
libraryName = "libLabJackM.so"
if(sys.platform.startswith("darwin")):
# Mac OS X
libraryName = "libLabJackM.dylib"
if libraryName is not None:
if libraryName == "LabJackM.dll" and sys.platform.startswith("win32"):
return ctypes.WinDLL(libraryName)
else:
return ctypes.CDLL(libraryName)
except Exception:
if(sys.platform.startswith("darwin")):
# Mac OS X load failed. Try with absolute path.
try:
libraryName = "/usr/local/lib/libLabJackM.dylib"
return ctypes.CDLL(libraryName)
except Exception:
pass
e = sys.exc_info()[1]
raise LJMError(errorString="Cannot load the LJM library "+str(libraryName)+". "+str(e))
# Unsupported operating system
raise LJMError(errorString="Cannot load the LJM library. Unsupported platform "+sys.platform+".")
except LJMError:
ljme = sys.exc_info()[1]
print(str(type(ljme)) + ": " + str(ljme))
return None
_staticLib = _loadLibrary()
def listAll(deviceType, connectionType):
"""Scans for LabJack devices and returns lists describing the
devices.
Args:
deviceType: An integer that filters which devices will be
returned (labjack.ljm.constants.dtT7,
labjack.ljm.constants.dtDIGIT, etc.).
labjack.ljm.constants.dtANY is allowed.
connectionType: An integer that filters by connection type
(labjack.ljm.constants.ctUSB, labjack.ljm.constants.ctTCP,
etc). labjack.ljm.constants.ctANY is allowed.
Returns:
A tuple containing:
(numFound, aDeviceTypes, aConnectionTypes, aSerialNumbers,
aIPAddresses)
numFound: Number of devices found.
aDeviceTypes: List of device types for each of the numFound
devices.
aConnectionTypes: List of connection types for each of the
numFound devices.
aSerialNumbers: List of serial numbers for each of the numFound
devices.
aIPAddresses: List of IP addresses for each of the numFound
devices, but only if the connection type is TCP-based. For
each corresponding device for which aIPAddresses[i] is not
TCP-based, aIPAddresses[i] will be
labjack.ljm.constants.NO_IP_ADDRESS.
Raises:
LJMError: An error was returned from the LJM library call.
Note:
This function only shows what devices can be opened. To
actually open a device, use labjack.ljm.open/openS.
"""
cDev = ctypes.c_int32(deviceType)
cConn = ctypes.c_int32(connectionType)
cNumFound = ctypes.c_int32(0)
cDevTypes = (ctypes.c_int32*constants.LIST_ALL_SIZE)()
cConnTypes = (ctypes.c_int32*constants.LIST_ALL_SIZE)()
cSerNums = (ctypes.c_int32*constants.LIST_ALL_SIZE)()
cIPAddrs = (ctypes.c_int32*constants.LIST_ALL_SIZE)()
error = _staticLib.LJM_ListAll(cDev, cConn, ctypes.byref(cNumFound), ctypes.byref(cDevTypes), ctypes.byref(cConnTypes), ctypes.byref(cSerNums), ctypes.byref(cIPAddrs))
if error != errorcodes.NOERROR:
raise LJMError(error)
numFound = cNumFound.value
return numFound, _convertCtypeArrayToList(cDevTypes[0:numFound]), _convertCtypeArrayToList(cConnTypes[0:numFound]), _convertCtypeArrayToList(cSerNums[0:numFound]), _convertCtypeArrayToList(cIPAddrs[0:numFound])
def listAllS(deviceType, connectionType):
"""Scans for LabJack devices with string parameters and returns
lists describing the devices.
Args:
deviceType: A string that filters which devices will be returned
("LJM_dtT7", etc.). "LJM_dtANY" is allowed.
connectionType: A string that filters by connection type
("LJM_ctUSB", "LJM_ctTCP", etc). "LJM_ctANY" is allowed.
Returns:
A tuple containing:
(numFound, aDeviceTypes, aConnectionTypes, aSerialNumbers,
aIPAddresses)
numFound: Number of devices found.
aDeviceTypes: List of device types for each of the numFound
devices.
aConnectionTypes: List of connection types for each of the
numFound devices.
aSerialNumbers: List of serial numbers for each of the numFound
devices.
aIPAddresses: List of IP addresses for each of the numFound
devices, but only if the connection type is TCP-based. For
each corresponding device for which aIPAddresses[i] is not
TCP-based, aIPAddresses[i] will be
labjack.ljm.constants.NO_IP_ADDRESS.
Raises:
TypeError: deviceType or connectionType are not strings.
LJMError: An error was returned from the LJM library call.
Note:
This function only shows what devices can be opened. To
actually open a device, use labjack.ljm.open/openS.
"""
if not isinstance(deviceType, str):
raise TypeError("Expected a string instead of " + str(type(deviceType)) + ".")
if not isinstance(connectionType, str):
raise TypeError("Expected a string instead of " + str(type(connectionType)) + ".")
cNumFound = ctypes.c_int32(0)
cDevTypes = (ctypes.c_int32*constants.LIST_ALL_SIZE)()
cConnTypes = (ctypes.c_int32*constants.LIST_ALL_SIZE)()
cSerNums = (ctypes.c_int32*constants.LIST_ALL_SIZE)()
cIPAddrs = (ctypes.c_int32*constants.LIST_ALL_SIZE)()
error = _staticLib.LJM_ListAllS(deviceType.encode("ascii"), connectionType.encode("ascii"), ctypes.byref(cNumFound), ctypes.byref(cDevTypes), ctypes.byref(cConnTypes), ctypes.byref(cSerNums), ctypes.byref(cIPAddrs))
if error != errorcodes.NOERROR:
raise LJMError(error)
numFound = cNumFound.value
return numFound, _convertCtypeArrayToList(cDevTypes[0:numFound]), _convertCtypeArrayToList(cConnTypes[0:numFound]), _convertCtypeArrayToList(cSerNums[0:numFound]), _convertCtypeArrayToList(cIPAddrs[0:numFound])
def listAllExtended(deviceType, connectionType, numAddresses, aAddresses, aNumRegs, maxNumFound):
"""Advanced version of listAll that performs an additional query of
arbitrary registers on the device.
Args:
deviceType: An integer containing the type of the device to be
connected (labjack.ljm.constants.dtT7,
labjack.ljm.constants.dtDIGIT, etc.).
labjack.ljm.constants.dtANY is allowed.
connectionType: An integer that filters by connection type
(labjack.ljm.constants.ctUSB,
labjack.ljm.constants.ctTCP, etc.).
labjack.ljm.constants.ctANY is allowed.
numAddresses: The number of addresses to query. Also the size of
aAddresses and aNumRegs.
aAddresses: List of addresses to query for each device that is
found.
aNumRegs: List of the number of registers to query for each
address. Each aNumRegs[i] corresponds to aAddresses[i].
maxNumFound: The maximum number of devices to find.
Returns:
A tuple containing:
(numFound, aDeviceTypes, aConnectionTypes, aSerialNumbers,
aIPAddresses, aBytes)
numFound: Number of devices found.
aDeviceTypes: List of device types for each of the numFound
devices.
aConnectionTypes: List of connection types for each of the
numFound devices.
aSerialNumbers: List of serial numbers for each of the numFound
devices.
aIPAddresses: List of IP addresses for each of the numFound
devices, but only if the connection type is TCP-based. For
each corresponding device for which aIPAddresses[i] is not
TCP-based, aIPAddresses[i] will be
labjack.ljm.constants.NO_IP_ADDRESS.
aBytes: List of the queried bytes sequentially. A device
represented by index i will have an aBytes index of:
(i * <the sum of aNumRegs> *
labjack.ljm.constants.BYTES_PER_REGISTER).
Raises:
LJMError: An error was returned from the LJM library call.
Note:
This function only shows what devices can be opened. To
actually open a device, use labjack.ljm.open/openS.
"""
cDev = ctypes.c_int32(deviceType)
cConn = ctypes.c_int32(connectionType)
cNumAddrs = ctypes.c_int32(numAddresses)
cAddrs = _convertListToCtypeArray(aAddresses, ctypes.c_int32)
cNumRegs = _convertListToCtypeArray(aNumRegs, ctypes.c_int32)
cMaxNumFound = ctypes.c_int32(maxNumFound)
cNumFound = ctypes.c_int32(0)
cDevTypes = (ctypes.c_int32*maxNumFound)()
cConnTypes = (ctypes.c_int32*maxNumFound)()
cSerNums = (ctypes.c_int32*maxNumFound)()
cIPAddrs = (ctypes.c_int32*maxNumFound)()
sumNumRegs = sum(aNumRegs[0:numAddresses])
cBytes = (ctypes.c_ubyte*(maxNumFound*sumNumRegs*constants.BYTES_PER_REGISTER))()
error = _staticLib.LJM_ListAllExtended(cDev, cConn, cNumAddrs, ctypes.byref(cAddrs), ctypes.byref(cNumRegs), cMaxNumFound, ctypes.byref(cNumFound), ctypes.byref(cDevTypes), ctypes.byref(cConnTypes), ctypes.byref(cSerNums), ctypes.byref(cIPAddrs), ctypes.byref(cBytes))
if error != errorcodes.NOERROR:
raise LJMError(error)
numFound = cNumFound.value
return numFound, _convertCtypeArrayToList(cDevTypes[0:numFound]), _convertCtypeArrayToList(cConnTypes[0:numFound]), _convertCtypeArrayToList(cSerNums[0:numFound]), _convertCtypeArrayToList(cIPAddrs[0:numFound]), _convertCtypeArrayToList(cBytes[0:(numFound*sumNumRegs*constants.BYTES_PER_REGISTER)])
def openS(deviceType="ANY", connectionType="ANY", identifier="ANY"):
"""Opens a LabJack device, and returns the device handle.
Args:
deviceType: A string containing the type of the device to be
connected, optionally prepended by "LJM_dt". Possible values
include "ANY", "T4", "T7", and "DIGIT".
connectionType: A string containing the type of the connection
desired, optionally prepended by "LJM_ct". Possible values
include "ANY", "USB", "TCP", "ETHERNET", and "WIFI".
identifier: A string identifying the device to be connected or
"LJM_idANY"/"ANY". This can be a serial number, IP address,
or device name. Device names may not contain periods.
Returns:
The new handle that represents a device connection upon success.
Raises:
TypeError: deviceType or connectionType are not strings.
LJMError: An error was returned from the LJM library call.
Note:
Args are not case-sensitive, and empty strings indicate the
same thing as "LJM_xxANY".
"""
if not isinstance(deviceType, str):
raise TypeError("Expected a string instead of " + str(type(deviceType)) + ".")
if not isinstance(connectionType, str):
raise TypeError("Expected a string instead of " + str(type(connectionType)) + ".")
identifier = str(identifier)
cHandle = ctypes.c_int32(0)
error = _staticLib.LJM_OpenS(deviceType.encode("ascii"), connectionType.encode("ascii"), identifier.encode("ascii"), ctypes.byref(cHandle))
if error != errorcodes.NOERROR:
raise LJMError(error)
return cHandle.value
def open(deviceType=constants.ctANY, connectionType=constants.ctANY, identifier="ANY"):
"""Opens a LabJack device, and returns the device handle.
Args:
deviceType: An integer containing the type of the device to be
connected (labjack.ljm.constants.dtT4,
labjack.ljm.constants.dtT7, labjack.ljm.constants.dtANY,
etc.).
connectionType: An integer containing the type of connection
desired (labjack.ljm.constants.ctUSB,
labjack.ljm.constants.ctTCP, labjack.ljm.constants.ctANY,
etc.).
identifier: A string identifying the device to be connected or
"LJM_idANY"/"ANY". This can be a serial number, IP address,
or device name. Device names may not contain periods.
Returns:
The new handle that represents a device connection upon success.
Raises:
TypeError: deviceType or connectionType are not integers.
LJMError: An error was returned from the LJM library call.
Notes:
Args are not case-sensitive.
Empty strings indicate the same thing as "LJM_xxANY".
"""
cDev = ctypes.c_int32(deviceType)
cConn = ctypes.c_int32(connectionType)
identifier = str(identifier)
cHandle = ctypes.c_int32(0)
error = _staticLib.LJM_Open(cDev, cConn, identifier.encode("ascii"), ctypes.byref(cHandle))
if error != errorcodes.NOERROR:
raise LJMError(error)
return cHandle.value
def getHandleInfo(handle):
"""Returns the device handle's details.
Args:
handle: A valid handle to an open device.
Returns:
A tuple containing:
(deviceType, connectionType, serialNumber, ipAddress, port,
maxBytesPerMB)
deviceType: The device type corresponding to an integer
constant such as labjack.ljm.constants.dtT7.
connectionType: The output device type corresponding to an
integer constant such as labjack.ljm.constants.ctUSB.
serialNumber: The serial number of the device.
ipAddress: The integer representation of the device's IP
address when connectionType is TCP-based. If connectionType
is not TCP-based, this will be
labjack.ljm.constants.NO_IP_ADDRESS. The integer can be
converted to a human-readable string with the
labjack.ljm.numberToIP function.
port: The port if the device connection is TCP-based, or the
pipe if the device connection is USB based.
maxBytesPerMB: The maximum packet size in number of bytes that
can be sent or received from this device. This can change
depending on connection and device type.
Raises:
LJMError: An error was returned from the LJM library call.
Note:
This function returns device information loaded during an open
call and therefore does not initiate communications with the
device. In other words, it is fast but will not represent
changes to serial number or IP address since the device was
opened.
"""
cDev = ctypes.c_int32(0)
cConn = ctypes.c_int32(0)
cSer = ctypes.c_int32(0)
cIPAddr = ctypes.c_int32(0)
cPort = ctypes.c_int32(0)
cPktMax = ctypes.c_int32(0)
error = _staticLib.LJM_GetHandleInfo(handle, ctypes.byref(cDev), ctypes.byref(cConn), ctypes.byref(cSer), ctypes.byref(cIPAddr), ctypes.byref(cPort), ctypes.byref(cPktMax))
if error != errorcodes.NOERROR:
raise LJMError(error)
return cDev.value, cConn.value, cSer.value, cIPAddr.value, cPort.value, cPktMax.value
def close(handle):
"""Closes the connection to the device.
Args:
handle: A valid handle to an open device.
Raises:
LJMError: An error was returned from the LJM library call.
"""
error = _staticLib.LJM_Close(handle)
if error != errorcodes.NOERROR:
raise LJMError(error)
def closeAll():
"""Closes all connections to all devices.
Raises:
LJMError: An error was returned from the LJM library call.
"""
error = _staticLib.LJM_CloseAll()
if error != errorcodes.NOERROR:
raise LJMError(error)
def cleanInfo(infoHandle):
"""Cleans/deallocates an infoHandle.
Args:
infoHandle: The info handle to clean/deallocate.
Raises:
LJMError: An error was returned from the LJM library call.
Note:
Calling cleanInfo on the same handle twice will cause error
INVALID_INFO_HANDLE.
"""
cInfo = ctypes.c_int32(infoHandle)
error = _staticLib.LJM_CleanInfo(cInfo)
if error != errorcodes.NOERROR:
raise LJMError(error)
def eWriteAddress(handle, address, dataType, value):
"""Performs Modbus operations that writes a value to a device.
Args:
handle: A valid handle to an open device.
address: An address to write.
dataTypes: The data type corresponding to the address
(labjack.ljm.constants.FLOAT32, labjack.ljm.constants.INT32,
etc.).
value: The value to write.
Raises:
LJMError: An error was returned from the LJM library call.
"""
cAddr = ctypes.c_int32(address)
cType = ctypes.c_int32(dataType)
cVal = ctypes.c_double(value)
error = _staticLib.LJM_eWriteAddress(handle, cAddr, cType, cVal)
if error != errorcodes.NOERROR:
raise LJMError(error)
def eReadAddress(handle, address, dataType):
"""Performs Modbus operations that reads a value from a device.
Args:
handle: A valid handle to an open device.
address: An address to read.
dataTypes: The data type corresponding to the address
(labjack.ljm.constants.FLOAT32, labjack.ljm.constants.INT32,
etc.).
Returns:
The read value.
Raises:
LJMError: An error was returned from the LJM library call.
"""
cAddr = ctypes.c_int32(address)
cType = ctypes.c_int32(dataType)
cVal = ctypes.c_double(0)
error = _staticLib.LJM_eReadAddress(handle, cAddr, cType, ctypes.byref(cVal))
if error != errorcodes.NOERROR:
raise LJMError(error)
return cVal.value
def eWriteName(handle, name, value):
"""Performs Modbus operations that writes a value to a device.
Args:
handle: A valid handle to an open device.
name: A name (string) to write.
value: The value to write.
Raises:
TypeError: name is not a string.
LJMError: An error was returned from the LJM library call.
"""
if not isinstance(name, str):
raise TypeError("Expected a string instead of " + str(type(name)) + ".")
cVal = ctypes.c_double(value)
error = _staticLib.LJM_eWriteName(handle, name.encode("ascii"), cVal)
if error != errorcodes.NOERROR:
raise LJMError(error)
def eReadName(handle, name):
"""Performs Modbus operations that reads a value from a device.
Args:
handle: A valid handle to an open device.
name: A name (string) to read.
Returns:
The read value.
Raises:
TypeError: name is not a string.
LJMError: An error was returned from the LJM library call.
"""
if not isinstance(name, str):
raise TypeError("Expected a string instead of " + str(type(name)) + ".")
cVal = ctypes.c_double(0)
error = _staticLib.LJM_eReadName(handle, name.encode("ascii"), ctypes.byref(cVal))
if error != errorcodes.NOERROR:
raise LJMError(error)
return cVal.value
def eReadAddresses(handle, numFrames, aAddresses, aDataTypes):
"""Performs Modbus operations that reads values from a device.
Args:
handle: A valid handle to an open device.
numFrames: The total number of reads to perform.
aAddresses: List of addresses to read. This list needs to be at
least size numFrames.
aDataTypes: List of data types corresponding to aAddresses
(labjack.ljm.constants.FLOAT32, labjack.ljm.constants.INT32,
etc.). This list needs to be at least size numFrames.
Returns:
A list of read values.
Raises:
LJMError: An error was returned from the LJM library call.
"""
cNumFrames = ctypes.c_int32(numFrames)
cAddrs = _convertListToCtypeArray(aAddresses, ctypes.c_int32)
cTypes = _convertListToCtypeArray(aDataTypes, ctypes.c_int32)
cVals = (ctypes.c_double*numFrames)()
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eReadAddresses(handle, cNumFrames, ctypes.byref(cAddrs), ctypes.byref(cTypes), ctypes.byref(cVals), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
return _convertCtypeArrayToList(cVals)
def eReadNames(handle, numFrames, aNames):
"""Performs Modbus operations that reads values from a device.
Args:
handle: A valid handle to an open device.
numFrames: The total number of reads to perform.
aNames: List of names (strings) to read. This list needs to be
at least size numFrames.
Returns:
A list of read values.
Raises:
TypeError: aNames is not a list of strings.
LJMError: An error was returned from the LJM library call.
"""
cNumFrames = ctypes.c_int32(numFrames)
asciiNames = []
for x in aNames:
if not isinstance(x, str):
raise TypeError("Expected a string list but found an item " + str(type(x)) + ".")
asciiNames.append(x.encode("ascii"))
cNames = _convertListToCtypeArray(asciiNames, ctypes.c_char_p)
cVals = (ctypes.c_double*numFrames)()
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eReadNames(handle, cNumFrames, cNames, ctypes.byref(cVals), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
return _convertCtypeArrayToList(cVals)
def eWriteAddresses(handle, numFrames, aAddresses, aDataTypes, aValues):
"""Performs Modbus operations that writes values to a device.
Args:
handle: A valid handle to an open device.
numFrames: The total number of writes to perform.
aAddresses: List of addresses to write. This list needs to be at
least size numFrames.
aDataTypes: List of data types corresponding to aAddresses
(labjack.ljm.constants.FLOAT32, labjack.ljm.constants.INT32,
etc.). This list needs to be at least size numFrames.
aValues: The list of values to write. This list needs to be at
least size numFrames.
Raises:
LJMError: An error was returned from the LJM library call.
"""
cNumFrames = ctypes.c_int32(numFrames)
cAddrs = _convertListToCtypeArray(aAddresses, ctypes.c_int32)
cTypes = _convertListToCtypeArray(aDataTypes, ctypes.c_int32)
cVals = _convertListToCtypeArray(aValues, ctypes.c_double)
numFrames = len(cAddrs)
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eWriteAddresses(handle, cNumFrames, ctypes.byref(cAddrs), ctypes.byref(cTypes), ctypes.byref(cVals), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
def eWriteNames(handle, numFrames, aNames, aValues):
"""Performs Modbus operations that writes values to a device.
Args:
handle: A valid handle to an open device.
numFrames: The total number of writes to perform.
aNames: List of names (strings) to write. This list needs to be
at least size numFrames.
aValues: List of values to write. This list needs to be at least
size numFrames.
Raises:
TypeError: aNames is not a list of strings.
LJMError: An error was returned from the LJM library call.
"""
cNumFrames = ctypes.c_int32(numFrames)
asciiNames = []
for x in aNames:
if not isinstance(x, str):
raise TypeError("Expected a string list but found an item " + str(type(x)) + ".")
asciiNames.append(x.encode("ascii"))
cNames = _convertListToCtypeArray(asciiNames, ctypes.c_char_p)
cVals = _convertListToCtypeArray(aValues, ctypes.c_double)
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eWriteNames(handle, cNumFrames, ctypes.byref(cNames), ctypes.byref(cVals), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
def eReadAddressArray(handle, address, dataType, numValues):
"""Performs Modbus operations that reads values from a device.
Args:
handle: A valid handle to an open device.
address: The address to read an array from.
dataType: The data type of address.
numValues: The size of the array to read.
Returns:
A list of size numValues with the read values.
Raises:
LJMError: An error was returned from the LJM library call.
Note:
If numValues is large enough, this functions will automatically
split reads into multiple packets based on the current device's
effective data packet size. Using both non-buffer and buffer
registers in one function call is not supported.
"""
cAddr = ctypes.c_int32(address)
cType = ctypes.c_int32(dataType)
cNumVals = ctypes.c_int32(numValues)
cVals = (ctypes.c_double*numValues)()
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eReadAddressArray(handle, cAddr, cType, cNumVals, ctypes.byref(cVals), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
return _convertCtypeArrayToList(cVals)
def eReadNameArray(handle, name, numValues):
"""Performs Modbus operations that reads values from a device.
Args:
handle: A valid handle to an open device.
name: The register name to read an array from.
numValues: The size of the array to read.
Returns:
A list of size numValues with the read values.
Raises:
TypeError: name is not a string.
LJMError: An error was returned from the LJM library call.
Note:
If numValues is large enough, this functions will automatically
split reads into multiple packets based on the current device's
effective data packet size. Using both non-buffer and buffer
registers in one function call is not supported.
"""
if not isinstance(name, str):
raise TypeError("Expected a string instead of " + str(type(name)) + ".")
cNumVals = ctypes.c_int32(numValues)
cVals = (ctypes.c_double*numValues)()
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eReadNameArray(handle, name.encode("ascii"), cNumVals, ctypes.byref(cVals), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
return _convertCtypeArrayToList(cVals)
def eWriteAddressArray(handle, address, dataType, numValues, aValues):
"""Performs Modbus operations that writes values to a device.
Args:
handle: A valid handle to an open device.
address: The address to write an array to.
dataType: The data type of address.
numValues: The size of the array to write.
aValues: List of values to write. This list needs to be at least
size numValues.
Raises:
LJMError: An error was returned from the LJM library call.
Note:
If numValues is large enough, this functions will automatically
split writes into multiple packets based on the current
device's effective data packet size. Using both non-buffer and
buffer registers in one function call is not supported.
"""
cAddr = ctypes.c_int32(address)
cType = ctypes.c_int32(dataType)
cNumVals = ctypes.c_int32(numValues)
cVals = _convertListToCtypeArray(aValues, ctypes.c_double)
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eWriteAddressArray(handle, cAddr, cType, cNumVals, ctypes.byref(cVals), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
def eWriteNameArray(handle, name, numValues, aValues):
"""Performs Modbus operations that writes values to a device.
Args:
handle: A valid handle to an open device.
name: The register name to write an array to.
numValues: The size of the array to write.
aValues: List of values to write. This list needs to be at least
size numValues.
Raises:
TypeError: name is not a string.
LJMError: An error was returned from the LJM library call.
Note:
If numValues is large enough, this functions will automatically
split writes into multiple packets based on the current
device's effective data packet size. Using both non-buffer and
buffer registers in one function call is not supported.
"""
if not isinstance(name, str):
raise TypeError("Expected a string instead of " + str(type(name)) + ".")
cNumVals = ctypes.c_int32(numValues)
cVals = _convertListToCtypeArray(aValues, ctypes.c_double)
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eWriteNameArray(handle, name.encode("ascii"), cNumVals, ctypes.byref(cVals), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
def eReadAddressByteArray(handle, address, numBytes):
"""Performs a Modbus operation to read a byte array.
Args:
handle: A valid handle to an open device.
address: The address to read an array from.
numBytes: The size of the byte array to read.
Returns:
A list of size numBytes with the read byte values.
Raises:
LJMError: An error was returned from the LJM library call.
Notes:
This function will append a 0x00 byte to aBytes for
odd-numbered numBytes.
If numBytes is large enough, this functions will automatically
split reads into multiple packets based on the current device's
effective data packet size. Using both non-buffer and buffer
registers in one function call is not supported.
"""
cAddr = ctypes.c_int32(address)
cNumBytes = ctypes.c_int32(numBytes)
cBytes = (ctypes.c_ubyte*numBytes)()
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eReadAddressByteArray(handle, cAddr, cNumBytes, ctypes.byref(cBytes), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
return _convertCtypeArrayToList(cBytes)
def eReadNameByteArray(handle, name, numBytes):
"""Performs a Modbus operation to read a byte array.
Args:
handle: A valid handle to an open device.
name: The register name to read an array from.
numBytes: The size of the byte array to read.
Returns:
A list of size numBytes with the read byte values.
Raises:
TypeError: name is not a string.
LJMError: An error was returned from the LJM library call.
Notes:
This function will append a 0x00 byte to aBytes for
odd-numbered numBytes.
If numBytes is large enough, this functions will automatically
split reads into multiple packets based on the current device's
effective data packet size. Using both non-buffer and buffer
registers in one function call is not supported.
"""
if not isinstance(name, str):
raise TypeError("Expected a string instead of " + str(type(name)) + ".")
cNumBytes = ctypes.c_int32(numBytes)
cBytes = (ctypes.c_ubyte*numBytes)()
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eReadNameByteArray(handle, name.encode("ascii"), cNumBytes, ctypes.byref(cBytes), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
return _convertCtypeArrayToList(cBytes)
def eWriteAddressByteArray(handle, address, numBytes, aBytes):
"""Performs a Modbus operation to write a byte array.
Args:
handle: A valid handle to an open device.
address: The register address to write a byte array to.
numBytes: The size of the byte array to write.
aBytes: List of byte values to write. This list needs to be at
least size numBytes.
Raises:
LJMError: An error was returned from the LJM library call.
Notes:
This function will append a 0x00 byte to aBytes for
odd-numbered numBytes.
If numBytes is large enough, this functions will automatically
split writes into multiple packets based on the current
device's effective data packet size. Using both non-buffer and
buffer registers in one function call is not supported.
"""
cAddr = ctypes.c_int32(address)
cNumBytes = ctypes.c_int32(numBytes)
aBytes = _coerceToByteArrayIfString(aBytes)
cBytes = _convertListToCtypeArray(aBytes, ctypes.c_ubyte)
cErrorAddr = ctypes.c_int32(-1)
error = _staticLib.LJM_eWriteAddressByteArray(handle, cAddr, cNumBytes, ctypes.byref(cBytes), ctypes.byref(cErrorAddr))
if error != errorcodes.NOERROR:
errAddr = cErrorAddr.value
if errAddr == -1:
errAddr = None
raise LJMError(error, errAddr)
def eWriteNameByteArray(handle, name, numBytes, aBytes):
"""Performs a Modbus operation to write a byte array.
Args:
handle: A valid handle to an open device.
name: The register name to write an array to.
numBytes: The size of the byte array to write.
aBytes: List of byte values to write. This list needs to be at
least size numBytes.
Raises:
TypeError: name is not a string.
LJMError: An error was returned from the LJM library call.
Notes:
This function will append a 0x00 byte to aBytes for
odd-numbered numBytes.
If numBytes is large enough, this functions will automatically
split writes into multiple packets based on the current
device's effective data packet size. Using both non-buffer and
buffer registers in one function call is not supported.
"""
if not isinstance(name, str):
raise TypeError("Expected a string instead of " + str(type(name)) + ".")
cNumBytes = ctypes.c_int32(numBytes)
aBytes = _coerceToByteArrayIfString(aBytes)