python serial communication problem
Moderators: adafruit_support_bill, adafruit

python serial communication problem

by meridia on Thu Jan 07, 2010 4:04 am

I have bought the tweet-a-watt product using xBee module.
And I followed all the instruction posted on the web.
But at the end of sequences, I met a problem.
The problem is that I can not open the serial com port with the python script.
For example, I can not execute the python codes from http://www.ladyada.net/make/tweetawatt/parser.html as shown below.

--------------------------------------
from xbee import xbee
import serial

SERIALPORT = "COM6" # the com/serial port the XBee is connected to
BAUDRATE = 9600 # the baud rate we talk to the xbee

# open up the FTDI serial port to get data transmitted to xbee
ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.open()

while True:
# grab one packet from the xbee, or timeout
packet = xbee.find_packet(ser)
if packet:
xb = xbee(packet)

print xb
-------------------------------------------------------

Whenever running this codes, I can see the following error messages.

------------------------------------------------------------------
Traceback (most recent call last):
File "C:/wattcher/test2.py", line 9, in <module>
ser.open()
File "C:\Python25\Lib\site-packages\serial\serialwin32.py", line 53, in open
raise SerialException("could not open port %s: %s" % (self.portstr, ctypes.WinError()))
SerialException: could not open port COM6: [Error 5] access denied.
---------------------------------------------------------------------

I've downloaded and installed the following programmes.
python-2.5.4.msi
pyserial-2.5-rc1.win32.exe
pywin32-214.win32-py2.5.exe
simplejson-2.0.9

And I assigned the correct COM port #.
Also I did not operate any other application program which can access the same COM port while operating the python codes.

What can I do for solving this problem?
meridia
 
Posts: 3
Joined: Thu Jan 07, 2010 3:42 am

Re: python serial communication problem

by franklin97355 on Thu Jan 07, 2010 8:05 pm

Can you talk to the local xbee module on com6? ( using X-CTU or your favorite terminal.
User avatar
franklin97355
 
Posts: 5889
Joined: Mon Apr 21, 2008 2:33 pm

Re: python serial communication problem

by meridia on Thu Jan 07, 2010 8:19 pm

Yes, I can talk with my xBee module through COM6 using X-CTU or HyperTerminal.
And on terminal window, I can see the received data from the xBee module.
Also I can execute the EnergyLogger application program that can be obtained from http://www.miloslick.com/EnergyLogger.html without any problems.
meridia
 
Posts: 3
Joined: Thu Jan 07, 2010 3:42 am

Re: python serial communication problem

by franklin97355 on Thu Jan 07, 2010 10:52 pm

Then it's got to be in the code.
User avatar
franklin97355
 
Posts: 5889
Joined: Mon Apr 21, 2008 2:33 pm

Re: python serial communication problem

by meridia on Fri Jan 08, 2010 4:36 am

Yes, indeed.
I found the reason why the python script doesn't work.
In the codes as I posted before, the following line should be modified as follows.

-------------------------------------------------------------
ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.open()

-->

ser = serial.Serial(SERIALPORT, BAUDRATE)
#ser.open()
--------------------------------------------------------------

ser.open() makes the access denying error becuse ser = serial.Serial(SERIALPORT, BAUDRATE) already open the port.
meridia
 
Posts: 3
Joined: Thu Jan 07, 2010 3:42 am

Re: python serial communication problem

by adafruit on Fri Jan 08, 2010 5:08 pm

? we dont think so. are you sure you didnt leave XCTU or some terminal program open? the code should not be modified, please undo your 'fix'!
User avatar
adafruit
 
Posts: 11740
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: python serial communication problem

by ProfNimrod on Sun Jan 17, 2010 3:31 pm

Did you ever fix this problem? I have the same issue (Vista). I can access my COM5 in a variety of ways successfully, and even view the incoming packets, but the python script always gives me the same error you see... even in interactive mode I cannot open COM5 without the access denied error.

Thanks, Kurt
ProfNimrod
 
Posts: 9
Joined: Tue Jan 12, 2010 11:30 pm

Re: python serial communication problem

by mwr on Sun Jan 17, 2010 8:00 pm

Long shot, and my memory could be completely wrong, but I think I ran into this on another pyserial-using program. I *think* the problem was resolved by installing the Visual C++ 2008 redistributable package.

http://www.microsoft.com/downloads/deta ... 4b9f2bc1bf
mwr
 
Posts: 47
Joined: Sun Jun 07, 2009 3:08 pm

Re: python serial communication problem

by adafruit on Mon Jan 18, 2010 9:06 pm

! please try it guys, and let us know
User avatar
adafruit
 
Posts: 11740
Joined: Thu Apr 06, 2006 4:21 pm
Location: nyc

Re: python serial communication problem

by ProfNimrod on Tue Jan 19, 2010 9:00 pm

Hi all

I added the Visual C++ 2008 redistributable package and this had no effect - I would've been very surprised if it did.

After some playing around I realized that my usual approach of installing the latest version of everything does not work. I was using Python 2.6, with the latest version of pyserial which I believe has not been updated since version 2.5, and I was using yet a different version of pywin32 (another essential element when using pyserial in windows). So I decided to go back to Python 2.4. After uninstalling all my version, I installed Python 2.4.4, I installed pyserial-2.4.win32 and then pywin32-214.win32-py2.4. (Files all in the attached zip file).

A quick interactive test proved that serial communication with my XBee using Python now works... hooray!

A simple interactive test once you have everything above installed:

python
>>> import serial
>>> ser = serial.Serial("COM5",9600) (adjust "COM5" as needed)
>>> ser.open()
>>> data = ser.readline() (you may have to wait some time for the response)
>>> data
'~\x00T\x83\x00\x01F\x00\x13"\x00\x01\xe2\x01\xe8\x02\xb9\x01\xea\x03%\x01\xe8\x03Z\x01\xe8\x03[\x01\xe8\x02\xf9\x01\xe8\x02j\x01\xe8\x01\xc6\x01\xe8\x01\x12\x01\xe8\x00\x9b\x01\xe8\x00z\x01\xe8\x00\x87\x01\xe8\x00\xfe\x01\xe8\x01\x97\x01\xe8\x02K\x01\xe8\x02\xf3\x01\xe9\x03O\x01\xe9\x03`\x01\xe8\x037\x01\xe9\x8b~\x00T\x83\x00\x01E\x00\x13"\x00\x03\x1a\x01\xe5\x02O\x01\xe9\x01\xaa\x01\xe8\x01\x0b\x01\xe8\x00\xa3\x01\xe8\x00z\x01\xe8\x00\x9c\x01\xe8\x01\x19\x01\xe9\x01\xb8\x01\xe8\x02U\x01\xe8\x02\xeb\x01.... blah blah blah.

Next I will try the same with version 2.5 of all the above...

Hope this is helpful to some.

Kurt
ProfNimrod
 
Posts: 9
Joined: Tue Jan 12, 2010 11:30 pm

Re: python serial communication problem

by ProfNimrod on Tue Jan 19, 2010 10:44 pm

No luck with:

python-2.5.4
pyserial-2.5-rc2.win32
pywin32-214.win32-py2.5
ProfNimrod
 
Posts: 9
Joined: Tue Jan 12, 2010 11:30 pm

Re: python serial communication problem

by Armghan on Tue Feb 02, 2010 11:08 am

Windows thinks that the com port is in use by another program so it denies access to the port.
If you add ser.close() just before ser.open() line, that should take care of the problem.
Armghan
 
Posts: 11
Joined: Mon Jan 25, 2010 11:41 am

Re: python serial communication problem

by ProfNimrod on Tue Feb 02, 2010 12:27 pm

What I also noticed was that although I had trouble with the combination of Python 2.6, the latest versions of pywin32 and pyserial in Vista, I had no such problem in XP... and unfortunately ser.close() before ser.open() did not work for me in Vista.
ProfNimrod
 
Posts: 9
Joined: Tue Jan 12, 2010 11:30 pm

Re: python serial communication problem

by Armghan on Tue Feb 02, 2010 1:58 pm

Funny, I had the same problem in Windows 7 and XP :)
Armghan
 
Posts: 11
Joined: Mon Jan 25, 2010 11:41 am

Re: python serial communication problem

by mwarre25 on Sat Feb 05, 2011 1:38 pm

Yes, indeed.
I found the reason why the python script doesn't work.
In the codes as I posted before, the following line should be modified as follows.

-------------------------------------------------------------
ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.open()

-->

ser = serial.Serial(SERIALPORT, BAUDRATE)
#ser.open()
--------------------------------------------------------------

ser.open() makes the access denying error becuse ser = serial.Serial(SERIALPORT, BAUDRATE) already open the port.



This worked for me as well.

I don't know if this matters but just for anybody that may need it, I am using VMWare on a macbook pro
mwarre25
 
Posts: 1
Joined: Mon Jan 17, 2011 11:39 pm