Exeption while listing active serial ports using pyserial - python

i'm a new python learner.
i'm trying to list my active serial ports with this simple code
import serial.tools.list_ports as port_list
ports = list(port_list.main())
for p in ports:
print (p)
this is the reasult
C:\Python27\python.exe C:/Users/tc34669/PycharmProjects/untitled/open_serial_port.py
COM1
COM3
2 ports found
Traceback (most recent call last):
File "C:/Users/tc34669/PycharmProjects/untitled/open_serial_port.py", line 2, in <module>
ports = list(port_list.main())
TypeError: 'NoneType' object is not iterable
Someone here knows how can i list these ports without this TypeError ?
thanks

According to the documentation of pySerial main() isn't actually a documented function you can use to get the info of all the ports. Try using the comports() function instead :
from serial.tools import list_ports
for p in list_ports.comports():
print(p)

If you want to print only the port numbers, (e.g COM1), try using the comport objects 'device' property:
from serial.tools import list_ports
for p in list_ports.comports():
print(p.device)

Related

micropython usocket.IPPROTO_SEC not available

I tried using usocket.IPPROTO_SEC for micropython however it does not seem available.
Is there anything else I should do to get access to usocket.IPPROTO_SEC?
Setup
I use this docker image.
Micropython version: 1.11
Description
The micropython docs say that usocket.IPPROTO_SEC is an available constant, however when I try to access it, it is not there.
The output below shows how I am trying to access it and what are the attributes available inside usocket.
MicroPython v1.11-10-g84f1067f7 on 2019-06-02; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import usocket
>>> usocket.IPPROTO_SEC
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'IPPROTO_SEC'
>>> usocket.
__class__ __name__ AF_INET AF_INET6
AF_UNIX MSG_DONTROUTE MSG_DONTWAIT SOCK_DGRAM
SOCK_RAW SOCK_STREAM SOL_SOCKET SO_BROADCAST
SO_ERROR SO_KEEPALIVE SO_LINGER SO_REUSEADDR
getaddrinfo inet_ntop inet_pton sockaddr
socket

Python cannot import name serial

I'm trying to communicate with my Arduino over serial using Python. I've installed pyserial, and this is my code.
#!/usr/bin/env python
from serial import serial
print("helloworld")
ser=serial.Serial('/dev/ttyACM0',9600)
a=raw_input("enter value")
ser.write(a)
When I try to run the code this is What I get.
Traceback (most recent call last):
File "/home/vm/Desktop/serial.py", line 2, in <module>
from serial import serial
File "/home/vm/Desktop/serial.py", line 2, in <module>
from serial import serial
ImportError: cannot import name serial
You've named your script serial. It's trying to import serial from itself. Rename your script.
or:
import serial
ser = serial.Serial('/dev/ttyACM0',9600)
when doing
from a import b
you are trying to import member b from module a.
when doing
import a
you are importing the whole module a.
good luck
Hi You Have Too USE bellow code
from serial import Serial

Finding a specific serial COM port in pySerial (Windows)

I have a script built (Windows 7, Python 2.7) to list the serial ports but I'm looking for a device with a specific name.
My script:
import serial.tools.list_ports
ports = list(serial.tools.list_ports.comports())
for p in ports:
print(p)
This returns:
COM3 - Intel(R) Active Management Technology - SOL (COM3)
COM6 - MyCDCDevice (COM6)
COM1 - Communications Port (COM1)
>>>
Great! However, I want this script to automatically pick out MyCDCDevice from the bunch and connect to it.
I tried:
import serial.tools.list_ports
ports = list(serial.tools.list_ports.comports())
for p in ports:
if 'MyCDCDevice' in p:
print(p)
// do connection stuff to COM6
But that doesn't work. I suspect because p isn't exactly a string, but an object of some sort?
Anyways, what's the correct way to go about this?
Thanks!!
I know this post is very old, but I thought I would post my findings since there was no 'accepted' answer (better late than never).
This documentation helped with determining members of the object, and I eventually came to this solution.
import serial.tools.list_ports
ports = list(serial.tools.list_ports.comports())
for p in ports:
if 'MyCDCDevice' in p.description:
print(p)
# Connection to port
s = serial.Serial(p.device)
To further extend on this, I've found it safer to make use of the PID and VID of the device in question.
import serial.tools.list_ports
# FTDI FT232 device (http://www.linux-usb.org/usb.ids)
pid="0403"
hid="6001"
my_comm_port = None
ports = list(serial.tools.list_ports.comports())
for p in ports:
if pid and hid in p.hwid:
my_comm_port = p.device
Better still, you can use the serial number of the device for the lookup, just in case you have 2 of the same device plugged in.
(Source)
You can use serial.tools.list_ports.grep, which searches all of the description fields for you. For example:
from serial.tools import list_ports
try:
cdc = next(list_ports.grep("MyCDCDevice"))
# Do connection stuff on cdc
except StopIteration:
print "No device found"
If that doesn't work, you may try adding a * to the end of the string you pass to grep in case there are extra characters in the descriptor.

Trying to generate a list of product/vendor ids using PyUSB

I am trying to generate a list of product/vendor IDs with Pyusb and I am having trouble. I found a suggestion online from orangecoat.
import sys
import usb.core
import usb.util
dev = usb.core.find(find_all=True)
if dev is None:
raise ValueError('Device not found')
cfg = dev.get_active_configuration()
Python gives the following error though:
Traceback (most recent call last):
File "C:/Python27/usbfinddevices.py", line 10, in <module>
cfg = dev.get_active_configuration()
AttributeError: 'generator' object has no attribute 'get_active_configuration'
Could someone help me understand why I am getting this error?
Thank you
You're almost there, but you need to iterate through your dev object which is a generator.
dev = usb.core.find(find_all=True)
for d in dev:
print usb.util.get_string(d,128,d.iManufacturer)
print usb.util.get_string(d,128,d.iProduct)
print (d.idProduct,d.idVendor)
Save this script
test.py
import usb.core
import usb.util
dev = usb.core.find(find_all=True)
# get next item from the generator
d = dev.next()
print d.get_active_configuration()
then, run this
sudo python test.py
On Windows with Python 3 you need to change line d = dev.next() to d = next(dev) (as pointed out in the comments by #gabin)

python read serial output from arduino

I have an Arduino hooked up with 2 DS18B20 temp sensors. I'm very (VERY) new to python. I am looking for a way to read the serial input and parse it into a sqlite database, but that is getting ahead of myself. Why do I get an error while trying to define my serial port to a variable?
First things first sys.version
2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)]
My current, just read input from the serial connection program.
from serial import serial
import time
# open serial port
ser = serial.Serial('/dev/tty.usbmodem621',9600,timeout=2)
ser.open()
while True:
print('dev 0' + ser.read())
pass
ser.close()
I can not currently get it to compile. Most of the results I've found for this error tell to add from serial import serial, but in this case it hasn't worked.
The error.
$ python ser.py
Traceback (most recent call last):
File "ser.py", line 1, in <module>
from serial import serial
File "/Users/frankwiebenga/serial.py", line 8, in <module>
AttributeError: 'module' object has no attribute 'Serial'
Also if I just use import serial I get the same error
$ python ser.py
Traceback (most recent call last):
File "ser.py", line 1, in <module>
import serial
File "/Users/frankwiebenga/serial.py", line 8, in <module>
AttributeError: 'module' object has no attribute 'Serial'
Also, per comment. Created new file named something.py and still get the same error regardless of using import serial or from serial import serial.
$ python something.py
Traceback (most recent call last):
File "something.py", line 1, in <module>
from serial import serial
ImportError: No module named serial
When running my bash script I get an output that is valid, so I know it isn't the Arduino code.
Output:
Requesting temperatures...DONE
Device 0: 25.62
Device 1: 25.75
Requesting temperatures...DONE
Device 0: 25.62
Device 1: 25.81
Bash:
while true # loop forever
do
inputline="" # clear input
# Loop until we get a valid reading from AVR
until inputline=$(echo $inputline | grep -e "^temp: ")
do
inputline=$(head -n 1 < /dev/tty.usbmodem621)
done
echo "$inputline"
done
You need to use import serial. serial is the name of the module and it does not contain an attribute with name serial.
http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports
You can EITHER do:
from serial import Serial
s = Serial(...)
OR:
import serial
s = serial.Serial(...)
Choose one.
You need do pip install pyserial instead of pip install serial (which does not run into an error but installs another module).

Categories