Ram Gandikota
/
IOTMetronome
FRDM K64F Metronome
pal/Test/Scripts/mbed.py@0:dbad57390bd1, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:37:05 2017 +0000
- Revision:
- 0:dbad57390bd1
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:dbad57390bd1 | 1 | # ----------------------------------------------------------------------- |
ram54288 | 0:dbad57390bd1 | 2 | # Copyright (c) 2016 ARM Limited. All rights reserved. |
ram54288 | 0:dbad57390bd1 | 3 | # SPDX-License-Identifier: Apache-2.0 |
ram54288 | 0:dbad57390bd1 | 4 | # Licensed under the Apache License, Version 2.0 (the License); you may |
ram54288 | 0:dbad57390bd1 | 5 | # not use this file except in compliance with the License. |
ram54288 | 0:dbad57390bd1 | 6 | # You may obtain a copy of the License at |
ram54288 | 0:dbad57390bd1 | 7 | # |
ram54288 | 0:dbad57390bd1 | 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:dbad57390bd1 | 9 | # |
ram54288 | 0:dbad57390bd1 | 10 | # Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:dbad57390bd1 | 11 | # distributed under the License is distributed on an AS IS BASIS, WITHOUT |
ram54288 | 0:dbad57390bd1 | 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:dbad57390bd1 | 13 | # See the License for the specific language governing permissions and |
ram54288 | 0:dbad57390bd1 | 14 | # limitations under the License. |
ram54288 | 0:dbad57390bd1 | 15 | # ----------------------------------------------------------------------- |
ram54288 | 0:dbad57390bd1 | 16 | |
ram54288 | 0:dbad57390bd1 | 17 | import os |
ram54288 | 0:dbad57390bd1 | 18 | import shutil |
ram54288 | 0:dbad57390bd1 | 19 | from time import sleep, strftime |
ram54288 | 0:dbad57390bd1 | 20 | import serial |
ram54288 | 0:dbad57390bd1 | 21 | import re |
ram54288 | 0:dbad57390bd1 | 22 | import sys |
ram54288 | 0:dbad57390bd1 | 23 | |
ram54288 | 0:dbad57390bd1 | 24 | |
ram54288 | 0:dbad57390bd1 | 25 | class MbedDeviceManager: |
ram54288 | 0:dbad57390bd1 | 26 | def __init__(self): |
ram54288 | 0:dbad57390bd1 | 27 | try: |
ram54288 | 0:dbad57390bd1 | 28 | import mbed_lstools |
ram54288 | 0:dbad57390bd1 | 29 | except ImportError, e: |
ram54288 | 0:dbad57390bd1 | 30 | print("Error: Can't import 'mbed_lstools' module: %s"% e) |
ram54288 | 0:dbad57390bd1 | 31 | mbed = mbed_lstools.create() |
ram54288 | 0:dbad57390bd1 | 32 | self.mbed_list = mbed.list_mbeds() |
ram54288 | 0:dbad57390bd1 | 33 | for dev in self.mbed_list: |
ram54288 | 0:dbad57390bd1 | 34 | dev['Available'] = True |
ram54288 | 0:dbad57390bd1 | 35 | # part. structure:{'mount_point': 'D:','serial_port': u'COM3','platform_name': 'LPC1768','Available'=True} |
ram54288 | 0:dbad57390bd1 | 36 | |
ram54288 | 0:dbad57390bd1 | 37 | def dump_all_devices(self): |
ram54288 | 0:dbad57390bd1 | 38 | print("dump Devices:") |
ram54288 | 0:dbad57390bd1 | 39 | print(self.mbed_list) |
ram54288 | 0:dbad57390bd1 | 40 | |
ram54288 | 0:dbad57390bd1 | 41 | |
ram54288 | 0:dbad57390bd1 | 42 | def get_device(self, platform_type): |
ram54288 | 0:dbad57390bd1 | 43 | found = False |
ram54288 | 0:dbad57390bd1 | 44 | index = int(-1) |
ram54288 | 0:dbad57390bd1 | 45 | mount_point = None |
ram54288 | 0:dbad57390bd1 | 46 | serial_port = None |
ram54288 | 0:dbad57390bd1 | 47 | print(platform_type) |
ram54288 | 0:dbad57390bd1 | 48 | for idx,dev in enumerate(self.mbed_list): |
ram54288 | 0:dbad57390bd1 | 49 | if dev['platform_name'] == platform_type: |
ram54288 | 0:dbad57390bd1 | 50 | found = True |
ram54288 | 0:dbad57390bd1 | 51 | if dev['platform_name'] == platform_type and dev['Available']: |
ram54288 | 0:dbad57390bd1 | 52 | mount_point = dev['mount_point'] |
ram54288 | 0:dbad57390bd1 | 53 | serial_port = dev['serial_port'] |
ram54288 | 0:dbad57390bd1 | 54 | print('Detected %s. Mount point %s Serial port %s index %s.' |
ram54288 | 0:dbad57390bd1 | 55 | % (platform_type, mount_point, serial_port, idx)) |
ram54288 | 0:dbad57390bd1 | 56 | dev['Available'] = False |
ram54288 | 0:dbad57390bd1 | 57 | return idx, mount_point, serial_port |
ram54288 | 0:dbad57390bd1 | 58 | if found: |
ram54288 | 0:dbad57390bd1 | 59 | print('Device %s is not available already in use.' %platform_type) |
ram54288 | 0:dbad57390bd1 | 60 | else: |
ram54288 | 0:dbad57390bd1 | 61 | print('Device %s is not found.' %platform_type) |
ram54288 | 0:dbad57390bd1 | 62 | return index, mount_point, serial_port |
ram54288 | 0:dbad57390bd1 | 63 | |
ram54288 | 0:dbad57390bd1 | 64 | def free_device(self, index): |
ram54288 | 0:dbad57390bd1 | 65 | dev = self.mbed_list[index] |
ram54288 | 0:dbad57390bd1 | 66 | if not dev['Available']: |
ram54288 | 0:dbad57390bd1 | 67 | dev['Available'] = True |
ram54288 | 0:dbad57390bd1 | 68 | print('Release Device %s : %s .' % (index, dev['platform_name'])) |
ram54288 | 0:dbad57390bd1 | 69 | else: |
ram54288 | 0:dbad57390bd1 | 70 | print('Error: Device %s : %s is already free.' % (index, dev['platform_name']), ) |
ram54288 | 0:dbad57390bd1 | 71 | |
ram54288 | 0:dbad57390bd1 | 72 | mbed_manager = MbedDeviceManager() |
ram54288 | 0:dbad57390bd1 | 73 | |
ram54288 | 0:dbad57390bd1 | 74 | class MBED: |
ram54288 | 0:dbad57390bd1 | 75 | def __init__(self,platform_type): |
ram54288 | 0:dbad57390bd1 | 76 | self.manager = mbed_manager |
ram54288 | 0:dbad57390bd1 | 77 | self.platform_type = platform_type |
ram54288 | 0:dbad57390bd1 | 78 | self.mount_point = None |
ram54288 | 0:dbad57390bd1 | 79 | self.serial_port = None |
ram54288 | 0:dbad57390bd1 | 80 | self.index = -1 |
ram54288 | 0:dbad57390bd1 | 81 | self.running = False |
ram54288 | 0:dbad57390bd1 | 82 | |
ram54288 | 0:dbad57390bd1 | 83 | def detect(self): |
ram54288 | 0:dbad57390bd1 | 84 | self.index, self.mount_point, self.serial_port = self.manager.get_device(self.platform_type) |
ram54288 | 0:dbad57390bd1 | 85 | if self.index >= 0: |
ram54288 | 0:dbad57390bd1 | 86 | return True |
ram54288 | 0:dbad57390bd1 | 87 | else: |
ram54288 | 0:dbad57390bd1 | 88 | return False |
ram54288 | 0:dbad57390bd1 | 89 | |
ram54288 | 0:dbad57390bd1 | 90 | def generic_mbed_copy_win(self, source, destination): |
ram54288 | 0:dbad57390bd1 | 91 | |
ram54288 | 0:dbad57390bd1 | 92 | |
ram54288 | 0:dbad57390bd1 | 93 | from win32com.shell import shell, shellcon |
ram54288 | 0:dbad57390bd1 | 94 | import pythoncom |
ram54288 | 0:dbad57390bd1 | 95 | from os.path import abspath, join |
ram54288 | 0:dbad57390bd1 | 96 | from glob import glob |
ram54288 | 0:dbad57390bd1 | 97 | |
ram54288 | 0:dbad57390bd1 | 98 | for f in glob(join(destination, '*.bin')): |
ram54288 | 0:dbad57390bd1 | 99 | os.unlink(f) |
ram54288 | 0:dbad57390bd1 | 100 | |
ram54288 | 0:dbad57390bd1 | 101 | src = shell.SHCreateItemFromParsingName(source, None, shell.IID_IShellItem) |
ram54288 | 0:dbad57390bd1 | 102 | dest_dir = shell.SHCreateItemFromParsingName( |
ram54288 | 0:dbad57390bd1 | 103 | abspath(destination), |
ram54288 | 0:dbad57390bd1 | 104 | None, |
ram54288 | 0:dbad57390bd1 | 105 | shell.IID_IShellItem |
ram54288 | 0:dbad57390bd1 | 106 | ) |
ram54288 | 0:dbad57390bd1 | 107 | pfo = pythoncom.CoCreateInstance( |
ram54288 | 0:dbad57390bd1 | 108 | shell.CLSID_FileOperation, |
ram54288 | 0:dbad57390bd1 | 109 | None, |
ram54288 | 0:dbad57390bd1 | 110 | pythoncom.CLSCTX_ALL, |
ram54288 | 0:dbad57390bd1 | 111 | shell.IID_IFileOperation |
ram54288 | 0:dbad57390bd1 | 112 | ) |
ram54288 | 0:dbad57390bd1 | 113 | pfo.SetOperationFlags(shellcon.FOF_NO_UI) |
ram54288 | 0:dbad57390bd1 | 114 | pfo.CopyItem(src, dest_dir, None, None) |
ram54288 | 0:dbad57390bd1 | 115 | pfo.PerformOperations() |
ram54288 | 0:dbad57390bd1 | 116 | |
ram54288 | 0:dbad57390bd1 | 117 | return True |
ram54288 | 0:dbad57390bd1 | 118 | |
ram54288 | 0:dbad57390bd1 | 119 | def install_bin(self,bin_file_name): |
ram54288 | 0:dbad57390bd1 | 120 | bin_on_mbed = os.path.join(self.mount_point,os.path.basename(bin_file_name)) |
ram54288 | 0:dbad57390bd1 | 121 | print('%s Copying %s --> %s' %(strftime('%H:%M:%S'),bin_file_name,bin_on_mbed)) |
ram54288 | 0:dbad57390bd1 | 122 | if 'win' in sys.platform: |
ram54288 | 0:dbad57390bd1 | 123 | self.generic_mbed_copy_win(os.path.abspath(bin_file_name), os.path.abspath(self.mount_point)) |
ram54288 | 0:dbad57390bd1 | 124 | else: |
ram54288 | 0:dbad57390bd1 | 125 | shutil.copyfile(bin_file_name,bin_on_mbed) |
ram54288 | 0:dbad57390bd1 | 126 | self.wait_for_file_system() |
ram54288 | 0:dbad57390bd1 | 127 | print('%s Bin installation complete' %strftime('%H:%M:%S')) |
ram54288 | 0:dbad57390bd1 | 128 | |
ram54288 | 0:dbad57390bd1 | 129 | def run(self,serial_capture_file_name,baud=9600,read_timeout=10,stop_on_serial_read_timeout=False): |
ram54288 | 0:dbad57390bd1 | 130 | if serial_capture_file_name is not None: |
ram54288 | 0:dbad57390bd1 | 131 | self.stop_on_serial_read_timeout = stop_on_serial_read_timeout |
ram54288 | 0:dbad57390bd1 | 132 | from multiprocessing import Process, Event, Pipe |
ram54288 | 0:dbad57390bd1 | 133 | self.event = Event() |
ram54288 | 0:dbad57390bd1 | 134 | parent_conn, child_conn = Pipe() |
ram54288 | 0:dbad57390bd1 | 135 | print('Start serial capture process') |
ram54288 | 0:dbad57390bd1 | 136 | process_args=(self.serial_port,baud,read_timeout,serial_capture_file_name,stop_on_serial_read_timeout,self.event,child_conn) |
ram54288 | 0:dbad57390bd1 | 137 | self.capture_process = Process(target=capture_serial,args=process_args) |
ram54288 | 0:dbad57390bd1 | 138 | self.capture_process.start() |
ram54288 | 0:dbad57390bd1 | 139 | print('Waiting for pipe input from subprocess') |
ram54288 | 0:dbad57390bd1 | 140 | self.ip,self.port = parent_conn.recv() |
ram54288 | 0:dbad57390bd1 | 141 | if not self.port or not self.ip : |
ram54288 | 0:dbad57390bd1 | 142 | return 1 |
ram54288 | 0:dbad57390bd1 | 143 | |
ram54288 | 0:dbad57390bd1 | 144 | print('Received IP %s port %s'%(self.ip,self.port)) |
ram54288 | 0:dbad57390bd1 | 145 | else: |
ram54288 | 0:dbad57390bd1 | 146 | print('Running without serial capture') |
ram54288 | 0:dbad57390bd1 | 147 | self.ip,self.port = run_no_capture(self.serial_port,baud,read_timeout) |
ram54288 | 0:dbad57390bd1 | 148 | print('%s IP %s port %s' % (self.platform_type, self.ip, self.port)) |
ram54288 | 0:dbad57390bd1 | 149 | self.running = True |
ram54288 | 0:dbad57390bd1 | 150 | |
ram54288 | 0:dbad57390bd1 | 151 | def end_run(self, delete_binaries=True, release_manager=True): |
ram54288 | 0:dbad57390bd1 | 152 | print('MBED end_run') |
ram54288 | 0:dbad57390bd1 | 153 | if self.running: |
ram54288 | 0:dbad57390bd1 | 154 | if not self.stop_on_serial_read_timeout: |
ram54288 | 0:dbad57390bd1 | 155 | self.event.set() # the thread does not stop on its own. send stop signal |
ram54288 | 0:dbad57390bd1 | 156 | print('MBED end_run waiting for subprocess to terminate') |
ram54288 | 0:dbad57390bd1 | 157 | self.capture_process.join() # wait for completion |
ram54288 | 0:dbad57390bd1 | 158 | print('MBED end_run subprocess terminated') |
ram54288 | 0:dbad57390bd1 | 159 | if delete_binaries: |
ram54288 | 0:dbad57390bd1 | 160 | print('MBED end_run deleting binaries') |
ram54288 | 0:dbad57390bd1 | 161 | # delete all binaries on MBED |
ram54288 | 0:dbad57390bd1 | 162 | filelist = [ f for f in os.listdir(self.mount_point) if f.endswith(".bin") ] |
ram54288 | 0:dbad57390bd1 | 163 | for f in filelist: |
ram54288 | 0:dbad57390bd1 | 164 | print('MBED end_run delete %s',f) |
ram54288 | 0:dbad57390bd1 | 165 | os.remove(os.path.join(self.mount_point,f)) |
ram54288 | 0:dbad57390bd1 | 166 | self.wait_for_file_system() |
ram54288 | 0:dbad57390bd1 | 167 | print('MBED end_run binary delete completed') |
ram54288 | 0:dbad57390bd1 | 168 | self.running = False |
ram54288 | 0:dbad57390bd1 | 169 | if release_manager: |
ram54288 | 0:dbad57390bd1 | 170 | self.manager.free_device(self.index) |
ram54288 | 0:dbad57390bd1 | 171 | |
ram54288 | 0:dbad57390bd1 | 172 | def run_and_capture_till_timeout(self,serial_capture_file_name,baud=9600,read_timeout=10,endOfData=None): |
ram54288 | 0:dbad57390bd1 | 173 | try: |
ram54288 | 0:dbad57390bd1 | 174 | print('[%s run_and_capture_till_timeout] Start' %strftime('%H:%M:%S')) |
ram54288 | 0:dbad57390bd1 | 175 | ser = serial.Serial(self.serial_port,baudrate=baud,timeout=read_timeout) |
ram54288 | 0:dbad57390bd1 | 176 | if ser == None: |
ram54288 | 0:dbad57390bd1 | 177 | print(' serial.Serial returned None..') |
ram54288 | 0:dbad57390bd1 | 178 | capture_file = open(serial_capture_file_name,'w') |
ram54288 | 0:dbad57390bd1 | 179 | read_size = 1000 |
ram54288 | 0:dbad57390bd1 | 180 | cont = True |
ram54288 | 0:dbad57390bd1 | 181 | print('[%s run_and_capture_till_timeout] Reseting device..' %strftime('%H:%M:%S')) |
ram54288 | 0:dbad57390bd1 | 182 | c = reset_mbed(ser) |
ram54288 | 0:dbad57390bd1 | 183 | if c == None: |
ram54288 | 0:dbad57390bd1 | 184 | cont = False |
ram54288 | 0:dbad57390bd1 | 185 | else: |
ram54288 | 0:dbad57390bd1 | 186 | capture_file.write(c) |
ram54288 | 0:dbad57390bd1 | 187 | print('[%s run_and_capture_till_timeout] capturing to file...' %strftime('%H:%M:%S')) |
ram54288 | 0:dbad57390bd1 | 188 | while cont: |
ram54288 | 0:dbad57390bd1 | 189 | c = ser.read(read_size) |
ram54288 | 0:dbad57390bd1 | 190 | # Look for the end of test data string and terminate if it is found. |
ram54288 | 0:dbad57390bd1 | 191 | if endOfData != None: |
ram54288 | 0:dbad57390bd1 | 192 | endPos = c.find(endOfData) |
ram54288 | 0:dbad57390bd1 | 193 | # Clip off the termination string and anything afterwards |
ram54288 | 0:dbad57390bd1 | 194 | if endPos != -1: |
ram54288 | 0:dbad57390bd1 | 195 | c = c[:(endPos + len(endOfData))] |
ram54288 | 0:dbad57390bd1 | 196 | capture_file.write(c) |
ram54288 | 0:dbad57390bd1 | 197 | if endPos != -1: |
ram54288 | 0:dbad57390bd1 | 198 | break |
ram54288 | 0:dbad57390bd1 | 199 | if len(c) < read_size: |
ram54288 | 0:dbad57390bd1 | 200 | print('[%s run_and_capture_till_timeout] serial read timeout. Stopping subprocess' %strftime('%H:%M:%S')) |
ram54288 | 0:dbad57390bd1 | 201 | print ("exit last Buffsize " + str(len(c)) + "<32" ) |
ram54288 | 0:dbad57390bd1 | 202 | cont = False |
ram54288 | 0:dbad57390bd1 | 203 | print('[%s run_and_capture_till_timeout] closing capture file' %strftime('%H:%M:%S')) |
ram54288 | 0:dbad57390bd1 | 204 | capture_file.close() |
ram54288 | 0:dbad57390bd1 | 205 | print('[%s run_and_capture_till_timeout] closing serial port' %strftime('%H:%M:%S')) |
ram54288 | 0:dbad57390bd1 | 206 | ser.flushInput() |
ram54288 | 0:dbad57390bd1 | 207 | ser.flushOutput() |
ram54288 | 0:dbad57390bd1 | 208 | ser.close() |
ram54288 | 0:dbad57390bd1 | 209 | print('[%s run_and_capture_till_timeout] done' %strftime('%H:%M:%S')) |
ram54288 | 0:dbad57390bd1 | 210 | return True |
ram54288 | 0:dbad57390bd1 | 211 | except serial.SerialException as e: |
ram54288 | 0:dbad57390bd1 | 212 | print('[run_and_capture_till_timeout] serial exception',e) |
ram54288 | 0:dbad57390bd1 | 213 | return False |
ram54288 | 0:dbad57390bd1 | 214 | |
ram54288 | 0:dbad57390bd1 | 215 | def wait_for_file_system(self): |
ram54288 | 0:dbad57390bd1 | 216 | #sleep(25) #MBED file system takes some 'settling' time after it is wrirtten to |
ram54288 | 0:dbad57390bd1 | 217 | sleep(3) #MBED file system takes some 'settling' time after it is wrirtten to |
ram54288 | 0:dbad57390bd1 | 218 | |
ram54288 | 0:dbad57390bd1 | 219 | def reset_mbed(ser): |
ram54288 | 0:dbad57390bd1 | 220 | for loop in range(5): |
ram54288 | 0:dbad57390bd1 | 221 | # reset called after open port |
ram54288 | 0:dbad57390bd1 | 222 | # add sleep for port ready (we saw that sometimes read failed...) |
ram54288 | 0:dbad57390bd1 | 223 | delay = 5 |
ram54288 | 0:dbad57390bd1 | 224 | print('[%s reset_mbed] loop=%d , delay=%d' %(strftime('%H:%M:%S'), loop, delay)) |
ram54288 | 0:dbad57390bd1 | 225 | sleep(delay) |
ram54288 | 0:dbad57390bd1 | 226 | try: |
ram54288 | 0:dbad57390bd1 | 227 | ser.sendBreak() |
ram54288 | 0:dbad57390bd1 | 228 | except Exception: |
ram54288 | 0:dbad57390bd1 | 229 | # In linux a termios.error is raised in sendBreak and in |
ram54288 | 0:dbad57390bd1 | 230 | # setBreak. The following setBreak() is needed to release |
ram54288 | 0:dbad57390bd1 | 231 | # the reset signal on the target mcu. |
ram54288 | 0:dbad57390bd1 | 232 | try: |
ram54288 | 0:dbad57390bd1 | 233 | ser.setBreak(False) |
ram54288 | 0:dbad57390bd1 | 234 | except: |
ram54288 | 0:dbad57390bd1 | 235 | pass |
ram54288 | 0:dbad57390bd1 | 236 | c = ser.read(1) |
ram54288 | 0:dbad57390bd1 | 237 | if len(c) == 1: |
ram54288 | 0:dbad57390bd1 | 238 | return c |
ram54288 | 0:dbad57390bd1 | 239 | print ("Error reading from serial port" ) |
ram54288 | 0:dbad57390bd1 | 240 | return None |
ram54288 | 0:dbad57390bd1 | 241 | |
ram54288 | 0:dbad57390bd1 | 242 | def is_valid_ip(ip): |
ram54288 | 0:dbad57390bd1 | 243 | return re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$",ip) |
ram54288 | 0:dbad57390bd1 | 244 | |
ram54288 | 0:dbad57390bd1 | 245 | def capture_serial(port,baud,read_timeout,capture_file_name,stop_on_serial_read_timeout,event,pipe_conn): |
ram54288 | 0:dbad57390bd1 | 246 | try: |
ram54288 | 0:dbad57390bd1 | 247 | print('[capture_serial subprocess] starting..') |
ram54288 | 0:dbad57390bd1 | 248 | ser = serial.Serial(port,baudrate=baud,timeout=read_timeout) |
ram54288 | 0:dbad57390bd1 | 249 | capture_file = open(capture_file_name,'w') |
ram54288 | 0:dbad57390bd1 | 250 | read_size = 32 |
ram54288 | 0:dbad57390bd1 | 251 | cont = True |
ram54288 | 0:dbad57390bd1 | 252 | print('[capture_serial subprocess] Reseting device..') |
ram54288 | 0:dbad57390bd1 | 253 | c = reset_mbed(ser) |
ram54288 | 0:dbad57390bd1 | 254 | if c == None: |
ram54288 | 0:dbad57390bd1 | 255 | print('[capture_serial subprocess] Reseting device failed') |
ram54288 | 0:dbad57390bd1 | 256 | |
ram54288 | 0:dbad57390bd1 | 257 | if c == None: |
ram54288 | 0:dbad57390bd1 | 258 | cont = False |
ram54288 | 0:dbad57390bd1 | 259 | print('[capture_serial subprocess] Failed to get IP from device... exiting ') |
ram54288 | 0:dbad57390bd1 | 260 | pipe_conn.send([None,None]) # send IP and port |
ram54288 | 0:dbad57390bd1 | 261 | pipe_conn.close() |
ram54288 | 0:dbad57390bd1 | 262 | capture_file.close() |
ram54288 | 0:dbad57390bd1 | 263 | ser.flushInput() |
ram54288 | 0:dbad57390bd1 | 264 | ser.flushOutput() |
ram54288 | 0:dbad57390bd1 | 265 | ser.close() |
ram54288 | 0:dbad57390bd1 | 266 | return |
ram54288 | 0:dbad57390bd1 | 267 | else: |
ram54288 | 0:dbad57390bd1 | 268 | # parse the first received string for IP and port |
ram54288 | 0:dbad57390bd1 | 269 | ip_str = c.split(':') |
ram54288 | 0:dbad57390bd1 | 270 | if is_valid_ip(ip_str[1]) : |
ram54288 | 0:dbad57390bd1 | 271 | pipe_conn.send([ip_str[1],ip_str[2]]) # send IP and port |
ram54288 | 0:dbad57390bd1 | 272 | print('[capture_serial subprocess] Sending IP to main process %s:%s'%(ip_str[1],ip_str[2])) |
ram54288 | 0:dbad57390bd1 | 273 | print('[capture_serial subprocess] capturing to file...') |
ram54288 | 0:dbad57390bd1 | 274 | else: |
ram54288 | 0:dbad57390bd1 | 275 | print('[capture_serial subprocess] No valid IP address') |
ram54288 | 0:dbad57390bd1 | 276 | pipe_conn.send([None,None]) # send IP and port |
ram54288 | 0:dbad57390bd1 | 277 | cont = False |
ram54288 | 0:dbad57390bd1 | 278 | capture_file.write(c) |
ram54288 | 0:dbad57390bd1 | 279 | pipe_conn.close() |
ram54288 | 0:dbad57390bd1 | 280 | |
ram54288 | 0:dbad57390bd1 | 281 | while cont: |
ram54288 | 0:dbad57390bd1 | 282 | c = ser.read(read_size) |
ram54288 | 0:dbad57390bd1 | 283 | capture_file.write(c) |
ram54288 | 0:dbad57390bd1 | 284 | if stop_on_serial_read_timeout: |
ram54288 | 0:dbad57390bd1 | 285 | if len(c) < read_size: |
ram54288 | 0:dbad57390bd1 | 286 | print('[capture_serial subprocess] serial read timeout. Stopping subprocess') |
ram54288 | 0:dbad57390bd1 | 287 | cont = False |
ram54288 | 0:dbad57390bd1 | 288 | else: |
ram54288 | 0:dbad57390bd1 | 289 | if event.is_set(): |
ram54288 | 0:dbad57390bd1 | 290 | print('[capture_serial subprocess] event is set. Stopping subprocess') |
ram54288 | 0:dbad57390bd1 | 291 | cont = False |
ram54288 | 0:dbad57390bd1 | 292 | |
ram54288 | 0:dbad57390bd1 | 293 | print('[capture_serial subprocess] closing capture file') |
ram54288 | 0:dbad57390bd1 | 294 | capture_file.close() |
ram54288 | 0:dbad57390bd1 | 295 | print('[capture_serial subprocess] closing serial port') |
ram54288 | 0:dbad57390bd1 | 296 | ser.flushInput() |
ram54288 | 0:dbad57390bd1 | 297 | ser.flushOutput() |
ram54288 | 0:dbad57390bd1 | 298 | ser.close() |
ram54288 | 0:dbad57390bd1 | 299 | print('[capture_serial subprocess] Subprocess exiting') |
ram54288 | 0:dbad57390bd1 | 300 | except serial.SerialException as e: |
ram54288 | 0:dbad57390bd1 | 301 | print('[capture_serial subprocess] serial exception',e) |
ram54288 | 0:dbad57390bd1 | 302 | |
ram54288 | 0:dbad57390bd1 | 303 | def run_no_capture(port,baud,read_timeout): |
ram54288 | 0:dbad57390bd1 | 304 | try: |
ram54288 | 0:dbad57390bd1 | 305 | ser = serial.Serial(port,baudrate=baud,timeout=read_timeout) |
ram54288 | 0:dbad57390bd1 | 306 | ip = None |
ram54288 | 0:dbad57390bd1 | 307 | port = None |
ram54288 | 0:dbad57390bd1 | 308 | c = reset_mbed(ser) |
ram54288 | 0:dbad57390bd1 | 309 | if c: |
ram54288 | 0:dbad57390bd1 | 310 | # parse the first received string for IP and port |
ram54288 | 0:dbad57390bd1 | 311 | ip_str = c.split(':') |
ram54288 | 0:dbad57390bd1 | 312 | if is_valid_ip(ip_str[1]) != None: |
ram54288 | 0:dbad57390bd1 | 313 | ip = ip_str[1] |
ram54288 | 0:dbad57390bd1 | 314 | port = ip_str[2] |
ram54288 | 0:dbad57390bd1 | 315 | ser.flushInput() |
ram54288 | 0:dbad57390bd1 | 316 | ser.flushOutput() |
ram54288 | 0:dbad57390bd1 | 317 | ser.close() |
ram54288 | 0:dbad57390bd1 | 318 | return ip,port |
ram54288 | 0:dbad57390bd1 | 319 | except serial.SerialException as e: |
ram54288 | 0:dbad57390bd1 | 320 | print e |
ram54288 | 0:dbad57390bd1 | 321 | |
ram54288 | 0:dbad57390bd1 | 322 | |
ram54288 | 0:dbad57390bd1 | 323 | class MBED_DEVICE(MBED): |
ram54288 | 0:dbad57390bd1 | 324 | def __init__(self,platform_type): |
ram54288 | 0:dbad57390bd1 | 325 | print('MBED Device:' + platform_type) |
ram54288 | 0:dbad57390bd1 | 326 | MBED.__init__(self, platform_type) |
ram54288 | 0:dbad57390bd1 | 327 | |
ram54288 | 0:dbad57390bd1 | 328 |