cannot communicate Arduino with python - python

I am getting an error while trying to communicate Arduino with python, I am using Arduino module and I'm getting cannot open port error and I can communicate my Arduino from Arduino IDE.
from Arduino import Arduino
import time
board = Arduino(port="/dev/cu.usbmodem14201") # plugged in via USB, serial com at rate 115200
board.pinMode(13, "OUTPUT")
while True:
board.digitalWrite(13, "LOW")
time.sleep(1)
board.digitalWrite(13, "HIGH")
time.sleep(1)
This is my error
serial.serialutil.SerialException: [Errno 2] could not open port /dev/cu.usbmodem14201: [Errno 2] No such file or directory: '/dev/cu.usbmodem14201'
when I tried with pyfirmata I am getting an error
This is my code:
import pyfirmata
import time
board = pyfirmata.Arduino('/dev/cu.usbmodem14201')
led = board.get_pin('d:13:o')
while True:
led.write(1)
time.time(1)
led.write(0)
time.time(1)
my error for pyfirmata:
AttributeError: partially initialized module 'pyfirmata' has no attribute 'Arduino' (most likely due to a circular import)

To preface, I have done some Serial communication with Arduino, but have not worked with the Arduino library too extensively.
I suggest if you haven't done so already, considering the PySerial library. This may help with your initial issue with serial connection between your Mac and board. This does not entirely fix your need of directly writing to the LEDs, but can serve as a substitute in the meantime. You can use the incoming serial communication from your Mac to direct certain operations on your Arduino.
A great tutorial I have used can be found here:
https://create.arduino.cc/projecthub/ansh2919/serial-communication-between-python-and-arduino-e7cce0
Another issue may be that your Serial Monitor may be active which is blocking serial communication between devices over Python.

Related

testing usb to serial to usb using pyserial has no output

I'm trying to test writing messages to serial port using pyserial and reading it again using two usb to serial adapters connected back to vack "USB-Serial --> Serial-USB" to verify it is writing to the serial port as it is meant for communication with hardware,
so I have open console witch is reading all the time
import serial
port = 'COM6'
read_ser = serial.Serial(port)
while True:
x=read_ser.read()
print(x)
and for writing I use
import serial
port = 'COM5'
ser = serial.Serial(port)
# ser.write(str.encode('$GPRMC,081117.24,A,5042.988302,N,1058.376556,E,14.7,,230813,0.0,E*74'))
ser.write('$GPRMC,081117.24,A,5042.988302,N,1058.376556,E,14.7,,230813,0.0,E*74'.encode())
And nothing is shown in the reading console
I tried both adapters and read from external serial hardware successfully.
I'm using python 3.7 on windows 10
I figured it out
The problem was with the pin layout as I used gender changer to connect the two serial adapters
using serial cable instead solved the problem.

Python Serial Communication with Mbed

import serial
while True:
device = serial.Serial('/dev/ttyACM0')
data = device.readline()
print(data + '\n')
I have a device that communicates to a raspberry pi using USB port and pyserial in python. What should I do so that when user inputs "reset" on the command line the device will be reset?
Late reply, but you would need to create some handler code on the device that calls NVIC_SystemReset() whenever it sees reset come in over the serial port.
You haven't specified what "a device" means. Assuming the device is an Mbed device connected to the Raspberry pi and your Python code is running on the Raspberry pi.
Most Mbed devices can be reset by sending BREAK condition on the serial interface. Check pySerial API for this https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.send_break
Also, check how it is done in this tool for Python 2.7 and 3.x https://github.com/ARMmbed/htrun/blob/master/mbed_host_tests/host_tests_plugins/module_reset_mbed.py#L60

Reading XBee data with Python not possible

I have chosen the following setup to read sensor data by Arduino and an XBee-connection:
List itemA TMP36-sensor is connected to an Arduino Uno
List itemA Sparkfun XBee-shield with an XBee S2C is mounted on the Arduino (Router, API-mode). The Arduino is connected to COM3.
List itemCOM4 is connected with a Sparkfun XBee-Explorer (USB-connection). Another XBee S2C is connected on the explorer. This XBee is the coordinator (API-mode).
I have written the code for reading data from Xbee-Explorer at COM4:
#! /usr/bin/python
# Import and init an XBee device
from xbee import XBee, ZigBee
import serial
ser = serial.Serial('COM4', 9600)
xbee = XBee(ser)
while True:
try:
enter response = xbee.wait_read_frame()
print response
except KeyboardInterrupt:
break
ser.close()
At the moment it is not possible to get any data received by the Xbee with the Python code, although it is possible to read the data by XCTU.
In Detail:
If I send sensor data (sensor reading and sending to Xbee is done by Arduino Software) from the router to the coordinator, I'm able to read the data frames by XCTU and the results make sense. If I use the Python-code above, I did not get any data frames, although the RSSI-diodes of router and coordinator are blinking independently from the software (XCTU or Python) I use.
For me it is not clear what is going wrong and I would be happy to get some help to solve the problem.
Thank you very much for your support.
Regards Daniel
I had the same problem, changing from API=2 to API=1 solved my problem

Having errors working with com ports using pyserial

I am trying to interface with my com ports, specifically an XBee connected to thi using this code.
from xbee import XBee
from serial import Serial
PORT = 'COM3'
BAUD = 9600
ser = Serial(PORT, BAUD)
xbee = XBee(ser)
# Send the string 'Hello World' to the module with MY set to 1
xbee.tx(dest_addr='\x00\x01', data='Hello World')
# Wait for and get the response
print(xbee.wait_read_frame())
ser.close()
However, this error keeps arising.
SerialException: could not open port 'COM3': WindowsError(5, 'Access is denied.'). It goes away when I restart my computer, buts it keeps returning. I'd prefer to understand why its happening so I don't need to keep restarting my computer. Would really appreciate any help, thanks. I am working through the IDLE interface with python 2.7 just in case that is relevant.
A serial port can be "open" in only one application at a time. Once application "A" opens the port, application "B" will get an Access Denied error when it tries to open the same port. In your case, you need to figure out what other application is holding the port and close it first.

how can i connect and send commands to a serial port while another program is using it?

I am using a telit he910g card. it is connected to my PC directly using a miniPCI slot.
I am using it for 3G internet connection and A-GPS/GPS services.
My system is running linux mint 17.1, the 3G connection is handled using the network manager APP and works great. The 3G connection is started and handled using a module that is part of a program I am writing.
The code I am using in order to connect to the serial port is this:
def _connect_to_device(self):
""" Connect to a serial port """
try:
self._device = serial.Serial(self._filename, baudrate=self._baud_rate)
except StandardError, e:
raise StandardError("Couldn't connect to GPS device. Error: %s" % str(e))
When I use the python program alone it works great. But when I try and use it while the 3G is on i cant connect to the serial device. The wierd thing is that if I try to connect to it using a program like "minicom" while 3G is turned on it DOES work.
So my question is: how can I make both run and work together? since now they are mutually exclusive.
thanks to all who help. :)
Glad you found a way round your problem. Just for completeness:
Normally, serial ports can be opened by multiple processes.
If one of them does ioctl(,TIOCEXCL) on the open file then further opens will return EBUSY until everyone closes the device. Only root can get past this and open the device at all times.
If root opens the device and does an ioctl(,TIOCNXCL), then other processes can open the device too.
In python, TIOCNXCL isnt defined anywhere, but you can do the ioctl (eg on stdin) with:
import fcntl
TIOCEXCL = 0x540c # from /usr/lib64/perl5/asm-generic/ioctls.ph
TIOCNXCL = 0x540d
print fcntl.ioctl(0, TIOCNXCL)
Ok, so it is solved.
the issue was that the telit module has 2 ports /dev/ttyACM0 (high speed) and /dev/ttyACM3 (lower speed).
I tried to connect to the high speed one, but apparently the 3G uses that one and it causes contentions.
So moving to use the lower speed port in my script solved the issue.

Categories

Resources