25from ctypes.util
import find_library
28if platform.system() ==
'Windows':
29 _gclib = WinDLL(find_library(
'gclib.dll'))
30 _gclibo = WinDLL(find_library(
'gclibo.dll'))
32elif 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")
38elif platform.system() ==
'Darwin':
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)
51_GCon_ptr = POINTER(_GCon)
53_GSize_ptr = POINTER(_GSize)
55_GCStringOut = c_char_p
58_GStatus_ptr = POINTER(_GStatus)
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]
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]
98_error_buf = create_string_buffer(128)
101 """Checks return codes from gclib and raises a python error if result is exceptional."""
103 _gclibo.GError(return_code, _error_buf, 128)
104 raise GclibError(str(_error_buf.value.decode(_enc)))
109 Error class for non-zero gclib return codes.
115 Represents a single Python connection to a Galil Controller or PLC.
119 """Constructor for the Connection class. Initializes gclib's handle and read buffer."""
121 self.
_buf = create_string_buffer(_buf_size)
126 """Destructor for the Connection class. Ensures close gets called to release Galil resource (Sockets, Kernel Driver, Com Port, etc)."""
131 """Checks if connection is established, throws error if not."""
132 if self.
_gcon.value ==
None:
136 """@ingroup py_connection
137 Opens a connection a galil controller.
138 See the gclib docs for address string formatting.
140 c_address = _GCStringIn(address.encode(_enc))
141 _rc(_gclib.GOpen(c_address, byref(self.
_gcon)))
146 """@ingroup py_connection
147 Closes a connection to a Galil Controller.
149 if self.
_gcon.value !=
None:
151 self.
_gcon = _GCon(0)
156 """@ingroup py_controller
157 Performs a command-and-response transaction on the connection.
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()
169 Provides a blocking sleep call which can be useful for timing-based chores.
177 Provides the gclib version number. Please include the output of this function on all support cases.
179 _rc(_gclibo.GVersion(self.
_buf, _buf_size))
180 return "py." + str(self.
_buf.value.decode(_enc))
183 """@ingroup py_remote
184 Provides the local server name and whether it is published to the local network.
186 _rc(_gclibo.GServerStatus(self.
_buf, _buf_size))
187 return str(self.
_buf.value.decode(_enc))
190 """@ingroup py_remote
191 Set the new active server.
193 c_server_name = _GCStringIn(server_name.encode(_enc))
194 _rc(_gclibo.GSetServer(c_server_name))
198 """@ingroup py_remote
199 Provide a list of all available gcaps servers on the local network.
201 _rc(_gclibo.GListServers(self.
_buf, _buf_size))
202 return str(self.
_buf.value.decode(_enc))
205 """@ingroup py_remote
206 Publish local gcaps server to the network.
208 c_server_name = _GCStringIn(server_name.encode(_enc))
209 _rc(_gclibo.GPublishServer(c_server_name, publish, save))
213 """@ingroup py_remote
214 Shows all remote addresses that are connected to the local server.
216 _rc(_gclibo.GRemoteConnections(self.
_buf, _buf_size))
217 return str(self.
_buf.value.decode(_enc))
220 """@ingroup py_connection
221 Provides a useful connection string. Please include the output of this function on all support cases.
224 return str(self.
_buf.value.decode(_enc))
228 """@ingroup py_connection
229 Provides a dictionary of all Galil controllers requesting IP addresses via BOOT-P or DHCP.
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'}
234 Linux/OS X users must be root to use GIpRequests() and have UDP access to bind and listen on port 67.
236 _rc(_gclibo.GIpRequests(self.
_buf, _buf_size))
238 for line
in str(self.
_buf.value.decode(_enc)).splitlines():
239 line = line.replace(
' ',
'')
240 if (line ==
""):
continue
241 fields = line.split(
',')
243 ip_req_dict[fields[0] +
'-' + fields[1]] = fields[2]
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.
252 c_ip = _GCStringIn(ip.encode(_enc))
253 c_mac = _GCStringIn(mac.encode(_enc))
254 _rc(_gclibo.GAssign(c_ip, c_mac))
259 """@ingroup py_connection
260 Provides a dictionary of all available connection addresses.
262 Returns a dictionary mapping 'address' -> 'revision reports', where possible
265 _rc(_gclibo.GAddresses(self.
_buf, _buf_size))
267 for line
in str(self.
_buf.value.decode(_enc)).splitlines():
268 fields = line.split(
',')
270 addr_dict[fields[0]] = fields[1]
272 addr_dict[fields[0]] =
''
278 """@ingroup py_memory
279 Downloads a program to the controller's program buffer.
280 See the gclib docs for preprocessor options.
283 c_prog = _GCStringIn(program.encode(_enc))
284 c_pre = _GCStringIn(preprocessor.encode(_enc))
285 _rc(_gclib.GProgramDownload(self.
_gcon, c_prog, c_pre))
290 """@ingroup py_memory
291 Uploads a program from the controller's program buffer.
294 _rc(_gclib.GProgramUpload(self.
_gcon, self.
_buf, _buf_size))
295 return str(self.
_buf.value.decode(_enc))
299 """@ingroup py_memory
300 Program download from file.
301 See the gclib docs for preprocessor options.
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))
310 """@ingroup py_memory
311 Program upload to file.
314 c_path = _GCStringIn(file_path.encode(_enc))
315 _rc(_gclibo.GProgramUploadFile(self.
_gcon, c_path))
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)
324 c_name = _GCStringIn(name.encode(_enc))
326 for val
in array_data:
327 array_string += str(val) +
","
328 c_data = _GCStringIn(array_string[:-1].encode(_enc))
329 _rc(_gclib.GArrayDownload(self.
_gcon, c_name, first, last, c_data))
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.
339 c_path = _GCStringIn(file_path.encode(_enc))
341 c_names = _GCStringIn(
''.encode(_enc))
343 names_string += name +
' '
345 c_names = _GCStringIn(names_string[:-1].encode(_enc))
346 _rc(_gclibo.GArrayUploadFile(self.
_gcon, c_path, c_names))
351 """@ingroup py_memory
352 Downloads a csv file containing array data at file_path.
355 c_path = _GCStringIn(file_path.encode(_enc))
356 _rc(_gclibo.GArrayDownloadFile(self.
_gcon, c_path))
361 """@ingroup py_memory
362 Uploads array data from the controller's array table.
365 c_name = _GCStringIn(name.encode(_enc))
366 _rc(_gclib.GArrayUpload(self.
_gcon, c_name, first, last, 1, self.
_buf, _buf_size))
367 string_list = str(self.
_buf.value.decode(_enc)).split(
',')
369 for s
in string_list:
370 float_list.append(float(s))
375 """@ingroup py_connection
376 Set the library timeout. Set to -1 to use the initial library timeout, as specified in GOpen.
379 _rc(_gclibo.GTimeout(self.
_gcon, timeout))
386 """@ingroup py_connection
387 Convenience property read access to timeout value. If -1, gclib uses the initial library timeout, as specified in GOpen.
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.
401 """@ingroup py_memory
405 c_path = _GCStringIn(file_path.encode(_enc))
406 _rc(_gclib.GFirmwareDownload(self.
_gcon, c_path))
411 """@ingroup py_unsolicited
412 Provides access to unsolicited messages from the controller.
416 return str(self.
_buf.value.decode(_enc))
420 """@ingroup py_controller
421 Blocking call that returns once all axes specified have completed their motion.
424 c_axes = _GCStringIn(axes.encode(_enc))
425 _rc(_gclibo.GMotionComplete(self.
_gcon, c_axes))
429 """@ingroup py_unsolicited
430 Provides access to PCI and UDP interrupts from the controller.
434 _rc(_gclib.GInterrupt(self.
_gcon, byref(status)))
438 """@ingroup py_memory
439 Downloads specified sectors from a Galil compressed backup (gcb) file to a controller.
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
445 c_path = _GCStringIn(file_path.encode(_enc))
447 rc = _gclibo.GSetupDownloadFile(self.
_gcon, c_path, options, self.
_buf, _buf_size)
452 for line
in str(self.
_buf.value.decode(_enc)).split(
"\"\n"):
453 fields = line.split(
',',1)
455 if (fields[0] ==
""):
continue
456 elif len(fields) >= 2:
457 info_dict[fields[0].strip(
"\"\'")] = fields[1].strip(
"\"\'")
459 info_dict[fields[0].strip(
"\"\'")] =
''
462 info_dict[
"options"] = rc
Error class for non-zero gclib return codes.
_cc(self)
Checks if connection is established, throws error if not.
__del__(self)
Destructor for the Connection class.
__init__(self)
Constructor for the Connection class.
GOpen(self, address)
Opens a connection a galil controller.
GTimeout(self, timeout)
Set the library timeout.
GInfo(self)
Provides a useful connection string.
GAddresses(self)
Provides a dictionary of all available connection addresses.
timeout(self)
Convenience property read access to timeout value.
GClose(self)
Closes a connection to a Galil Controller.
GAssign(self, ip, mac)
Assigns IP address over the Ethernet to a controller at a given MAC address.
GIpRequests(self)
Provides a dictionary of all Galil controllers requesting IP addresses via BOOT-P or DHCP.
GCommand(self, command)
Performs a command-and-response transaction on the connection.
GMotionComplete(self, axes)
Blocking call that returns once all axes specified have completed their motion.
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...
GFirmwareDownload(self, file_path)
Upgrade firmware.
GSetupDownloadFile(self, file_path, options)
Downloads specified sectors from a Galil compressed backup (gcb) file to a controller.
GProgramDownloadFile(self, file_path, preprocessor="")
Program download from file.
GArrayDownload(self, name, first, last, array_data)
Downloads array data to a pre-dimensioned array in the controller's array table.
GProgramUpload(self)
Uploads a program from the controller's program buffer.
GArrayUpload(self, name, first, last)
Uploads array data from the controller's array table.
GProgramUploadFile(self, file_path)
Program upload to file.
GProgramDownload(self, program, preprocessor="")
Downloads a program to the controller's program buffer.
GArrayDownloadFile(self, file_path)
Downloads a csv file containing array data at file_path.
GRemoteConnections(self)
Shows all remote addresses that are connected to the local server.
GPublishServer(self, server_name, publish, save)
Publish local gcaps server to the network.
GListServers(self)
Provide a list of all available gcaps servers on the local network.
GSetServer(self, server_name)
Set the new active server.
GServerStatus(self)
Provides the local server name and whether it is published to the local network.
GInterrupt(self)
Provides access to PCI and UDP interrupts from the controller.
GMessage(self)
Provides access to unsolicited messages from the controller.
GVersion(self)
Provides the gclib version number.
GSleep(self, val)
Provides a blocking sleep call which can be useful for timing-based chores.
_rc(return_code)
Checks return codes from gclib and raises a python error if result is exceptional.