I am trying to write a code in python to automate the work that I normally do manually. The work is simply modifying some parts of C code and then, insert the below command to terminal to build, compile and bootload C code for each Z1 Zolertia motes.

sudo scons board=z1 toolchain=mspgcc bootload=/dev/ttyUSB0 oos_openwsn    

The command works fine from terminal or inside bash script, but when I run it from python script it doesn't work properly.

I believe the problem is: Build and compile stages successfully finish, but bootloading stage (Mass Erase) runs twice, thus port is off and cannot be seen anymore.

This part of the code making trouble:

subprocess.call('(cd /home/ilkin/git/Yadhunandana-testbed/openwsn-fw ; sudo scons board=z1 toolchain=mspgcc bootload=/dev/ttyUSB0 oos_openwsn)', shell=True)

This is the output of building, compiling and bootloading stage. As you see, "Mass Erase" runs concurrently, thus it fails.

sudo python automate.py 

scons: Reading SConscript files ...

___                 _ _ _  ___  _ _ 
| . | ___  ___ ._ _ | | | |/ __>| \ |
| | || . \/ ._>| ' || | | |\__ \|   |
`___'|  _/\___.|_|_||__/_/ <___/|_\_|
    |_|                  openwsn.org

none
scons: done reading SConscript files.
scons: Building targets ...
Dynifying build/z1_mspgcc/openapps/openapps_dyn.c
msp430-size build/z1_mspgcc/projects/common/03oos_openwsn_prog
  text    data     bss     dec     hex filename
 51442     532    6598   58572    e4cc build/z1_mspgcc/projects/common/03oos_openwsn_prog
z1_bootload(["build/z1_mspgcc/projects/common/03oos_openwsn_prog.phonyupload"], ["build/z1_mspgcc/projects/common/03oos_openwsn_prog.ihex"])
starting bootloading on /dev/ttyUSB0
MSP430 Bootstrap Loader Version: 1.39-goodfet-8
Mass Erase...
MSP430 Bootstrap Loader Version: 1.39-goodfet-8
Mass Erase...
Traceback (most recent call last):
 File "bootloader/z1/z1-bsl-nopic", line 1925, in <module>
   main(0);
 File "bootloader/z1/z1-bsl-nopic", line 1840, in main
   for f in toinit: f()
 File "bootloader/z1/z1-bsl-nopic", line 1102, in actionMassErase
   0xa506)             #Required setting for mass erase!
 File "bootloader/z1/z1-bsl-nopic", line 761, in bslTxRx
   self.bslSync(wait)                          #synchronize BSL
 File "bootloader/z1/z1-bsl-nopic", line 720, in bslSync
   raise BSLException(self.ERR_BSL_SYNC)       #Sync. failed
__main__.BSLException: Bootstrap loader synchronization error
done bootloading on /dev/ttyUSB0
scons: done building targets.

This is not case when I run the command manually from terminal, "Mass Erase" part runs just once and bootloading finish successfully. I expect output like this:

(cd /home/ilkin/git/Yadhunandana-testbed/openwsn-fw ; sudo scons board=z1 toolchain=mspgcc bootload=/dev/ttyUSB0 oos_openwsn)

scons: Reading SConscript files ...

___                 _ _ _  ___  _ _ 
| . | ___  ___ ._ _ | | | |/ __>| \ |
| | || . \/ ._>| ' || | | |\__ \|   |
`___'|  _/\___.|_|_||__/_/ <___/|_\_|
    |_|                  openwsn.org

none
scons: done reading SConscript files.
scons: Building targets ...
Dynifying build/z1_mspgcc/openapps/openapps_dyn.c
msp430-size build/z1_mspgcc/projects/common/03oos_openwsn_prog
  text    data     bss     dec     hex filename
 51442     532    6598   58572    e4cc build/z1_mspgcc/projects/common/03oos_openwsn_prog
z1_bootload(["build/z1_mspgcc/projects/common/03oos_openwsn_prog.phonyupload"], ["build/z1_mspgcc/projects/common/03oos_openwsn_prog.ihex"])
starting bootloading on /dev/ttyUSB0
MSP430 Bootstrap Loader Version: 1.39-goodfet-8
Mass Erase...
Transmit default password ...
Invoking BSL...
Transmit default password ...
Current bootstrap loader version: 2.13 (Device ID: f26f)
Changing baudrate to 38400 ...
Program ...
51974 bytes programmed.
Reset device ...
done bootloading on /dev/ttyUSB0
scons: done building targets.

Thank you in advance!

1

There are 1 answers

0
Ilkin On

Luckily, I have solved the problem. The problem happened because of using a function in my code to list connected serial devices. And the main reason is because this function closing the serial devices after listing them. Thus, the serial port is not seen in this short period since bootloading stage starts after some lines when serial is closed (in microseconds).

def serial_ports():

ports = glob.glob("/dev/ttyUSB*")

result = []
for elements in reversed(ports):
    try:
        s = serial.Serial(elements)
        s.close()
        result.append(elements)
    except (IOError, serial.SerialException):
        pass