Page 1 of 1

Sounds Life RFID

Posted: Sun Jan 25, 2015 8:29 am
by qscience
My son has been having trouble with the "showtags.py" part of this project. He keeps getting syntax errors in python. He is using Windows 8.
The instructions shows the following:

thread = threading.Thread(target=read_from_port, args=(serial_port,))
thread.start()

He has replaced "port" with our serial port, COM3. Can anyone please help, this project is due tomorrow and he has spent hours trying to make this work.

Thank You!

Re: Sounds Life RFID

Posted: Sun Jan 25, 2015 9:08 am
by deleted-249560
Accessing serial ports is a nuisance in Windows. You might find this thread useful: http://stackoverflow.com/questions/2063 ... ess-denied

I just tried the reader on a Windows 8 laptop, using "port = '\\.\COM3'" to specify the port and it worked fine. Here's the copy of showtags.py as presented on the website, but with the OSX serial port commented out and replaced by a Windows COM3 port:

Code: Select all

import serial
import threading
import time
import sys

# The name of the actual serial port will depend on the operating system
# and your computer's configuration. The easiet way to find it is to try
# read the tags in a serial terminal emulation program (as discussed in
# the project description)
# port = '/dev/cu.usbserial-AH012QEW'
port = '\\.\COM3'

# This is the data rate of the RFID reader
baud = 2400

# Open a connection to the serial port using the information above
serial_port = serial.Serial(port, baud, timeout=0)

# This is a stub of a 'data handler' that will get called by the rest of the 
# program when a complete tag comes in from the reader
def handle_data(data):
      print("data=",data)
      # This is where you would use the data for something.
	  # Right now it simply shows it on screen.

# This is a short program that reads data coming from the RFID reader, one
# character at a time. The data coming in is 10 chararcters of useful information
# followed by 2 characters that indicate the end of the tag. It takes each incoming
# character and decides if it's a character a human can read or if it's one of the special
# end-of-tag characters. When it thinks it's done with the tag, it calls the 'handle_data()'
# progam we defined just above.
def read_from_port(ser):
	  # Let's start with an empty storage area for the incoming data
      new_data = ""
      while True:
			# Read in one character
            reading = ser.read(1)
			# If we got one, the 'length' will be 1. If not, we have nothing to look at
            if len(reading) > 0:
				  # A standard CR character (ASCII 13, or 0x0D) tells us we have all the data
                  if b'\r' == reading:
						# If the data is complete, call our data handler to do something with it
                        handle_data(new_data)
						# clear the storage area and get ready for new data
                        new_data = ""
                  elif reading >= b'0' and reading <= b'F':
						# The character is human readable and will be in the range 0-9 or A-F,
						# so stick it on the end of the storage area to build up the incoming data string
                        new_data = new_data + reading.decode()
 
# read_from_port() is intended to run in the background as a separate thread
thread = threading.Thread(target=read_from_port, args=(serial_port,))
thread.start()

# With the data collection thread running, we can go do something else.
print ("Go ahead and wave a tag near the reader.")

# This test program has nothing else to do. Yours might and this is where you'd
# do that.
while True:
      # wait here forever. You have to have something in the loop so
	  # we'll just keep assigning the value 0 to the variable a. It's
	  # wasting time while the other thread does all the work.
	  # You could have the program do something else if you wanted to.
      a = 0
I wish your son luck with this, and if you'd be willing to share a video of his project, we'd love to hear from you and post a video. The one I included in the project description really needs a student and not an adult.

Howard

Re: Sounds Life RFID

Posted: Mon Feb 23, 2015 2:47 pm
by deleted-249560
As it happens, I now have a Windows 8.1 machine myself and I tried the code as you posted. After I changed COM4 to COM3 to match my serial port assignment, it worked fine.

Two questions:

1) If you go into the control panel and look at System, Device Manager, Ports - you should see your newly added COM port. Are you sure it's COM4? Whatever it shows up as there, try that in Putty and if that works, make sure you use the same in Python.

2) Are you an administrator on the computer? Maybe that makes a difference. I only have one login on this one so I don't know if having administrator access matters or not.

In your code you specified all the parameters in the Serial() call which isn't entirely necessary. You said:
port = '\\.\COM4'
baud = 2400
serial_port = serial.Serial(port='\\.\COM4', baudrate=2400, bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, timeout=1)

and then you rewrote 'port' a second time and ignored 'baud'. You can leave out the first two statements or simply use:

serial_port = serial.Serial(port, baud, timeout=0)

which uses the values you just set and then uses the defaults for the other values. It's not causing your problen but it could confuse someone reading it.

Howard

Re: Sounds Life RFID

Posted: Mon Feb 23, 2015 3:06 pm
by deleted-249560
Okay, three questions:

3) Did you exit out of and close PuTTy first? You have to do that.

Re: Sounds Life RFID

Posted: Tue Mar 03, 2015 11:06 am
by deleted-274944
This Code doesnt work .It always shows me Access denied !!

Re: Sounds Life RFID

Posted: Tue Mar 03, 2015 8:10 pm
by deleted-249560
Pyserial seems to be a tricky thing sometimes. If you search for 'python 3 pyserial "access denied"' you'll find lots and lots of queries about this, and just about as many solutions.

http://forum.arduino.cc/index.php?topic=102375.0 has someone trying to use Python to connect to an Arduino. That thread says to use 'port = "com3"' instead of 'port = "\\.\com3"'. On my Windows 8 machine I find the second form works better.

http://stackoverflow.com/questions/2063 ... ess-denied is a thread that explains when you need the extra slashes.

http://stackoverflow.com/questions/6032 ... ython-win7 discusses a situation fixed by elevating the access level of python.exe to fix the problem.

Have you searched around and tried any of these solutions? Once you get Pyserial working correctly you shouldn't have any more problems.

Howard