testing usb to serial to usb using pyserial has no output - python

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.

Related

cannot communicate Arduino with 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.

How do I communicate to a serial com port through a Python command in Jupyter?

I'm trying to communicate with a serial com port through Jupyter Notebooks. I need to be able to send commands for the serial device to run. This can be done through Hyperterminal commands but I need to be able to do this through a Python command in Jupyter.
I've tried using the write function of Pyserial but the strings that I send through Pyserial won't execute in the serial com port. I've implemented it like below:
import serial
ser = serial.Serial()
ser.baudrate = 19200
ser.port = 'COM25'
ser.write(b'!!!!!')
ser.close()
Since "!!!!!" is the command needing to be executed by the serial device, is there a way to send this to the device through Python?

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

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