Reading arduino serial connection in python - python

I'm trying to connect to my Ardiuno with Python on my Ubuntu computer. I tried this example:
http://playground.arduino.cc/interfacing/python
But I always get:
>>> import serial
>>> ser = serial.Serial('/dev/tty.usbserial', 9600)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/serial/serialutil.py", line 282, in __init__
File "build/bdist.linux-x86_64/egg/serial/serialposix.py", line 289, in open
OSError: [Errno 2] No such file or directory: '/dev/tty.usbserial'
>>>
I can use the serial connection in sketch without any problems. I also added my user to the groups tty and dialout.
How do I connect to my Arduino using Python?

list the serial ports first. I'm not sure dev/tty.usbserial is correct.
You try do ls /dev/tty* in Terminal, pehaps your Arduino board will pop up as ttyACM0 or something like that(sorry, haven't used Ubuntu in a while).
You can also check out the PySerial finding ports article(may need PySerial 2.7 or newer)

Related

PyDev breaking at built-in breakpoint

PyDev is breaking at the built-in breakpoint in Python code with the following error.
warning: Debugger speedups using cython not found. Run '"/Users/Work/opt/anaconda3/envs/ch_dev37/bin/python" "/Applications/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev.core_8.0.1.202011071328/pysrc/setup_cython.py" build_ext --inplace' to build.
Could not connect to 127.0.0.1: 5678
Traceback (most recent call last):
File "/Applications/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev.core_8.0.1.202011071328/pysrc/_pydevd_bundle/pydevd_comm.py", line 462, in start_client
s.connect((host, port))
ConnectionRefusedError: [Errno 61] Connection refused
Traceback (most recent call last):
File "/Volumes/GoogleDrive/My Drive/free_energy_data/write_energies.py", line 111, in <module>
write_gases, write_adsorbates, verbose)
File "/Users/Work/opt/dev/gits/CatHub/cathub/catmap_interface.py", line 31, in write_energies
breakpoint()
File "/Applications/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev.core_8.0.1.202011071328/pysrc/pydev_sitecustomize/sitecustomize.py", line 74, in custom_sitecustomize_breakpointhook
pydevd.settrace(*args, **kwargs)
File "/Applications/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev.core_8.0.1.202011071328/pysrc/pydevd.py", line 2623, in settrace
notify_stdin=notify_stdin,
File "/Applications/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev.core_8.0.1.202011071328/pysrc/pydevd.py", line 2688, in _locked_settrace
py_db.connect(host, port) # Note: connect can raise error.
File "/Applications/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev.core_8.0.1.202011071328/pysrc/pydevd.py", line 1262, in connect
s = start_client(host, port)
File "/Applications/Eclipse.app/Contents/Eclipse/plugins/org.python.pydev.core_8.0.1.202011071328/pysrc/_pydevd_bundle/pydevd_comm.py", line 462, in start_client
s.connect((host, port))
ConnectionRefusedError: [Errno 61] Connection refused
I am running the Python code using the 'Run' option (Play button in the attached image) in GUI.
This behavior is surprising because as the release notes says breakpoint() builtin has been supported since earlier release (v6.5.0) of PyDev. So far I have used import pdb; pdb.set_trace() for a breakpoint and it is still functioning well. The breakpoint() builtin is working well without any problem outside PyDev in a terminal.
Additional Details:
Python Version: 3.7.8
Eclipse Version: 2020-09 (4.17.0)
PyDev Version: 8.0.1
Device: MacBook Pro
OS: macOS Big Sur 11.0.1
In this case, breakpoint() tries to connect to the IDE using the Remote Debugger, and, the ConnectionRefusedError: [Errno 61] Connection refused means that you haven't started the remote debugger in the client side.
So, to fix this, please start the debug server in the client side prior to the run (then on launch it will run without the debugger attached until breakpoint() is reached).
See: https://www.pydev.org/manual_adv_remote_debugger.html for info on how to start the remote debugger on the client side.

serial.serialutil.SerialException: could not open port /dev/ttyAMA0: [Errno 13] Permission denied: '/dev/ttyAMA0'

I have a Raspberry that I am trying to connect to a Ubidots IOT cloud but I got this error:
pi#raspberrypi:~/Downloads/raspbiDaniela/Ver 2016/ZonrProject $ python main.py
Traceback (most recent call last):
File "main.py", line 124, in <module>
main()
File "main.py", line 21, in main
ser = serial.Serial(port=port, baudrate=baud)
File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 261, in __init__
self.open()
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 278, in open
raise SerialException("could not open port %s: %s" % (self._port, msg))
serial.serialutil.SerialException: could not open port /dev/ttyAMA0: [Errno 13] Permission denied: '/dev/ttyAMA0'
You don't need to run the script every time with sudo as suggested in the comments but you need to make sure that you have read/write permissions for /dev/ttyAMA0. You can modify the permisions using:
sudo chmod 666 /dev/ttyAMA0
If the device is detachable, you may also consider creating an udev rule which will apply correct permissions each time you connect it. You can read more about udev rules at Debian's wiki.
I faced this issue when I turn on SSH over serial. When I turn it off and only turn on serial interface through sudo raspi-config, everything works fine without permission problems.
Hope it helps other developers.

Connecting to an Ubuntu Server with Python

My goal is to have a basic FTP program written in Python. First, I need to gain more knowledge. My question is, how can I connect to an Ubuntu Server (hosted via VirtualBox) using Python?
I have tried using the page on the official Python website but I get an error saying socket.error: [Errno 61] Connection refused when using this
from ftplib import FTP
ftp = FTP('jordan#10.0.0.12')
This is the output I get when using ftp - FTP('10.0.0.12')
Traceback (most recent call last): File "", line 1, in
File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py",
line 120, in init
self.connect(host) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ftplib.py",
line 135, in connect
self.sock = socket.create_connection((self.host, self.port), self.timeout) File
"/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py",
line 575, in create_connection
raise err socket.error: [Errno 61] Connection refused
I can use an FTP program such as Transmit (the same port and on SFTP) on the same machine and it works fine.
Python's ftplib does not support SFTP, so using PySFTP would work.
An alternative is using paramiko.

Pyserial: could not configure port: (5, 'Input/output error)

I've been trying to get the following two lines of Python code to run for the past two days, without much success:
import serial
ser = serial.Serial(0)
Each time I run it, I get the following error:
Traceback (most recent call last):
File "./test.py", line 4, in <module>
ser = serial.Serial(0)
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 280, in open
self._reconfigurePort()
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 308, in _reconfigurePort
raise SerialException("Could not configure port: %s" % msg)
serial.serialutil.SerialException: Could not configure port: (5, 'Input/output error')
I'm running Ubuntu 11.10 64-bit, with Pyserial 2.5 (python-serial 2.5-2.1) and Python 2.7 (python 2.7.2-7ubuntu2) and my user is a member of the dialout group.
I run Ubuntu 11.10 64-bit at work too, with the same versions of Python and Pyserial, and the problem doesn't seem to occur there. Any suggestions are welcome - I'm pretty flummoxed...
Thanks,
Donagh
This exception is thrown if the port is not available. On Linux, you can simply specify the exact name of the port to use, like
ser = serial.Serial('/dev/ttyACM0')

PyMongo error while running on Windows

I just installed pymongo from an .exe on a windows 7 box. I tried connecting to mongodb by:
import pymongo
c = pymongo.Connection()
i get the following error:
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
c = pymongo.Connection()
File "E:\Softwares\installed\Python2.7\lib\site-packages\pymongo\connection.py", line 370, in __init__
self.__find_master()
File "E:\Softwares\installed\Python2.7\lib\site-packages\pymongo\connection.py", line 605, in __find_master
raise AutoReconnect("could not find master/primary")
AutoReconnect: could not find master/primary
I did some googling and i figured that the mongodb daemon is not running and hence i'm not able to connect. I tried doing the following:
c = pymongo.Connection('localhost', 27017)
But i got the same error. How do i start the mongodb deamon ?? Or is there any other problem/error ??
Thanks in advance :)
Go to http://www.mongodb.org/downloads to download the version of mongodb for your operating system.
Unzip the downloaded folder.
Run the file mongod.exe. (This is in the "bin" folder)
You should now be able to connect using pymongo.

Categories

Resources