I presume this is something really simple, but i cant work out what! :-[
When I run the python script to load the hex file I get the error that the .hex file can't be found so the python script exits.
If I type in the file path into command prompt then it finds the hex file just fine. But if I use that path as the -f argument I can't find it. Ive also tried putting the path in the .py file as mentioned by someone in another post with no effect.
I have copied the hex file to the same directory as the WirelessProgramming.py. Its just from the blink example, called Blink.cpp.hex
not really versed in python so i presume there is some nuance that needs doing for this to work?
thanks in advance.
Hm, I don't recall anyone having issues with that specifically. I did notice I made a change to my python file which strips() the HEX parameter, so I uploaded that to github right now. Please try the latest python script and let me know if it's still happening. If so, then I'm not sure what to suggest. Make sure to specify all parameters ...
I had some trouble with this, seems passing arguments to Python was different on Windows versus Linux. I believe I had to remove the space between the flag and the argument, like "-fnewsketch.hex"
To debug, add a print command like this:
if (sys.argv[i] == "-f" or sys.argv[i] == "-file") and len(sys.argv) >= i+2:
HEX = sys.argv[i+1].strip()
print "FILE arguments is [", HEX, "]"
If you see an extra space, try my trick above. Otherwise use the printout to make sure the filename you pass to the Python script matches the filename of the HEX file you want to send to the remote Moteino.
Felix, since the command line argument handling of "import sys" gave me trouble, I switched to "import getopt" and it's worked better. Plus it has some nice features like support for short and long commands, none or many arguments, and specifying which commands require arguments. Here's my code:
# Read command line arguments
try:
opts, args = getopt.getopt(sys.argv[1:],"df:m:s:b:h", ["help"])
except getopt.GetoptError as err:
print str(err) # will print something like "option -a not recognized"
usage_help() # print out the usage help
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage_help()
exit(0)
elif opt == "-d":
DEBUG = True
elif opt == "-f":
HEX = arg
elif opt in '-m':
MOTEID = int(arg)
if (MOTEID < 1) or (MOTEID > 255):
print "ERROR: Target ID {", MOTEID, "} invalid, must be 1-255."
exit(1)
elif opt == "-s":
SERIALPORT = arg
elif opt == "-b":
BAUD = arg
def usage_help():
print " -d Turn on debugging"
print " -f {file} HEX file to upload (Default: ", HEX, ")"
print " -m {ID} ID of target Moteino to be programmed (Default: ", MOTEID, ")"
print " -s {port} Serial port (Default: ", SERIALPORT, ")"
print " -b {baud} Baud rate of serial port (Default: ", BAUDRATE, ")"
print " -h, --help Print this message"
I also added some code to my Python program to check the first line of the HEX file to make sure it looks like a HEX file. I figure there's no use uploading a bunch of data to a Moteino to have it rejected at the end when a simple sniff test at the beginning might prevent the bad upload.
with open(HEX) as f:
print "File found, passing to Moteino..."
content = f.readlines()
# the first line of a valid HEX file should start with these bytes
if content[0].find(':100000000C94') > -1:
print "HEX file looks valid, passing to gateway Moteino..."
else:
print "not a HEX file, exiting..."
ser.close()
exit(1)
Thanks for the replies but i'm still getting the same problem.
I've downloaded the new file and adding the debug code from Kris I get;
c:\Python27>WirelessProgramming.py -f c:\Python27\Blink.cpp.hex -t 5 -s 6 -b 115
200
FILE arguments is [ c:\Python27\Blink.cpp.hex ]
File [ c:\Python27\Blink.cpp.hex ] not found, exiting...
This is definitly the correct path as if i type that into command prompt it tries to open the file
Removing the space means the file doesnt get passed into the code, it just tries to use the default flash.hex
c:\Python27>WirelessProgramming.py -fc:\Python27\Blink.cpp.hex -t 5 -s 6 -b 1152
00
File [ flash.hex ] not found, exiting...
I also tried the code Kris posted but same problem :-\
Edit: forgot to mention I am using windows 7 not linux
I don't know man this is really weird. I think I had something like that some time, but I can't recall what it was, probably some kind of parameter issue, wasn't setting it right. The script *DOES* work.
Maybe the open command is having trouble with the full path, try changing to the Python27 directory and just use the filename like this:
cd c:/Python27
WirelessProgramming.py -f Blink.cpp.hex -s COM6 -b 115200
Another idea, maybe Python doesn't like backslashes "\" so try forward slashes "/".
Note that "-s 6" isn't valid. You need to specify the full serial port name like "-s COM6".
Note that you don't need the -t option. That is for my pull style wireless programming. I removed it from the code above. You can leave it in your program, it won't hurt anything, or remove it.
Good catch Kris, "-s 6" might be the problem
In case anyone else has this issue- I had the same problem when I was trying to test the python script without the gateway moteino connected to the computer- when I actually plugged it in this error went away.
Right. I've observed this.
If the com port doesn't exist, the python script hits an exception and gives the 'file not found' error, which is slightly misleading.
I ran into this issue as well until I found this post. I made a change, perhaps this could be integrated into the script:
try:
# open up the FTDI serial port to get data transmitted to Moteino
ser = serial.Serial(SERIALPORT, BAUDRATE, timeout=1) #timeout=0 means nonblocking
time.sleep(2) #wait for Moteino reset after port open and potential bootloader time (~1.6s)
ser.flushInput();
except IOError as e:
print "COM Port [", SERIALPORT, "] not found, exiting..."
exit(1)
try:
Integrated!
Changes checked in: https://github.com/LowPowerLab/WirelessProgramming/blob/master/WirelessProgramming.py
Thanks ssmall!