python read serial output from arduino - python

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).

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

How to fix the error 'AttributeError: module 'board' has no attribute 'SCK'' I am using raspberry pi

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

Exeption while listing active serial ports using pyserial

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)

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

UART in python on raspberry pi doesn't receive data

I am trying to implement data sending in Python 3 on a raspberryPi (as a part of a bigger project) and cannot receive data when I connect the Rx and Tx pins. Regardless of using Python 2 or 3 (as far as I understand this API allows Python 3 programming) I either get Received: b'\n' response or such an exception:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 471, in write
n = os.write(self.fd, d)
OSError: [Errno 5] Input/output error
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./uart.py", line 12, in <module>
port.write(bytearray(input_data, 'utf-8'))
File "/usr/lib/python3/dist-packages/serial/serialposix.py", line 485, in write
raise SerialException('write failed: %s' % (v,))
serial.serialutil.SerialException: write failed: [Errno 5] Input/output error
I can't write anything except from buffered reader though.
The code I've made is here:
#!/usr/bin/env python3
import serial
port = serial.Serial("/dev/ttyAMA0", baudrate=9600)
while True:
input_data = input("Say sth: ")
if input_data != 'exit':
port.write(bytearray(input_data, 'utf-8'))
print('Sent: {0}'.format(bytearray(input_data, 'ASCII')))
output_data = port.readline()
print('Received: {0}\n'.format(str(output_data)))
else:
break
port.close()
I want to use ASCII encoding since it will be further connected to an microcontroller with code in C. I've also checked whether any data is written into the buffer (and it is), I've tried out laying the programme to sleep for a second after sending data, I've tried using port.read(port.inWaiting()) and port.read(in_waiting) (no attribute found in the latter case) and nothing seems to be helpful.
I've also tried this example; I am sure correct pins are connected and I have updated and upgraded my raspbian by using sudo apt-get update and sudo apt-get upgrade and when I typed sudo apt-get install python3-serial I was told that I already have newest version installed.
I am posting this answear to close the topic and to help anyone who might come across similar difficulties.
Since the processor is of different architecture trying to set up ports with setserial was pointles, however this was exactly the problem.
pi#raspberrypi ~ $ sudo setserial -g /dev/ttyAMA0
/dev/ttyAMA0, UART: undefined, Port: 0x0000, IRQ: 83
The answear I found here solved all the problems.

Categories