4

I'm trying to use PySerial to accept inputs from an RFID Reader. As per the answers here: I've tried using WinObj and found something odd: there is no COM3 port in the GLOBAL??? folder pointing to something "more driver specific." However, when I run the command python -m serial.tools.list_ports, it does throw up COM3. When I try a simple program like:

import serial
ser = serial.Serial()
ser.port = 2
print(ser)
ser.open()

I get the following output:

Serial<id=0x45e8198, open=False>(port='COM3', baudrate=9600, bytesize=8, parity='N', stopbits=1, timeout=None, xonxoff=False, rtscts=False, dsrdtr=False)
serial.serialutil.SerialException: could not open port 'COM3': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)

So, I know that PySerial is looking for my reader in the right place, and, according to two different sources (the device manager and the command line), the device is registering. And yet I'm still getting this error. What is going on? I'm using Python 3.3 on Windows 8.1.

EDIT: That error is actually what I get from python's command line. The one I get from making and running a program like the one above is:

AttributeError: 'function' object has no attribute 'Serial.'

I'd appreciate thoughts on that too.

DrCord
  • 3,917
  • 3
  • 34
  • 47
AirAnAria
  • 41
  • 1
  • 1
  • 3

2 Answers2

2

First thing I would check is what you have for com ports attached and what's currently in use:

import serial.tools.list_ports
import sys

list = serial.tools.list_ports.comports()
connected = []
for element in list:
    connected.append(element.device)
print("Connected COM ports: " + str(connected))
# compliments of https://stackoverflow.com/questions/12090503/listing-available-com-ports-with-python#14224477
""" Lists serial port names

    :raises EnvironmentError:
        On unsupported or unknown platforms
    :returns:
        A list of the serial ports available on the system
"""
if sys.platform.startswith('win'):
# !attention assumes pyserial 3.x
    ports = ['COM%s' % (i + 1) for i in range(256)]
elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'):
    # this excludes your current terminal "/dev/tty"
    ports = glob.glob('/dev/tty[A-Za-z]*')
elif sys.platform.startswith('darwin'):
    ports = glob.glob('/dev/tty.*')
else:
    raise EnvironmentError('Unsupported platform')

result = []
for port in ports:
    try:
        s = serial.Serial(port)
        s.close()
        result.append(port)
    except (OSError, serial.SerialException):
        pass
print("Availible COM Ports: " + str(result))

Then, make sure you are calling the serial port constructor with the parameters you want:

ser = serial.Serial(
    port="com2", # assumes pyserial 3.x, for 2.x use integer values
    baudrate=19200,
    bytesize=8,
    parity="E",  # options are: {N,E,O,S,M}
    stopbits=1,
    timeout=0.05)

When you call "serial.Serial()" without any parameters and then add the port ID, I'm not entirely sure what it's going to do, I've always explicitly referenced the port I want to use there.

grambo
  • 283
  • 2
  • 10
0

Your issue lies in the fact that the serial object is looking for a string "COMXX" or else it won't work. I don't know if it need to be capitalized or not.

make sure you configure it like this.

serial.Serial(port = "COM2")