Python Serial Communication with Mbed - python

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

Related

Serial communication with Raspberry Pi Pico and Python

I'm trying to achieve 2-way comms over USB (COM port) between Raspberry Pi Pico and Windows PC (Python).
The point is, that I'm unable to send anything from my PC to raspberry nor the way back.
Doesn't affect the LEDs on breadboard, nor the messages get printed in terminal.
Here's the code for PC:
import serial
import time
# open a serial connection
s = serial.Serial("COM7", 115200)
print(s)
# blink the led
while True:
s.write(b"on\n")
time.sleep(1)
s.readline().strip()
s.write(b"off\n")
time.sleep(1)
s.readline().strip()
And here's the piece of code on Raspberry Pi Pico:
import time
from machine import Pin
import sys
led = Pin(0, machine.Pin.OUT)
led2 = Pin(2, machine.Pin.OUT)
led2.value(0)
led.value(0)
def led_on():
led.value(1)
def led_off():
led.value(0)
while True:
# read a command from the host
v = sys.stdin.readline().strip()
# perform the requested action
if v.lower() == "on":
led_on()
print("Turned on!")
elif v.lower() == "off":
led_off()
print("Turned off!")
Also - what's the smartest way to debug the code onboard the Raspberry Pi Pico?
After acquiring the serial connection, standard print debug?
Is there any way to use the sequence debugger in Thonny IDE?
Best Regards!
I've tried many methods from both serial or stdlib libraries without any result.
Also important thing - for PC side of script I'm using PyCharm, for Raspberry side - Thonny.
After flashing Raspberry Pico, I'm disconnecting the serial and starting to run the script in PyCharm, with different interpreter.
Easiest Solution
Serial communication is purely bi-directional. There cannot be more than 2 devices on a given serial port. In Thonny (and micropython) this is dedicated to loading code. You cannot directly write to the serial port from your computer when a program is running. When a program isn't running on the pico, however, you have access to REPL. You can read a little more about it here. This allows you to call functions directly on the pico. So you could write a program such as this:
from machine import pin
led = Pin(0, machine.Pin.OUT)
led2 = Pin(2, machine.Pin.OUT)
led2.value(0)
led.value(0)
def led_on():
led.value(1)
def led_off():
led.value(0)
and call it from REPL by sending
>>> led_on()
This is far and away the easiest solution and also gives you access to any function within microPython which may be useful for debugging as you can just print whatever value you want.
More Robust Solution
Code code above isn't wrong. It just will not work with Thonny since Thonny has taken over the serial port. If you upload your code as a .u2f file or use a separate debugger (such as a pi or a second pico as a picoprobe) using one of the UART peripherals then you could use the USB port as a serial connection. There is a thread over on the raspberry pi forms where functional 2-way serial code is shown. Similarly, this tutorial walks you through doing it in a background-thread.

Using bluetooth module to read input from bluetooth terminal on phone and print to console on raspberry pi

I am stuck on a problem I cannot overcome now and would appreciate the help!
I am trying to send commands to raspberry pi from a phone via a Bluetooth terminal.
As I am developing, I am now trying to get the raspberry pi to read the serial value from the UART pins that the Bluetooth module is connected to when the phone is connected to the Bluetooth module.
My setup is as follows:
I am using Pyserial to attempt to communicate with the Bluetooth module connected to the tx/rx pins.
The code I am using to do this is as follows:
import time
import serial
if __name__ == '__main__':
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=1)
ser.reset_input_buffer()
while True:
time.sleep(1)
line = ser.readline().decode('utf-8').rstrip()
print(line)
The resultant output to the raspberry pi console is just a blank output, even when I send letters/values in the Bluetooth terminal connected with the Bluetooth module.
Note: I am using a Bluetooth module instead of the built-in Bluetooth because I need phones to connect to the raspberry pi without needing to accept the pairing connection within the raspberry pi console.

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.

how to read data that is written from a communication Port?

I try to make communication between my device and python code via COM4 a communication port: pyserial communication.
So at first I try to send and write a hello then to read the output of my device,
But the problem for me is how to read the hello that is sent firstly. I mean that I want to read the string hello on COM4 from my device
import serial
ser = serial.Serial(
port='COM4',\
baudrate= 230400) # open serial port
print(ser.name) # check which port was really used
#ser.write(b'hello\n') # write a string
#str=ser.readline()
while True:
print(ser.read(30).decode())
ser.close() # close ports
The code of my device is written in C language.
I would be very grateful if you could guide me please.
This process differs based on the operating system. Here's a good library that supports Linux and Windows:
http://www.teuniz.net/RS-232/

Serial communication emulation in Windows

I want to emulate an Arduino serial communication in Windows. I wrote this pySerial script to represent the connection:
from serial import Serial
from time import sleep
serial_conn = Serial(<some port>)
serial_conn.baudrate = 9600
for i in range(1,10):
serial_conn.write(<dummy data>)
sleep(1)
The problem is the following: I tried to use available serial ports (like COM1 or COM3 for example) but I can't sniff the port with a serial monitor tool. It's because I need hardware to open the port? Or maybe the problem are the tested ports? (maybe Windows uses the COM1 for comm as Linux uses the first serial too). Should I try with a virtual serial ports tool? If that's the point, can you recomend me someone and the usage?
In Windows hardware and virtual serial ports have the same enumeration scheme, so they will be COM.
The problem is that only one program at time (theorically) can use a Serial port, so if the port is used by your Python program, it won't be available to the terminal.
You should setup a fake COM, and this mean a custom driver... too much difficult.
Socket, file and standard input can be read/written one byte a time, so you can test your parser using them.

Categories

Resources