############################################################################### # Object: Interface to FlexStack Camera demo # Author: antics( christopher pepe ) # # Descritpion: Host side application that uploads a jpg image from the # flexstack demo. BMP is scattered thru this file because it # was built from another host app that worked with bitmaps. # # This application is intended to run on a host with bluetooth # and connect to the FlexStack via the RFCOMM profile for the demo. # In practice it would be useful with any RS232 to radio converter, # ideally radios with some real range like a few miles. # # Use this front end to issue a command to the embedded system and # upload the resultant image if needed. This program is fairly # customized for the demo its used in and should be modified to be # more generic or easier to setup. ############################################################################### import os import sys import serial import getopt from time import time def binDummy(fname): """ This function revert the CRLF (carriage return, line feed) characters to '0x0a'. Because the embedded system simply prints the binary data to a terminal the '0x0a' characters are automatically translated to '0x0d0a'. This function undoes that. """ fout=open(fname,'wb') fd=open('dummy','rb') byte = fd.read(1) while byte != '': next = fd.read(1) if byte == '\x0d' and next == '\x0a': byte = next next = fd.read(1) fout.write(byte) byte = next fd.close() fout.close() CHUNKSIZE = 2048 #default serial device, file name, and command SERDEV = '/dev/ttyUSB0' BMPFILE = 'dummy' CMD = 'TN' #override defaults with commandline options #pyHostController.py -c -d /dev/tty -f opts,args = getopt.getopt(sys.argv[1:] , 'f:d:c:z' ) opts = dict(opts) cmd = opts.get('-c', CMD) serdev = opts.get('-d', SERDEV) bmpfile = opts.get('-f', BMPFILE) #open the serial device ser = serial.Serial(serdev, 57600, timeout=2, rtscts=0, xonxoff=0, parity=serial.PARITY_NONE) if ser.isOpen(): #print 'Serial port ' + str(ser.portstr) + ' is open' fake=1 else: print 'Serial port ' + str(ser.portstr) + " can't be opened" if 1: #FIXME: assuming connection is up, (isUp != '':) startTime = time() bmpparts = [] cnkCnt = 1 #Send command to FlexStack ser.write(cmd + "\r\n") #$ is the start char, read # the serial port until $ # is recieved. fReady = ser.read(1) if fReady != '$': fReady = ser.read(1) #Grab the header which is no more # than 100 chars long. header = ser.read(100) hList = header.split(':') try: filename = hList[0].split('$')[1] except: filename = hList[0] filesize = hList[1] #chunk0 is the leftover binary data #once the filename and filesize have #been stripped out chunk0 = hList[2] #Write chunk0 to file for debugging #debug = file('debug.txt', 'wb') #debug.write(chunk0) #debug.close() #print 'Filename: ' + filename #print 'Filesize: ' + filesize chunk = ser.read(CHUNKSIZE) while len(chunk) == CHUNKSIZE: bmpparts.append(chunk) chunk = ser.read(2048) #print 'Reading Chunk ' + str(cnkCnt) + ' [' + str(CHUNKSIZE) + ' bytes]' cnkCnt += 1 #There are some extra chars at the end of the #stream because the embedded shell gets confused. #Time constraints dictated it being fixed here since #the extra output is always the same. #rm '\r\nACK\r\ncmd>cmd>' bmpparts.append(chunk[:-12]) ###use filename to write temp file which contains ###extra characters fd = file(bmpfile,'wb') #fd.write(bmp) fd.write(chunk0) for chunk in bmpparts: fd.write(chunk) fd.close() ###use eval('0xff') to get int. how to get that to '\xff'? #print "Capture took " + str(time()-startTime) + " sec" #print 'Wrote BMP to ' + str(os.path.abspath(os.curdir)) + str(bmpfile) ser.close() if( filename[:2] == 'tn'): binDummy('../images/tn/' + filename) else: binDummy('../images/' + filename) else: print 'Serial device is not responding' ser.close()