Can't open Serial port Python - python

I have a problem with opening a serial USB port in windows 7 over my Python code.
TeraTerm and Docklight are able to open port without any problems.
When I try to open the port it comes up with the following error
SerialException: Cannot configure port, something went wrong. Original message: WindowsError(31, 'A device attached to the system is not functioning.')
I need to know if I am using the right serial lib. Maybe there is one that can open it without messing with the OS.
the following code returns the error above
ser = serial.Serial()
ser.port = 'COM5'
ser.open()
The entire message is as follows:
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2883, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-13-882203061f11>", line 4, in <module>
ser.open()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 78, in open
self._reconfigure_port()
File "C:\Python27\lib\site-packages\serial\serialwin32.py", line 220, in _reconfigure_port
raise SerialException("Cannot configure port, something went wrong. Original message: %r" % ctypes.WinError())
SerialException: Cannot configure port, something went wrong. Original message: WindowsError(31, 'A device attached to the system is not functioning.')
Thanks for support

This is not pyserial fault, what this lib do is act as 'bridge' between your python code and windows native code from kernel32 lib using ctypes.
My guess is that your fimware or device driver is faulty but well... WINAPI Serial api is a little buggy as well anyway.

Related

/dev/rfcommo not giving any data when reading OBD data in raspberry pi via bluetooth

I am trying to connect my raspberry pi to a Bluetooth OBD adapter. I have verified that the OBD device is working by connecting to it from the android phone using Torque lite app.
I am following this tutorial to connect the raspberry pi to the OBD adapter. I am able to pair with the OBD adapter and then trust it. Then I manually create the "/dev/rfcomm0" abstraction. As per the tutorial "screen /dev/rfcomm0" is supposed to print some output. But for me it just says "Screen is terminating" and it exits.
I tried running the "obd_reader.py" script, but it is throwing the following error:
python obd_reader.py
Ports:
['/dev/rfcomm0']
[obd.obd] ======================= python-OBD (v0.6.1) =======================
[obd.obd] Explicit port defined
[obd.elm327] Initializing ELM327: PORT=/dev/rfcomm0 BAUD=auto PROTOCOL=auto
Traceback (most recent call last):
File "obd_reader.py", line 11, in <module>
connection = obd.OBD(ports[0])
File "/usr/local/lib/python2.7/dist-packages/obd/obd.py", line 58, in __init__
self.__connect(portstr, baudrate, protocol) # initialize by connecting and loading sensors
File "/usr/local/lib/python2.7/dist-packages/obd/obd.py", line 85, in __connect
self.interface = ELM327(portstr, baudrate, protocol)
File "/usr/local/lib/python2.7/dist-packages/obd/elm327.py", line 137, in __init__
if not self.set_baudrate(baudrate):
File "/usr/local/lib/python2.7/dist-packages/obd/elm327.py", line 265, in set_baudrate
return self.auto_baudrate()
File "/usr/local/lib/python2.7/dist-packages/obd/elm327.py", line 293, in auto_baudrate
response = self.__port.read(1024)
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 460, in read
raise SerialException('device reports readiness to read but returned no data (device disconnected?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)
I tried a suggestion provided here, but there was no change in output. This was the change suggested:
You need to add the SP profile editing this:
sudo nano /etc/systemd/system/dbus-org.bluez.service
Then add the compatibility flag -C for adding the SP to sdptool. Modify the lines from the file above to:
ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP
I feel that I am creating or configuring the rfcomm device in the wrong way. But not sure how to fix this. Can someone please suggest a solution?
It seems that line 27 of the script you are running you are missing a dot in the query:
res = connection.query(obd.commands[command])
Tt should be like this as far as I know:
res= connection.query(obd.commands.[command])

python3.5: serial port reading gives error

While reading serial port from COM4 port using python, i get the following error:
----------------------------------------------------------------------------
F:\Invsense\motion_driver_6.12\eMPL-pythonclient>python.exe eMPL-client.py 4
Traceback (most recent call last):
File "eMPL-client.py", line 543, in <module>
data_delegate = data)
File "eMPL-client.py", line 23, in __init__
self.s = serial.Serial(port,115200)
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\site-
packages\pyserial-3.1.1-py3.5.egg\serial\serialwin32.py", line 31, in __init__
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\site-
packages\pyserial-3.1.1-py3.5.egg\serial\serialutil.py", line 162, in init
File "C:\Users\admin\AppData\Local\Programs\Python\Python35-32\lib\site-
packages\pyserial-3.1.1-py3.5.egg\serial\serialutil.py", line 206, in port
ValueError: "port" must be None or a string, not
F:\Invsense\motion_driver_6.12\eMPL-pythonclient>
This error is coming from serialutil.py. Can you please help me to resolve this problem. I checked COM4 port seperately using putty tool and it worked fine.
Regards
Vinay
The problem is with the Invensense python script.
Open eMPL-client.py in text editor and search for the line: comport = int(sys.argv[1]) - 1
change it to: comport = sys.argv[1]
The script was trying to cast the string 'COM4' into an integer before passing it to the serial function, which wants a string anyway. I think maybe they were using an old version of pyserial because there are some other errors in the script.
Comment out (or delete) these lines:
self.s.setTimeout(0.1)
self.s.setWriteTimeout(0.2)
Run the client with python eMPL-client.py COM4
After all this my python client is showing the MPU data correctly. I hope this isn't too late!

Reading and writing data from same serial port using pyserial

So I'm working on a program to simulate a data terminal. The program uses GUI and is required to read the data from the COM port and write to it as well. I've used the pyserial library and the read part of the program is correctly implemented.
However, When I attempt to write using the same port, an exception is raised by the library saying the port is not accessible. It should be possible to read and write using the same COM port since it's full duplex, so the problem must lie in the way I'm approaching it. Can anyone shed any light as to how, I can enable the write part of the program as well.
Edit: Here is the traceback of the exception:
Traceback (most recent call last):
File "C:\Ankit\python_27\lib\threading.py", line 801, in __bootstrap_inner
self.run()
File "C:\Ankit\SerWatchCode\serialcomm.py", line 85, in run
self.ser.open()
File "C:\Ankit\python_27\lib\site-packages\serial\serialwin32.py", line 62, in open
raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
SerialException: could not open port 'COM2': WindowsError(5, 'Access is denied.')

Running Eclipse with PyDev as root

i need to debug a program i wrote in Eclipse with PyDev plugin. However I can not debug it because Eclipse obviously does not have the permission to do so.
I always get the following error when debugging reaches the send() function.
socket.error: [Errno 1] Operation not permitted
Running Eclipse with gksudo does not work either. It opens, but it says the editor could not be found.
Could not open the editor: No editor descriptor for id
org.python.pydev.editor.PythonEditor
How do I fix this?
I can't post the full code, it's too long. Here is a kind of breakdown, which causes the same error. Eclipse is not allowed the execute the send() function. Executing it with sudo in the command shell works.
from scapy.all import *
from scapy.layers.inet import IP, UDP
class SomeIP(Packet):
name = "SomeIP Packet"
fields_desc=[XShortField("X", 0x1000),
XShortField("Y", 0x1000)]
SIP = SomeIP()
packet = IP(src="129.168.101.164",dst="192.168.101.143")/UDP(sport=1000, dport=1000)/SIP
send(packet, verbose=False)
Here is the error I get - the first line can be ignored.
WARNING: No route found for IPv6 destination :: (no default route?)
Traceback (most recent call last):
File "scapy_test.py", line 19, in <module>
send(packet, verbose=False)
File "/usr/lib/python2.7/dist-packages/scapy/sendrecv.py", line 251, in send
__gen_send(conf.L3socket(*args, **kargs), x, inter=inter, loop=loop, count=count,verbose=verbose, realtime=realtime)
File "/usr/lib/python2.7/dist-packages/scapy/arch/linux.py", line 307, in __init__
self.ins = socket.socket(socket.AF_PACKET, socket.SOCK_RAW, socket.htons(type))
File "/usr/lib/python2.7/socket.py", line 187, in __init__
_sock = _realsocket(family, type, proto)
socket.error: [Errno 1] Operation not permitted
Could you use another port number? If I understand your code correctly you try to open a port on number 1000. Since port numbers up to 1024 can only be used by root, setting the port number to something higher than 1024 could maybe solve the problem.

Python code error message that I don't understand

I am having problems decoding this error message for the python on my Raspberry Pi. Please don't tell me to head to that SE, they sent me here as no one really knew what it means. It is from [GitHub][1]
Here is the error output:
Traceback (most recent call last):
File "/home/pi/piarduino/MissionControl-master/mcDesk.py", line 8, in <module> serialFromArduino = serial.Serial("/dev/serial/by-id/usb-FTDI_FTDI_FT232R_USB_UART_A900aepy-if800-port0", 115200)
File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 260, in __init__
self.open()
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 276, in open
raise SerialException("could not open por %s: %s" % (self._port, msg))
SerialException: could not open port /dev/serial/by-id/usb-FTDI_FTDI_FT232R_USB_UART_A900aepy-if800-port0: [Errno 2] No such file or directory: '/dev/serial/by-id/usb-FTDI_FTDI_FT232R_USB_UART_A900aepy-if800-port0'
Basic troubleshooting:
Try running your script as root using sudo. It's possible you're being denied access to the serial port.
Run ls /dev/serial/by-id in a shell. Make sure the serial port you are trying to open exists and matches exactly with the port you're trying to open in your program.
If neither of those options work, try these steps.
If you care to update your question with more details, we may be able to help you further.

Categories