gclib  2.0.8
Communications API for Galil controllers and PLCs
example.py
Go to the documentation of this file.
1 '''! \file example.py
2 '''
3 
4 import sys
5 import string
6 import gclib
7 
8 
9 def main():
10  g = gclib.py() #make an instance of the gclib python class
11 
12  try:
13  print('gclib version:', g.GVersion())
14 
15 
18  '''
19  #Get Ethernet controllers requesting IP addresses
20  print('Listening for controllers requesting IP addresses...')
21  ip_requests = g.GIpRequests()
22  for id in ip_requests.keys():
23  print(id, 'at mac', ip_requests[id])
24 
25  #define a mapping of my hardware to ip addresses
26  ips = {}
27  ips['DMC4000-783'] = '192.168.0.42'
28  ips['DMC4103-9998'] = '192.168.0.43'
29 
30  for id in ips.keys():
31  if id in ip_requests: #if our controller needs an IP
32  print("\nAssigning", ips[id], "to", ip_requests[id])
33  g.GAssign(ips[id], ip_requests[id]) #send the mapping
34  g.GOpen(ips[id]) #connect to it
35  print(g.GInfo())
36  g.GCommand('BN') #burn the IP
37  g.GClose() #disconnect
38 
39  print('\nAvailable addresses:') #print ready connections
40  available = g.GAddresses()
41  for a in sorted(available.keys()):
42  print(a, available[a])
43 
44  print('\n')
45  '''
46 
49  g.GOpen('192.168.0.42 -s ALL')
50  #g.GOpen('COM1')
51  print(g.GInfo())
52 
53 
56  '''
57  print('Programs')
58  program = '#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;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';
59  #Program line above is too large to fit on any Galil controller, however it can easily fit if broken up with level 4 compression.
60  #the value of i at the end of code execution is 20.
61  try:
62  g.GProgramDownload(program, '--max 3') #program won't fit at level 3
63  except gclib.GclibError as e:
64  print(' GProgramDownload() correctly errored. Can\'t fit with level 3 compression')
65 
66  g.GProgramDownload(program, '')
67  g.GProgramUploadFile('temp.dmc')
68  g.GProgramDownload('','') #erase program
69  g.GProgramDownloadFile('temp.dmc', '')
70  print(' Uploaded program:\n%s' % g.GProgramUpload())
71  g.GCommand('XQ') #execute the code
72  g.GSleep(10) #wait a brief interval for the code to complete.
73  if (float(g.GCommand('i=?')) == 20): #python can convert '20.0000' to float
74  print(' Downloaded program verified')
75  else:
76  print(' Unexpected program result')
77 
78  #g.timeout = 5000 #set longish timeout
79  #g.GCommand('BP') #burn program
80  '''
81 
84  '''
85  print('Arrays')
86  g.GCommand('DA *,*[]')
87  g.GCommand('DM A[5], B[10]')
88  array_a = [1, 2, 3.14, 4, 5]
89  array_b = [30, 42, 50, 60, 70]
90  g.GArrayDownload('A', 0, -1, array_a)
91  g.GArrayDownload('B', 2, -1, array_b)
92  g.GArrayUploadFile('arrays.csv', ['A','B'])
93  g.GArrayDownloadFile('arrays.csv')
94  array_b_up = g.GArrayUpload('B', 3, 5)
95  if array_b_up[0] == 42:
96  print(' Array element verified')
97  else:
98  print(' Unexpected aray element', array_b_up[0])
99  '''
100 
103  '''
104  print('Messages')
105  g.GProgramDownload('WT100\rMGTIME\rEN')
106  g.GCommand('XQ')
107  print(g.GMessage())
108  '''
109 
112  '''
113  firmware = r'c:\temp\dmc-4000-r12c.hex'
114  #firmware = r'~/temp/dmc-4000-r12c.hex'
115  print('Loading firmware', firmware)
116  g.GFirmwareDownload(firmware)
117  print(g.GInfo())
118  '''
119 
122  '''
123  #Motion Complete
124  print('Motion Complete')
125  c = g.GCommand #alias the command callable
126  c('AB') #abort motion and program
127  c('MO') #turn off all motors
128  c('SHA') #servo A
129  c('SPA=1000') #speead, 1000 cts/sec
130  c('PRA=3000') #relative move, 3000 cts
131  print(' Starting move...')
132  c('BGA') #begin motion
133  g.GMotionComplete('A')
134  print(' done.')
135  del c #delete the alias
136  '''
137 
140  except gclib.GclibError as e:
141  print('Unexpected GclibError:', e)
142 
143  finally:
144  g.GClose() #don't forget to close connections!
145 
146  return
147 
148 
149 #runs main() if example.py called from the console
150 if __name__ == '__main__':
151  main()
int main(int argc, char *argv[])
Main function for Commands Example.