NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » pyserial on windows 7

May 01, 2011
by kle8309
kle8309's Avatar

I have been having permission problem with windows 7 when I try reading from the serial port.
Similar error was found when I try linux (problem was fixed when I login as root) This is the code

import serial import sys

serial = serial.Serial("COM5", 115200) while True: data = serial.read() if data == 1: print "1" if data == 2: print "2" else: print "0" print "--"

This is the error from the windows python gui (ver 2.6.6)

Traceback (most recent call last): File "C:Python26serialRead.py", line 4, in <module> serial = serial.Serial("COM5", 115200) File "C:Python26libsite-packagesserialserialwin32.py", line 30, in init SerialBase.init(self, args, *kwargs) File "C:Python26libsite-packagesserialserialutil.py", line 260, in init self.open() File "C:Python26libsite-packagesserialserialwin32.py", line 56, in open raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError())) SerialException: could not open port COM5: [Error 5] Access is denied.

May 01, 2011
by kle8309
kle8309's Avatar
import serial
import sys

serial = serial.Serial("COM5", 115200)
while True:
 data = serial.read()
 if data == 1: print "1"
 if data == 2: print "2"
 else: print "0"
 print "--"
May 01, 2011
by kle8309
kle8309's Avatar
 Error:   
 Traceback (most recent call last):
 File "C:\Python26\serialRead.py", line 4, in <module>
 serial = serial.Serial("COM5", 115200)
 File "C:\Python26\lib\site-packages\serial\serialwin32.py", line 30, in __init__
 SerialBase.__init__(self, *args, **kwargs)
 File "C:\Python26\lib\site-packages\serial\serialutil.py", line 260, in __init__
 self.open()
 File "C:\Python26\lib\site-packages\serial\serialwin32.py", line 56, in open
 raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
 SerialException: could not open port COM5: [Error 5] Access is denied.
May 01, 2011
by Ralphxyz
Ralphxyz's Avatar

kle8309, are you sure you are using "COM5" ?

Ralph

May 01, 2011
by kle8309
kle8309's Avatar

Ralph

yes, I checked my devices and it's COM5

This setup worked on the linux but not windows.

May 01, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi kle8309,

Try opening the command line from which you run the python script as Administrator.

Humberto

May 01, 2011
by kle8309
kle8309's Avatar

YES!!! Thank you Humberto, that solved everything.
I did try to mess around with the uac before (by turning the warning off) but I don't know why I didn't think of this before.

I have been spending hours to find an alternative solution toward serial communication like c++ and visual basic (I got visual basic to work)

But python is much easier to play with.

Post a Reply

Please log in to post a reply.

Did you know that a motor's no-load current at a given voltage is much less than it's resistance would suggest? Learn more...