| gclib 2.4.1 Galil Communications Library

Loading...
Searching...
No Matches
gclib.py
Go to the documentation of this file.
1
18
19
23 import platform #for distinguishing 'Windows', 'Linux', 'Darwin'
24 from ctypes import *
25 from ctypes.util import find_library
26 import os
27
28 if platform.system() == 'Windows':
29 _gclib = WinDLL(find_library('gclib.dll'))
30 _gclibo = WinDLL(find_library('gclibo.dll'))
31
32 elif platform.system() == 'Linux':
33 cdll.LoadLibrary("libgclib.so.2")
34 _gclib = CDLL("libgclib.so.2")
35 cdll.LoadLibrary("libgclibo.so.2")
36 _gclibo = CDLL("libgclibo.so.2")
37
38 elif platform.system() == 'Darwin': #OSX
39 _gclib_path = '/Applications/gclib/dylib/gclib.0.dylib'
40 _gclibo_path = '/Applications/gclib/dylib/gclibo.0.dylib'
41 cdll.LoadLibrary(_gclib_path)
42 _gclib = CDLL(_gclib_path)
43 cdll.LoadLibrary(_gclibo_path)
44 _gclibo = CDLL(_gclibo_path)
45
46
47
48# Python "typedefs"
49_GReturn = c_int #type for a return code
50_GCon = c_void_p #type for a Galil connection handle
51_GCon_ptr = POINTER(_GCon) #used for argtypes declaration
52_GSize = c_ulong #type for a Galil size variable
53_GSize_ptr = POINTER(_GSize) #used for argtypes declaration
54_GCStringIn = c_char_p #char*. In C it's const.
55_GCStringOut = c_char_p #char*
56_GOption = c_int #type for option variables, e.g. GArrayDownload
57_GStatus = c_ubyte #type for interrupt status bytes
58_GStatus_ptr = POINTER(_GStatus) #used for argtypes declaration
59
60#Define arguments and result type (if not C int type)
61#gclib calls
62_gclib.GArrayDownload.argtypes = [_GCon, _GCStringIn, _GOption, _GOption, _GCStringIn]
63_gclib.GArrayUpload.argtypes = [_GCon, _GCStringIn, _GOption, _GOption, _GOption, _GCStringOut, _GSize]
64_gclib.GClose.argtypes = [_GCon]
65_gclib.GCommand.argtypes = [_GCon, _GCStringIn, _GCStringOut, _GSize, _GSize_ptr]
66_gclib.GFirmwareDownload.argtypes = [_GCon, _GCStringIn]
67_gclib.GInterrupt.argtypes = [_GCon, _GStatus_ptr]
68_gclib.GMessage.argtypes = [_GCon, _GCStringOut, _GSize]
69_gclib.GOpen.argtypes = [_GCStringIn, _GCon_ptr]
70_gclib.GProgramDownload.argtypes = [_GCon, _GCStringIn, _GCStringIn]
71_gclib.GProgramUpload.argtypes = [_GCon, _GCStringOut, _GSize]
72#gclibo calls (open source component/convenience functions)
73_gclibo.GAddresses.argtypes = [_GCStringOut, _GSize]
74_gclibo.GArrayDownloadFile.argtypes = [_GCon, _GCStringIn]
75_gclibo.GArrayUploadFile.argtypes = [_GCon, _GCStringIn, _GCStringIn]
76_gclibo.GAssign.argtypes = [_GCStringIn, _GCStringIn]
77_gclibo.GError.argtypes = [_GReturn, _GCStringOut, _GSize]
78_gclibo.GError.restype = None
79_gclibo.GError.argtypes = [_GCon, _GCStringOut, _GSize]
80_gclibo.GIpRequests.argtypes = [_GCStringOut, _GSize]
81_gclibo.GMotionComplete.argtypes = [_GCon, _GCStringIn]
82_gclibo.GProgramDownloadFile.argtypes = [_GCon, _GCStringIn, _GCStringIn]
83_gclibo.GSleep.argtypes = [c_uint]
84_gclibo.GSleep.restype = None
85_gclibo.GProgramUploadFile.argtypes = [_GCon, _GCStringIn]
86_gclibo.GTimeout.argtypes = [_GCon, c_int]
87_gclibo.GVersion.argtypes = [_GCStringOut, _GSize]
88_gclibo.GServerStatus.argtypes = [_GCStringOut, _GSize]
89_gclibo.GSetServer.argtypes = [_GCStringIn]
90_gclibo.GListServers.argtypes = [_GCStringOut, _GSize]
91_gclibo.GPublishServer.argtypes = [_GCStringIn, _GOption, _GOption]
92_gclibo.GRemoteConnections.argtypes = [_GCStringOut, _GSize]
93_gclibo.GSetupDownloadFile.argtypes = [_GCon, _GCStringIn, _GOption, _GCStringOut, _GSize]
94
95#Set up some constants
96_enc = "ASCII" #byte encoding for going between python strings and c strings.
97_buf_size = 500000 #size of response buffer. Big enough to fit entire 4000 program via UL/LS, or 24000 elements of array data.
98_error_buf = create_string_buffer(128) #buffer for retrieving error code descriptions.
99
100 def _rc(return_code):
101 """Checks return codes from gclib and raises a python error if result is exceptional."""
102 if return_code != 0:
103 _gclibo.GError(return_code, _error_buf, 128) #Get the library's error description
104 raise GclibError(str(_error_buf.value.decode(_enc)))
105 return
106
107 class GclibError(Exception):
108 """@ingroup python
109 Error class for non-zero gclib return codes.
110 """
111 pass
112
113 class py:
114 """
115 Represents a single Python connection to a Galil Controller or PLC.
116 """
117
118 def __init__(self):
119 """Constructor for the Connection class. Initializes gclib's handle and read buffer."""
120 self._gcon = _GCon(0) #handle to connection
121 self._buf = create_string_buffer(_buf_size)
122 self._timeout = 5000
123 return
124
125 def __del__(self):
126 """Destructor for the Connection class. Ensures close gets called to release Galil resource (Sockets, Kernel Driver, Com Port, etc)."""
127 self.GClose()
128 return
129
130 def _cc(self):
131 """Checks if connection is established, throws error if not."""
132 if self._gcon.value == None:
133 _rc(-1201) #G_CONNECTION_NOT_ESTABLISHED
134
135 def GOpen(self, address):
136 """@ingroup py_connection
137 Opens a connection a galil controller.
138 See the gclib docs for address string formatting.
139 """
140 c_address = _GCStringIn(address.encode(_enc))
141 _rc(_gclib.GOpen(c_address, byref(self._gcon)))
142 return
143
144
145 def GClose(self):
146 """@ingroup py_connection
147 Closes a connection to a Galil Controller.
148 """
149 if self._gcon.value != None:
150 _rc(_gclib.GClose(self._gcon))
151 self._gcon = _GCon(0)
152 return
153
154
155 def GCommand(self, command):
156 """@ingroup py_controller
157 Performs a command-and-response transaction on the connection.
158 Trims the response.
159 """
160 self._cc()
161 c_command = _GCStringIn(command.encode(_enc))
162 _rc(_gclib.GCommand(self._gcon, c_command, self._buf, _buf_size, None))
163 response = str(self._buf.value.decode(_enc))
164 return response[:-3].strip() # trim trailing /r/n: and leading space
165
166
167 def GSleep(self, val):
168 """@ingroup python
169 Provides a blocking sleep call which can be useful for timing-based chores.
170 """
171 _gclibo.GSleep(val)
172 return
173
174
175 def GVersion(self):
176 """@ingroup python
177 Provides the gclib version number. Please include the output of this function on all support cases.
178 """
179 _rc(_gclibo.GVersion(self._buf, _buf_size))
180 return "py." + str(self._buf.value.decode(_enc))
181
182 def GServerStatus(self):
183 """@ingroup py_remote
184 Provides the local server name and whether it is published to the local network.
185 """
186 _rc(_gclibo.GServerStatus(self._buf, _buf_size))
187 return str(self._buf.value.decode(_enc))
188
189 def GSetServer(self, server_name):
190 """@ingroup py_remote
191 Set the new active server.
192 """
193 c_server_name = _GCStringIn(server_name.encode(_enc))
194 _rc(_gclibo.GSetServer(c_server_name))
195 return
196
197 def GListServers(self):
198 """@ingroup py_remote
199 Provide a list of all available gcaps servers on the local network.
200 """
201 _rc(_gclibo.GListServers(self._buf, _buf_size))
202 return str(self._buf.value.decode(_enc))
203
204 def GPublishServer(self, server_name, publish, save):
205 """@ingroup py_remote
206 Publish local gcaps server to the network.
207 """
208 c_server_name = _GCStringIn(server_name.encode(_enc))
209 _rc(_gclibo.GPublishServer(c_server_name, publish, save))
210 return
211
212 def GRemoteConnections(self):
213 """@ingroup py_remote
214 Shows all remote addresses that are connected to the local server.
215 """
216 _rc(_gclibo.GRemoteConnections(self._buf, _buf_size))
217 return str(self._buf.value.decode(_enc))
218
219 def GInfo(self):
220 """@ingroup py_connection
221 Provides a useful connection string. Please include the output of this function on all support cases.
222 """
223 _rc(_gclibo.GInfo(self._gcon, self._buf, _buf_size))
224 return str(self._buf.value.decode(_enc))
225
226
227 def GIpRequests(self):
228 """@ingroup py_connection
229 Provides a dictionary of all Galil controllers requesting IP addresses via BOOT-P or DHCP.
230
231 Returns a dictionary mapping 'model-serial' --> 'mac address'
232 e.g. {'DMC4000-783': '00:50:4c:20:03:0f', 'DMC4103-9998': '00:50:4c:38:27:0e'}
233
234 Linux/OS X users must be root to use GIpRequests() and have UDP access to bind and listen on port 67.
235 """
236 _rc(_gclibo.GIpRequests(self._buf, _buf_size)) #get the c string from gclib
237 ip_req_dict = {}
238 for line in str(self._buf.value.decode(_enc)).splitlines():
239 line = line.replace(' ', '') #trim spaces throughout
240 if (line == ""): continue
241 fields = line.split(',')
242 #fields go [model, serial number, mac]
243 ip_req_dict[fields[0] + '-' + fields[1]] = fields[2] # e.g. DMC4000-783 maps to its MAC addr.
244 return ip_req_dict
245
246
247 def GAssign(self, ip, mac):
248 """@ingroup py_connection
249 Assigns IP address over the Ethernet to a controller at a given MAC address.
250 Linux/OS X users must be root to use GAssign() and have UDP access to send on port 68.
251 """
252 c_ip = _GCStringIn(ip.encode(_enc))
253 c_mac = _GCStringIn(mac.encode(_enc))
254 _rc(_gclibo.GAssign(c_ip, c_mac))
255 return
256
257
258 def GAddresses(self):
259 """@ingroup py_connection
260 Provides a dictionary of all available connection addresses.
261
262 Returns a dictionary mapping 'address' -> 'revision reports', where possible
263 e.g. {}
264 """
265 _rc(_gclibo.GAddresses(self._buf, _buf_size))
266 addr_dict = {}
267 for line in str(self._buf.value.decode(_enc)).splitlines():
268 fields = line.split(',')
269 if len(fields) >= 2:
270 addr_dict[fields[0]] = fields[1]
271 else:
272 addr_dict[fields[0]] = ''
273
274 return addr_dict
275
276
277 def GProgramDownload(self, program, preprocessor=""):
278 """@ingroup py_memory
279 Downloads a program to the controller's program buffer.
280 See the gclib docs for preprocessor options.
281 """
282 self._cc()
283 c_prog = _GCStringIn(program.encode(_enc))
284 c_pre = _GCStringIn(preprocessor.encode(_enc))
285 _rc(_gclib.GProgramDownload(self._gcon, c_prog, c_pre))
286 return
287
288
289 def GProgramUpload(self):
290 """@ingroup py_memory
291 Uploads a program from the controller's program buffer.
292 """
293 self._cc()
294 _rc(_gclib.GProgramUpload(self._gcon, self._buf, _buf_size))
295 return str(self._buf.value.decode(_enc))
296
297
298 def GProgramDownloadFile(self, file_path, preprocessor=""):
299 """@ingroup py_memory
300 Program download from file.
301 See the gclib docs for preprocessor options.
302 """
303 self._cc()
304 c_path = _GCStringIn(file_path.encode(_enc))
305 c_pre = _GCStringIn(preprocessor.encode(_enc))
306 _rc(_gclibo.GProgramDownloadFile(self._gcon, c_path, c_pre))
307 return
308
309 def GProgramUploadFile(self, file_path):
310 """@ingroup py_memory
311 Program upload to file.
312 """
313 self._cc()
314 c_path = _GCStringIn(file_path.encode(_enc))
315 _rc(_gclibo.GProgramUploadFile(self._gcon, c_path))
316 return
317
318 def GArrayDownload(self, name, first, last, array_data):
319 """@ingroup py_memory
320 Downloads array data to a pre-dimensioned array in the controller's array table.
321 array_data should be a list of values (e.g. int or float)
322 """
323 self._cc()
324 c_name = _GCStringIn(name.encode(_enc))
325 array_string = ""
326 for val in array_data:
327 array_string += str(val) + ","
328 c_data = _GCStringIn(array_string[:-1].encode(_enc)) #trim trailing command
329 _rc(_gclib.GArrayDownload(self._gcon, c_name, first, last, c_data))
330 return
331
332
333 def GArrayUploadFile(self, file_path, names = []):
334 """@ingroup py_memory
335 Uploads the entire controller array table or a subset and saves the data as a csv file specified by file_path.
336 names is optional and should be a list of array names on the controller.
337 """
338 self._cc()
339 c_path = _GCStringIn(file_path.encode(_enc))
340 names_string = ''
341 c_names = _GCStringIn(''.encode(_enc)) #in case empty list provided
342 for name in names:
343 names_string += name + ' '
344
345 c_names = _GCStringIn(names_string[:-1].encode(_enc)) #trim trailing space
346 _rc(_gclibo.GArrayUploadFile(self._gcon, c_path, c_names))
347 return
348
349
350 def GArrayDownloadFile(self, file_path):
351 """@ingroup py_memory
352 Downloads a csv file containing array data at file_path.
353 """
354 self._cc()
355 c_path = _GCStringIn(file_path.encode(_enc))
356 _rc(_gclibo.GArrayDownloadFile(self._gcon, c_path))
357 return
358
359
360 def GArrayUpload(self, name, first, last):
361 """@ingroup py_memory
362 Uploads array data from the controller's array table.
363 """
364 self._cc()
365 c_name = _GCStringIn(name.encode(_enc))
366 _rc(_gclib.GArrayUpload(self._gcon, c_name, first, last, 1, self._buf, _buf_size)) #1 is comma delimiter
367 string_list = str(self._buf.value.decode(_enc)).split(',')
368 float_list = []
369 for s in string_list:
370 float_list.append(float(s))
371 return float_list
372
373
374 def GTimeout(self, timeout):
375 """@ingroup py_connection
376 Set the library timeout. Set to -1 to use the initial library timeout, as specified in GOpen.
377 """
378 self._cc()
379 _rc(_gclibo.GTimeout(self._gcon, timeout))
380 self._timeout = timeout
381 return
382
383
384 @property
385 def timeout(self):
386 """@ingroup py_connection
387 Convenience property read access to timeout value. If -1, gclib uses the initial library timeout, as specified in GOpen.
388 """
389 return self._timeout
390
391 @timeout.setter
392 def timeout(self, timeout):
393 """@ingroup py_connection
394 Convenience property write access to timeout value. Set to -1 to use the initial library timeout, as specified in GOpen.
395 """
396 self.GTimeout(timeout)
397 return
398
399
400 def GFirmwareDownload(self, file_path):
401 """@ingroup py_memory
402 Upgrade firmware.
403 """
404 self._cc()
405 c_path = _GCStringIn(file_path.encode(_enc))
406 _rc(_gclib.GFirmwareDownload(self._gcon, c_path))
407 return
408
409
410 def GMessage(self):
411 """@ingroup py_unsolicited
412 Provides access to unsolicited messages from the controller.
413 """
414 self._cc()
415 _rc(_gclib.GMessage(self._gcon, self._buf, _buf_size))
416 return str(self._buf.value.decode(_enc))
417
418
419 def GMotionComplete(self, axes):
420 """@ingroup py_controller
421 Blocking call that returns once all axes specified have completed their motion.
422 """
423 self._cc()
424 c_axes = _GCStringIn(axes.encode(_enc))
425 _rc(_gclibo.GMotionComplete(self._gcon, c_axes))
426 return
427
428 def GInterrupt(self):
429 """@ingroup py_unsolicited
430 Provides access to PCI and UDP interrupts from the controller.
431 """
432 self._cc()
433 status = _GStatus(0)
434 _rc(_gclib.GInterrupt(self._gcon, byref(status)))
435 return status.value
436
437 def GSetupDownloadFile(self, file_path, options):
438 """@ingroup py_memory
439 Downloads specified sectors from a Galil compressed backup (gcb) file to a controller.
440
441 Returns a dictionary with the controller information stored in the gcb file.
442 If options is specified as 0, an additional "options" key will be in the dictionary indicating the info sectors available in the gcb
443 """
444 self._cc()
445 c_path = _GCStringIn(file_path.encode(_enc))
446
447 rc = _gclibo.GSetupDownloadFile(self._gcon, c_path, options, self._buf, _buf_size)
448 if (options != 0):
449 _rc(rc)
450
451 info_dict = {}
452 for line in str(self._buf.value.decode(_enc)).split("\"\n"):
453 fields = line.split(',',1)
454
455 if (fields[0] == ""): continue
456 elif len(fields) >= 2:
457 info_dict[fields[0].strip("\"\'")] = fields[1].strip("\"\'")
458 else:
459 info_dict[fields[0].strip("\"\'")] = ''
460
461 if (options == 0):
462 info_dict["options"] = rc
463
464 return info_dict
gclib.GclibError
Error class for non-zero gclib return codes.
Definition gclib.py:107
gclib.py._gcon
_gcon
Definition gclib.py:120
gclib.py._cc
_cc(self)
Checks if connection is established, throws error if not.
Definition gclib.py:130
gclib.py._timeout
int _timeout
Definition gclib.py:122
gclib.py.__del__
__del__(self)
Destructor for the Connection class.
Definition gclib.py:125
gclib.py.__init__
__init__(self)
Constructor for the Connection class.
Definition gclib.py:118
gclib.py._buf
_buf
Definition gclib.py:121
gclib.py.GOpen
GOpen(self, address)
Opens a connection a galil controller.
Definition gclib.py:135
gclib.py.GTimeout
GTimeout(self, timeout)
Set the library timeout.
Definition gclib.py:374
gclib.py.GInfo
GInfo(self)
Provides a useful connection string.
Definition gclib.py:219
gclib.py.GAddresses
GAddresses(self)
Provides a dictionary of all available connection addresses.
Definition gclib.py:258
gclib.py.timeout
timeout(self)
Convenience property read access to timeout value.
Definition gclib.py:385
gclib.py.GClose
GClose(self)
Closes a connection to a Galil Controller.
Definition gclib.py:145
gclib.py.GAssign
GAssign(self, ip, mac)
Assigns IP address over the Ethernet to a controller at a given MAC address.
Definition gclib.py:247
gclib.py.GIpRequests
GIpRequests(self)
Provides a dictionary of all Galil controllers requesting IP addresses via BOOT-P or DHCP.
Definition gclib.py:227
gclib.py.GCommand
GCommand(self, command)
Performs a command-and-response transaction on the connection.
Definition gclib.py:155
gclib.py.GMotionComplete
GMotionComplete(self, axes)
Blocking call that returns once all axes specified have completed their motion.
Definition gclib.py:419
gclib.py.GArrayUploadFile
GArrayUploadFile(self, file_path, names=[])
Uploads the entire controller array table or a subset and saves the data as a csv file specified by f...
Definition gclib.py:333
gclib.py.GFirmwareDownload
GFirmwareDownload(self, file_path)
Upgrade firmware.
Definition gclib.py:400
gclib.py.GSetupDownloadFile
GSetupDownloadFile(self, file_path, options)
Downloads specified sectors from a Galil compressed backup (gcb) file to a controller.
Definition gclib.py:437
gclib.py.GProgramDownloadFile
GProgramDownloadFile(self, file_path, preprocessor="")
Program download from file.
Definition gclib.py:298
gclib.py.GArrayDownload
GArrayDownload(self, name, first, last, array_data)
Downloads array data to a pre-dimensioned array in the controller's array table.
Definition gclib.py:318
gclib.py.GProgramUpload
GProgramUpload(self)
Uploads a program from the controller's program buffer.
Definition gclib.py:289
gclib.py.GArrayUpload
GArrayUpload(self, name, first, last)
Uploads array data from the controller's array table.
Definition gclib.py:360
gclib.py.GProgramUploadFile
GProgramUploadFile(self, file_path)
Program upload to file.
Definition gclib.py:309
gclib.py.GProgramDownload
GProgramDownload(self, program, preprocessor="")
Downloads a program to the controller's program buffer.
Definition gclib.py:277
gclib.py.GArrayDownloadFile
GArrayDownloadFile(self, file_path)
Downloads a csv file containing array data at file_path.
Definition gclib.py:350
gclib.py.GRemoteConnections
GRemoteConnections(self)
Shows all remote addresses that are connected to the local server.
Definition gclib.py:212
gclib.py.GPublishServer
GPublishServer(self, server_name, publish, save)
Publish local gcaps server to the network.
Definition gclib.py:204
gclib.py.GListServers
GListServers(self)
Provide a list of all available gcaps servers on the local network.
Definition gclib.py:197
gclib.py.GSetServer
GSetServer(self, server_name)
Set the new active server.
Definition gclib.py:189
gclib.py.GServerStatus
GServerStatus(self)
Provides the local server name and whether it is published to the local network.
Definition gclib.py:182
gclib.py.GInterrupt
GInterrupt(self)
Provides access to PCI and UDP interrupts from the controller.
Definition gclib.py:428
gclib.py.GMessage
GMessage(self)
Provides access to unsolicited messages from the controller.
Definition gclib.py:410
gclib.py.GVersion
GVersion(self)
Provides the gclib version number.
Definition gclib.py:175
gclib.py.GSleep
GSleep(self, val)
Provides a blocking sleep call which can be useful for timing-based chores.
Definition gclib.py:167
gclib._rc
_rc(return_code)
Checks return codes from gclib and raises a python error if result is exceptional.
Definition gclib.py:100