# main.py # This file is part of Android FTDI Serial # # Copyright (C) 2011 - Manuel Di Cerbo, Nexus-Computing GmbH # # Android FTDI Serial is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # Android FTDI Serial is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with Android FTDI Serial; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, # Boston, MA 02110-1301 USA # # # Thanks to the libftdi project http://www.intra2net.com/en/developer/libftdi/ # #! /usr/bin/env python import usb import thread def setup(): busses = usb.busses() handle = 0 for bus in busses: devices = bus.devices for dev in devices: if dev.idVendor == 0x0403: handle = dev.open() handle.setConfiguration(dev.configurations[0]) handle.claimInterface(dev.configurations[0].interfaces[0][0]) handle.controlMsg(requestType = 0x40, request = 0, value = 0, index = 0, buffer = 0, timeout = 0)#reset handle.controlMsg(requestType = 0x40, request = 0, value = 1, index = 0, buffer = 0, timeout = 0)#reset handle.controlMsg(requestType = 0x40, request = 0, value = 2, index = 0, buffer = 0, timeout = 0)#reset handle.controlMsg(requestType = 0x40, request = 0x03, value = 0x4138, index = 0, buffer = 0, timeout = 0)#9600 baudrate return handle def echo(handle): handle.bulkWrite(0x02, "",0)#somehow the first bytes are not returned thread.start_new_thread(readLoop, (handle,)) while True: msg = raw_input("send command: ") handle.bulkWrite(0x02, msg,0) def readLoop(handle): while True: data = handle.bulkRead(0x81, 64,0) if len(data) > 2: print "" print 'out: ' + ''.join([chr(c) for c in data])[2:len(data)] print "" def sendCommand(handle): while True: msg = raw_input("enter command in hex (0xA4): ") try: num = int(msg, 16) print "sending "+hex(num) handle.bulkWrite(0x02, [num,],0) except: print "cannot parse number" if __name__=='__main__': handle = setup() sendCommand(handle) # echo(handle)