-
Notifications
You must be signed in to change notification settings - Fork 3
/
pipeolcblink.py
executable file
·187 lines (150 loc) · 5.28 KB
/
pipeolcblink.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
#!/usr/bin/env python
'''
Drive an OpenLCB via pipes to an executable
argparse is new in Jython 2.7, so dont use here
@author: Bob Jacobsen
'''
import subprocess
# stdout,stderr = p.communicate("send stuff input\n more studd")
# print "O",stdout
# print "E",stderr
class PipeOlcbLink :
def __init__(self) :
# Defaults (generally overridden by system-wide defaults elsewhere)
# To use these:
# cd ../C (e.g. to prototypes/C)
# make
self.location = "../C/libraries/OlcbTestCAN/obj/test/" # where to find file
self.name = "pyOlcbBasicNode" # executable name
self.timeout = 0.010
self.startdelay = 0;
self.verbose = False
self.process = None
return
def connect(self) :
# if verbose, print
if (self.verbose) : print " starting ",self.name," from ",self.location
executable = self.location+self.name
self.process = subprocess.Popen(executable,1,None,subprocess.PIPE,subprocess.PIPE,
sys.stderr, None, False, True)
self.seenEnd = False
# dump startup needed here
if self.timeout < 2 :
self.flush()
self.process.stdin.write('T\n')
self.flush()
self.process.stdin.write('T\n')
self.flush()
self.process.stdin.write('T\n')
self.flush()
self.process.stdin.write('T\n')
self.flush()
self.process.stdin.write('T\n')
self.flush()
return
def send(self, frame) :
if self.process == None :
self.connect()
elif not self.seenEnd :
self.flush()
# if verbose, print
if self.verbose : print " send ",frame
# send
self.seenEnd = False
self.process.stdin.write(frame+'\n')
self.process.stdin.flush()
return
def receive(self) : # returns frame
if (self.process == None) : self.connect()
# if verbose, print
if (self.verbose) : print " receive ",
r = self.process.stdout.readline()
# timeout returns empty line
count = 0
while not r.startswith(":") and self.timeout >= count*0.040 :
count = count + 1
# try again a time or two
self.process.stdin.write('T\n')
self.process.stdin.flush()
r = self.process.stdout.readline()
if not r.startswith(":") :
if (self.verbose) : print "<none>" # blank line to show delay?
self.seenEnd = True
return None
# if verbose, display what's received
if (self.verbose) : print r,
return r
def close(self) :
self.process.kill()
self.process.wait()
return
def flush(self) : # reads past any pending input
while True :
r = self.process.stdout.readline()
if not r.startswith(":") :
self.seenEnd = True
break
if self.verbose : print " drop ",r,
return
def more(self) : # checks whether any more input in response to most recent stimulus
return False
import getopt, sys
def main():
global frame
# create connection object
network = PipeOlcbLink()
# get defaults
location = network.location
name = network.name
verbose = network.verbose
frame = ':X180A7000N;'
# process arguments
(location, name, frame, verbose) = args(location, name, frame, verbose)
# load new defaults
network.location = location
network.name = name
network.verbose = verbose
# send the frame
network.send(frame)
while True :
network.receive()
return # done with example
def usage(location, name) :
print ""
print "Python module for connecting to an OpenLCB via an Ethernet connection."
print "Called standalone, will send one CAN frame."
print ""
print "valid options:"
print " -v for verbose; also displays any responses"
print " -n, --name executable name, e.g. "+location
print " -l, --location directory to look for file, e.g. "+name
print ""
print "example:"
print " python pipeolcblink.py :X180A7000N;\;"
print ""
print "Note: Most shells require escaping the semicolon at the end of the frame."
def args(location, name, frame, verbose) :
# argument processing
try:
opts, remainder = getopt.getopt(sys.argv[1:], "n:l:vV", ["name=", "location="])
except getopt.GetoptError, err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
usage(location, name)
sys.exit(2)
for opt, arg in opts:
if opt == "-v":
verbose = True
elif opt == "-V":
verbose = True
elif opt in ("-l", "--location"):
location = arg
elif opt in ("-n", "--name"):
name = arg
else:
assert False, "unhandled option"
if (len(remainder) > 0) :
frame = remainder[0]
return (location, name, frame, verbose)
if __name__ == '__main__':
main()