#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Location
    Atlanta
    Posts
    5
    Rep Power
    0

    Help! My serial IO exception handler not working


    I am trying to use the following code to enable me to catch when another program is using the serial port, and pop-up a standard error window:

    Code:
    def OpenSerial():
        global ser
        try:
            ser = serial.Serial()
        except:
            tkMessageBox.showerror("Serial Port Error", "Could not get serial port")
        else:
            if ser.isOpen():
                ser.close()
            ser.baudrate = 9600
            ser.port = 'COM1'
            ser.open()
            ser.setDTR()
            #self.StartThread()
            tkMessageBox.showinfo("Serial Port Status","COM on %s [%s, %s%s%s%s%s]" % (
                ser.portstr,
                ser.baudrate,
                ser.bytesize,
                ser.parity,
                ser.stopbits,
                ser.rtscts and ' RTS/CTS' or '',
                ser.xonxoff and ' Xon/Xoff' or '',
                )
            )
    The part showing a succesfully opened COM port works like I want it to (the "else" portion). Anyone have any ideas as to what I'm doing wrong?

    I am using Python 2.3 and Pyserial 2.1
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    You will only get an exception when you try to open/read/write the port.

    Until you attempt to grab the port with the open method the serial module cannot know which port you will be communicationg with.

    grim

IMN logo majestic logo threadwatch logo seochat tools logo