-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoplevel.py
60 lines (49 loc) · 1.65 KB
/
toplevel.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
#------------------------------------------------------------------------------
# Name: toplevel.py
# Purpose: Top level controller for launching and controlling
# child processes
#
# Author: jsafrit
# Created: 07/31/2014
#------------------------------------------------------------------------------
import serial
import shlex,subprocess
import time
import sys
MCOM = 'COM8'
SCOM = 'COM9'
def createVirtualComms(comm1='COM8', comm2='COM9'):
'''create virtual com ports for testing link'''
cmdline = 'socat PTY,link={} PTY,link={}'.format(comm1,comm2)
args = shlex.split(cmdline)
pComms = subprocess.Popen(args)
#wait minimum time to ensure ports are up before returning
time.sleep(1)
if not pComms:
print('Attempt to open comms failed. Exiting...')
sys.exit(3)
return pComms
def createChildProcess(script,comm):
'''create child process connected to comm'''
cmdline = 'python3 {} {}'.format(script, comm)
args = shlex.split(cmdline)
pChild = subprocess.Popen(args)
#wait minimum time to ensure ports are up before returning
time.sleep(1)
if not pChild:
print('Attempt to create child failed. Exiting...')
sys.exit(4)
return pChild
def main():
#create virtual comms
print('Calling {}...'.format('createVirtualComms'))
toplevel = createVirtualComms(MCOM,SCOM)
if toplevel:
print('Sucessful!')
#toplevel.terminate()
masterChild = createChildProcess('master.py',MCOM)
slaveChild = createChildProcess('sensor.py',SCOM)
toplevel.terminate()
print('Complete.')
if __name__ == '__main__':
main()