Shop OBEX P1 Docs P2 Docs Learn Events
how to identify a com port in python? — Parallax Forums

how to identify a com port in python?

chzchz Posts: 8
edited 2006-04-27 07:23 in BASIC Stamp
i'm using python to communicate with the basic stamp and am successful with it, but I am trying to detect which com port the stamp is located at. how would you identify this?

what i've got currently is that it will run through a loop of different device ports, and then when it establishes a connection, it will complete the function call. the only problem is that it will complete on any com port that is established on the system whether its the bs2 or some other device.

even if you dont know python, some other language or example will help. i thank you all in advance. here is what i have for the python code.

def connect(self):
    device = [noparse][[/noparse]'/dev/tty.usbserial-1B1','COM1','COM2',    #mac, win, win
            'COM3','COM4','/dev/tty.usbserial-3B1']   #win,win,mac
    i = 0
    for dev in device:
        try:
            self.activeSerial = dev
            i=i+1
            print str(i) + " " + dev
            self.serConn = self.openSerial(dev)
            if(self.serConn):
                print "self.serConn", self.serConn
                break
        except serial.serialutil.SerialException:
            self.activeSerial = None
            self.serConn = None
    print "activeSerial", self.activeSerial

Comments

  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2006-04-27 03:18
    Well, you have two options for detection prior to actually trying to talk to the Stamp Module.· One is to look for loopback.· If you're using a serial port and a board configured as we have them then you should be able to detect the loopback connection.· Your second option is to send data to each port and listen for an echo of what you sent.· A powered BASIC Stamp will echo what is sent to it.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
    csavage@parallax.com
  • chzchz Posts: 8
    edited 2006-04-27 07:23
    i ended up using the echo which was acting odd at first. i attempted to write "wel" and have the bs debug "come" so when i read back into a variable like x, it should be = "welcome", but for some reason it was outputting "comewel". after tinkering around with it, it fixed itself and is now saying welcome. i don't know if it has anything to do with reading in through the python prompt vs a python script, but the script seems to be doing just fine for me.

    thanks for the advice. smilewinkgrin.gif
Sign In or Register to comment.