Python error WindowsError(5, 'Access is denied.' - python

I am new to python and am trying to run a prewritten program but keep recieving an error here is the whole error.
Traceback (most recent call last):
File "C:\source\python\pill.py", line 29, in <module>
ser.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 66, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
SerialException: could not open port 'COM7': WindowsError(5, 'Access is denied.')
I am the admin as well

You may have another program using the COM port, for example a test program like firmata_test.exe that our course work told use to use.
One course module told us to run that program, which is a Windows executable that opens the COM port and monitors the various pins on the board and lets us toggle their state High or Low. The next module told us to run some Python code in the IDLE code editor that would connect to the board and write to the pin state to turn an LED on and off. The Python code failed with the Access is denied message, because the COM port was already in use by firmata_test.exe, though the error message did not mention this specifically (because the Python code did not know why it could not open the port, only that it could not).
Close any other software such as firmata_test.exe that may be using the COM port and run your Python code again.

Related

How to detect if a device on COM port is hanged?

I have a temperature measurement device with which i can communicate using pyserial module on COM port. I can read and write from and to the device from USB interface.
Now the device hangs sometimes and i can no longer read or write values to the device. The python script always hangs on the following initialization function
serial.Serial(port='COM13', baudrate=9600)
and shows no response until i have to kill the cmd terminal. The traceback after killing the terminal is as follows.
Traceback (most recent call last):
File "C:\scripts\test.py", line 4, in <module>
serial.Serial('COM5', baudrate=9600)
File "C:\Users\User1\AppData\Local\Programs\Python\Python310\lib\site-packages\serial\serialwin32.py", line 33, in __init__
super(Serial, self).__init__(*args, **kwargs)
File "C:\Users\User1\AppData\Local\Programs\Python\Python310\lib\site-packages\serial\serialutil.py", line 244, in __init__
self.open()
File "C:\Users\User1\AppData\Local\Programs\Python\Python310\lib\site-packages\serial\serialwin32.py", line 54, in open
self._port_handle = win32.CreateFile(
KeyboardInterrupt
In my opinion a hanged device is like a powered off device. I only want to raise exception if it cannot initialize the connection after 3 seconds. I have read about timeout, but it also creates no effect.
Freezing inside the serial constructor means that something is wrong on the usb-to-serial level, while the serial backend might still function properly. Problem with driver or hardware can't have a general solution.
Some thoughts:
Terminating the process, while a read operation is ongoing may leave the serial port handle in an open state, preventing further connections. Thus, using the read timeout is a must.
serial.Serial("COM3", 115200, timeout=1)
Sometimes converter may stall inside the driver, rendering timeouts useless, and only disconnecting the deivce would help. We've encountered this behavior with cheap chineese adapters.
You may switch to non-blocking operations, using in_waiting field, which contains the number of bytes in the receive buffer. Calling read() with this value will cause it to return almost immediately. Of course you'll have to implement manual message assembly.

Python: File Can't be Found (pySerial)

Having a small problem with opening a serial/console port via pySerial.
My program is meant to get the active com port, open a console connection and then send data. When the program is running and I plug in my RS232 USB I receive a SerialException error. (More specifically, "Could not open port: FileNotFoundError")
In the event where the program is run, it will keep printing "No RS232 Connected", but when the RS232 USB is connected the program breaks and runs into the SerialException error.
If I plug in the RS232 USB before running the program and then run it, it reads and performs normal operation without issue.
ports = serial.tools.list_ports.comports(include_links=False)
if not ports:
print("No RS232 Connected")
if ports:
for port in ports:
print('Found port ' + port.device)
ser = serial.Serial(port.device)
if ser.isOpen():
ser.close()
break
console = serial.Serial(port.device, baudrate=9600, parity="N", stopbits=1, bytesize=8, timeout=0.4)
I am quite new to Python and programming in general, but I feel that the problem may be around the 'ports' list already being populated twice due to the while True loop. Then when we go to create the console by opening the port we are expecting one entry in the list, but there are two.
Since we can't have 2 open console connections on the same COM port, we receive the error.
If I print the 'ports' list I get this.
"[<serial.tools.list_ports_common.ListPortInfo object at 0x000002B5D77F0D68>]
[<serial.tools.list_ports_common.ListPortInfo object at 0x000002B5D77F0D68>]"
Any help would be greatly appreciated! Please let me know if you require any more details.
Thanks,
After further research into it, I have realised that when a RS232 USB is plugged into the PC, we need to give it a bit of time to open a stream. It sounds like it is opened when the temp file is created for it.
Although it was identifying that a COM port was available almost immediately, it was not ready by the time I was trying to create the Serial instance, which was why I was getting the FileNotFound error.
A simple sleep function of half a second has solved the issue!

pyaudio OSError: [Errno - 9999] Unanticipated host error

I'm trying to use the microphone of my webcam logitech to listen
with sr.Microphone(sample_rate=32000) as source:
...
This error occurs :
File "/home/pi/project2/interface.py", line 226, in listen
with sr.Microphone(sample_rate = 32000) as source:
File
"/usr/local/lib/python3.4/dist-packages/speech_recognition/init.py",
line 140, in enter
input=True, # stream is an input stream File "/usr/local/lib/python3.4/dist-packages/PyAudio-0.2.10-py3.4-linux-armv7l.egg/pyaudio.py",
line 750, in open
stream = Stream(self, *args, **kwargs) File "/usr/local/lib/python3.4/dist-packages/PyAudio-0.2.10-py3.4-linux-armv7l.egg/pyaudio.py",
line 441, in init
self._stream = pa.open(**arguments) OSError: [Errno -9999] Unanticipated host error
I didn't find any fitting information on the web
You may check your anti-virus SW.
I got the same problem "OSError: [Errno - 9999] Unanticipated host error" every time I tried to record the wave file by pyAudio.
I check and confirm all the parameters OK.
I was stuck for several days before I noticed the anti-virus on my PC block my python script call to the hardware level.
All the thing I need to do is disable or exit the anti-virus SW and everything work well again.
I got this error while working with python threads.
I was creating 2 pyaudio objects for different purposes and hadn't closed one of them later. This gave me the error:
[Errno - 9999] Unanticipated host error
Closing that thread fixed my issue.
It may be a problem with microphone permission for your app make sure you have enabled the microphone permission for all apps in settings. because have developed one Tkinter project in which I am using pyaudio for recording purpose. On my pc the recorder working fine but not on the client machine. after investigation and running the same script on client machin it gives the same error as you mention. but after giving permission of a microphone for my project it working fine.
pyaudio throws this error because the python script not able to use the mic hardware.
I hope you will get some clarity with this and my answer may help you.
Windows user here. For me, I just had to change the default microphone which was being changed every time I connected my headphones.
This might also happen if there is no microphone access to the program or the microphone is not working properly. To check if the microphone is working properly, try recording some audio in voice recorder, and see if the selected microphone is the default audio recording device.

Com Port Permissions Python

Working on trying to get serial comminications working between an arduino and my computer. When working off the code that can be seen here I keep getting this error:
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM4': WindowsError(5, 'Access is denied.')
I have edited the com port used in the aformentioned code to match what I am actually using, but I cant get this error to go away. Here are a few thinks I've tried:
Ran file as administrator
Started pycharm as administrator
Changed the security properties for the directory the program is in
Unistalled and reinstalled the COM port in device manager
Restarted Computer
The weird thing is sometimes after I do these fixes it will work once, and then when I stop the program and start it again it once again throws the error.
Ran into the same problem before and I realized that I left the serial connection open after serial read program execution completed. Closing the serial connection once done should work.
import serial
serial = serial.Serial('COM124', baudrate=115200, timeout=1)
#Main function to do something
my_function():
...
#end of Main function
my_function()
#adding below line will solve your problem
serial.close()

Having errors working with com ports using pyserial

I am trying to interface with my com ports, specifically an XBee connected to thi using this code.
from xbee import XBee
from serial import Serial
PORT = 'COM3'
BAUD = 9600
ser = Serial(PORT, BAUD)
xbee = XBee(ser)
# Send the string 'Hello World' to the module with MY set to 1
xbee.tx(dest_addr='\x00\x01', data='Hello World')
# Wait for and get the response
print(xbee.wait_read_frame())
ser.close()
However, this error keeps arising.
SerialException: could not open port 'COM3': WindowsError(5, 'Access is denied.'). It goes away when I restart my computer, buts it keeps returning. I'd prefer to understand why its happening so I don't need to keep restarting my computer. Would really appreciate any help, thanks. I am working through the IDLE interface with python 2.7 just in case that is relevant.
A serial port can be "open" in only one application at a time. Once application "A" opens the port, application "B" will get an Access Denied error when it tries to open the same port. In your case, you need to figure out what other application is holding the port and close it first.

Categories

Resources