gclib  437
Communications API for Galil controllers and PLCs
python.md
1 # Python {#python}
2 
3 ## Install gclib
4 
5 The gclib Python wrapper assumes the default gclib \link installation \endlink location.
6 
7 ## Install Python
8 
9 * See https://www.python.org/ if Python is not already installed on the system. The gclib Python wrapper supports Python versions 2 and 3.
10 * On Windows, choose to add Python to the environment variable during installation. This allows Python to be invoked from the command line.
11 
12 ## Install the gclib Python module
13 
14 ### Windows
15 
16 * Type the following commands into a command prompt.
17 
18 
19  >cd %temp%
20  >mkdir py
21  >cd py
22  >copy "c:\Program Files (x86)\Galil\gclib\source\wrappers\python\*" .
23  c:\Program Files (x86)\Galil\gclib\source\wrappers\python\gclib.py
24  c:\Program Files (x86)\Galil\gclib\source\wrappers\python\setup.py
25  2 file(s) copied.
26  >copy "c:\Program Files (x86)\Galil\gclib\examples\python\*" .
27  c:\Program Files (x86)\Galil\gclib\examples\python\example.py
28  1 file(s) copied.
29  >python setup.py install
30  running install
31  running build
32  running build_py
33  creating build
34  creating build\lib
35  copying gclib.py -> build\lib
36  running install_lib
37  running install_egg_info
38  Removing C:\Users\user\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\gclib-1.0-py3.7.egg-info
39  Writing C:\Users\user\AppData\Local\Programs\Python\Python37-32\Lib\site-packages\gclib-1.0-py3.7.egg-info
40 
41 
42 * The gclib Python wrapper is now installed. Go to the next section, **Using gclib from the Python Interpreter**.
43 
44 
45 ### Linux
46 
47 * Type the following commands into a terminal prompt.
48 
49 
50  $ mkdir ~/py
51  $ cd ~/py
52  $ tar -xvf /usr/share/doc/gclib/src/gclib_python.tar.gz
53  gclib.py
54  setup.py
55  $ tar -xvf /usr/share/doc/gclib/src/gclib_python_examples.tar.gz
56  example.py
57  $ sudo python setup.py install
58  [sudo] password for user:
59  running install
60  running build
61  running build_py
62  creating build
63  creating build/lib
64  copying gclib.py -> build/lib
65  running install_lib
66  copying build/lib/gclib.py -> /usr/lib/python2.7/site-packages
67  byte-compiling /usr/lib/python2.7/site-packages/gclib.py to gclib.pyc
68  running install_egg_info
69  Writing /usr/lib/python2.7/site-packages/gclib-1.0-py2.7.egg-info
70 
71 
72 * The gclib Python wrapper is now installed. Go to the next section, **Using gclib from the Python Interpreter**.
73 
74 
75 ### OS X
76 
77 * Be sure that the *Create Environment Variable* step has been followed in the \link osx OS X\endlink installation instructions.
78 * Type the following commands into a Terminal prompt.
79 
80 
81  $ mkdir ~/python_temp
82  $ cd ~/python_temp/
83  $ tar -xvf /Applications/gclib/source/gclib_python.tar.gz
84  x gclib.py
85  x setup.py
86  $ tar -xvf /Applications/gclib/examples/gclib_python_examples.tar.gz
87  x example.py
88  $ sudo python setup.py install
89  running install
90  running build
91  running build_py
92  creating build
93  creating build/lib
94  copying gclib.py -> build/lib
95  running install_lib
96  copying build/lib/gclib.py -> /Library/Python/2.7/site-packages
97  byte-compiling /Library/Python/2.7/site-packages/gclib.py to gclib.pyc
98  running install_egg_info
99  Writing /Library/Python/2.7/site-packages/gclib-1.0-py2.7.egg-info
100 
101 * The gclib Python wrapper is now installed. Go to the next section, **Using gclib from the Python Interpreter**.
102 
103 
104 ## Using gclib from the Python Interpreter
105 
106 * Invoke the <a href="https://docs.python.org/2/tutorial/interpreter.html" target="_blank">Python Interpreter</a>.
107 * Type the following into the Python prompt.
108 
109 
110 
111  >>> import gclib
112  >>> g = gclib.py()
113  >>> g.GOpen('192.168.0.42 --direct')
114  >>> print(g.GInfo())
115  192.168.0.42, DMC4080 Rev 1.2c, 783
116 
117 ## Running Python scripts
118 
119 * Navigate the terminal to the location from **Install the gclib Python module** where example.py was copied.
120 * Open *example.py* in a text editor.
121 * Set the address in the g.GOpen() call to match an avaiable connection.
122 * Execute the following command at the Terminal.
123 
124 
125  $ python example.py
126  gclib version: py.127.110.250
127  192.168.0.42, DMC4080 Rev 1.2c, 783
128 
129 * Experiment with the example by uncommenting sections, between the triple quotes, '''.
130 
131 
132  $ python example.py
133  gclib version: py.127.110.250
134  192.168.0.42, DMC4080 Rev 1.2c, 783
135  GProgramDownload() correctly errored. Can't fit with level 3 compression
136  Uploaded program:
137  #A;i=0;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1
138  i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;i=i+1;EN
139  Downloaded program verified
140  Array element verified
141  187942.0000
142 
143  Starting move...
144  done.
145 
146 ## Getting help
147 
148 
149  >>> help(g.GOpen)
150  Help on method GOpen in module gclib:
151 
152  GOpen(address) method of gclib.py instance
153  Opens a connection a galil controller.
154  See the gclib docs for address string formatting.
155  See Link GOpen() <http://www.galil.com/sw/pub/all/doc/gclib/html/gclib_8h_aef4aec8a85630eed029b7a46aea7db54.html#aef4aec8a85630eed029b7a46aea7db54>
156 
157  >>> help(g.GCommand)
158  Help on method GCommand in module gclib:
159 
160  GCommand(command) method of gclib.py instance
161  Performs a command-and-response transaction on the connection.
162  Trims the response.
163  See Link GCommand() <http://www.galil.com/sw/pub/all/doc/gclib/html/gclib_8h_a5ac031e76efc965affdd73a1bec084a8.html#a5ac031e76efc965affdd73a1bec084a8>
164 
165  >>> 'for a full listing, try help(g)'
166 
167 
168 
169 
170