3
import serial
while True:
    ser=serial.Serial(port='COM30',baudrate=9600)
    print "try"
    s=ser.read(100) #reading up to 100 bytes
    print s
ser.close()

Device Manager:

enter image description here
I am trying to read data from port using python. But it show the error:

Traceback (most recent call last):
  File "new_python.py", line 3, in <module>
    ser=serial.Serial(port='COM30',baudrate=9600)
  File "C:\Anaconda\lib\site-packages\serial\serialwin32.py", line 38, in __init__
    SerialBase.__init__(self, *args, **kwargs)
  File "C:\Anaconda\lib\site-packages\serial\serialutil.py", line 282, in __init__
    self.open()
  File "C:\Anaconda\lib\site-packages\serial\serialwin32.py", line 66, in open
    raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM30': WindowsError(5, 'Access is denied.')

***Repl Closed***

Now from previous solution on stack I am tried using python 32 bit and call it from a cmd with admin priviledges but same error!

When I try it matlab it shows me this:

s = serial('COM30')

   Serial Port Object : Serial-COM30

   Communication Settings 
      Port:               COM30
      BaudRate:           9600
      Terminator:         'LF'

   Communication State 
      Status:             closed
      RecordStatus:       off

   Read/Write State  
      TransferStatus:     idle
      BytesAvailable:     0
      ValuesReceived:     0
      ValuesSent:         0
Abhishek Bhatia
  • 9,404
  • 26
  • 87
  • 142

2 Answers2

0

Look like you haven't opened the serial port

ser=serial.Serial(port='COM30',baudrate=9600)
ser.open()

Also. What happens if you remove the loop?

Bridgekeeper
  • 354
  • 2
  • 12
  • 1
    According to [this answer](http://stackoverflow.com/a/6032694/3087339) that's not necessary. I wonder if there's an issue that each loop will try to open the port when it hasn't been closed after the previous loop. – elParaguayo Nov 13 '15 at 12:25
  • @elParaguayo OK i did not know that. – Bridgekeeper Nov 13 '15 at 12:28
  • @AbhishekBhatia Serial connections can be a hazzle. It seems to me that you try to open the port several times and you may have to kill the process that speaks with the com-port. I guess it is easiest to restart windows. Also you can try to 'ser.flushInput()' and 'ser.flushOutput()' – Bridgekeeper Nov 13 '15 at 12:33
0
import serial
import time
ser=serial.Serial(port='COM30',baudrate=9600)
ser.open()
while True:
    print "try"
    time.sleep(10)
    s=ser.read(100) #reading up to 100 bytes
    print s
ser.close()

Can you try this?

drscaon
  • 401
  • 2
  • 9
  • 'Access is denied.' : Did you check if some other service si running and locked this port? – drscaon Nov 13 '15 at 12:29
  • 1
    Disable the COM port in the device manager window and enable it again, or use Portmon (https://technet.microsoft.com/en-us/sysinternals/bb896644) – drscaon Nov 13 '15 at 12:32