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
Related
I am using a modul max31865 and a pt100 sensor to measure the temperature but, I have a problem when i run the program,because appears this error
Traceback (most recent call last):
File "/home/pi/eduardo/videos/temperature.py", line 5, in <module>
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
AttributeError: module 'board' has no attribute 'SCLK'
I don't know what the problem is.
import board
import busio
import digitalio
import adafruit_max31865
spi = busio.SPI(board.SCLK, MOSI=board.MOSI, MISO=board.MISO)
cs = digitalio.DigitalInOut(board.D5) # Chip select of the MAX31865 board.
sensor = adafruit_max31865.MAX31865(spi, cs, wires=3)
print('Temperature: {0:0.3f}C'.format(sensor.temperature))
print('Resistance: {0:0.3f} Ohms'.format(sensor.resistance))`enter code here`
I expect to read the temperature
Kind regards.
Are you very sure that should not be board.SCLK? Your code seems to be quite close to this example and that is how the constant is spelled there.
Have you enabled I2C interface in raspi-config?
This did it for me
sudo pip3 install --force-reinstall adafruit-blinka
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)
I have this structure
02.SensorTag/
sensortag_example.py
bluepy/bluepy/sensortag.py
bluepy/bluepy/btle.py
So the sensortag_example.py is importing the sensortag.py
import bluepy
from bluepy.bluepy import sensortag
When I ran the code it complains about the import from the sensortag.
Traceback (most recent call last):
File "sensortag_example.py", line 2, in <module>
from bluepy.bluepy import sensortag
File "/home/pi/Development/02.SensorTag/bluepy/bluepy/__init__.py", line 3, in <module>
from . import sensortag
File "/home/pi/Development/02.SensorTag/bluepy/bluepy/sensortag.py", line 1, in <module>
from bluepy.btle import UUID, Peripheral, DefaultDelegate, AssignedNumbers
ImportError: No module named 'bluepy.btle'
I've tried to add a new path but it didin't work. If I move the program to the first folder bluepy and change the import to "from bluepy import sensortag" it works, but I'll need to import other libs so I don't want to let it in bluepy folder.
I am trying to run this code:
https://gist.github.com/atotto/ae603b962115eef703c0011d8e652ea3
Thanks and best regards,
Edu
Because sensortag.py is in the same directory as btle.py, add a . in front of the import
from .btle import UUID, Peripheral, DefaultDelegate, AssignedNumbers
This is known as a relative import: https://docs.python.org/2.5/whatsnew/pep-328.html
As both btle.py and sensortag.py are in the same directory so by looking at your error I am assuming that you tried to import it from previous directory. So place from .btle import UUID in sensortag.py should solve the issue.
You should create two init.py file.
02.SensorTag/
sensortag_example.py
bluepy/__init__.py
bluepy/bluepy/__init__.py
bluepy/bluepy/sensortag.py
bluepy/bluepy/btle.py
I'm currently working on a project that requires I edit a configure file to replace an old standard port number if the port is being used. The code I'm currently using is the following:
import os
import sys
import socket
import select
import tempfile
import subprocess
import threading
import Queue
import time
import fileinput
...
def find_open_port():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(("",0))
s.listen(1)
tempport = s.getsocketname()[1]
s.close()
return tempport
When I run it from my Ubuntu machine (Python 2.7.6) , it runs fine, but on my CentOS 6 VM running in my Redhawk Component I get the following:
AttributeError: '_socketobject' object has no attribute 'getsocketname'
Not exactly sure why I'm getting this error. Python in Redhawk is running 2.6 I want to say?
Any clue as to why this would happen and how to fix?
Your code calls the method getsockname but your error says getsocketname, you sure you copied it right when writing it to Redhawk?
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).