¿Alguien puede mostrarme un código de muestra completo de Python que use pyserial , tengo el paquete y me pregunto cómo enviar los comandos AT y leerlos de nuevo!
96
Publicación de blog Conexiones serie RS232 en Python
import time
import serial
# configure the serial connections (the parameters differs on the device you are connecting to)
ser = serial.Serial(
port='/dev/ttyUSB1',
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)
ser.isOpen()
print 'Enter your commands below.\r\nInsert "exit" to leave the application.'
input=1
while 1 :
# get keyboard input
input = raw_input(">> ")
# Python 3 users
# input = input(">> ")
if input == 'exit':
ser.close()
exit()
else:
# send the character to the device
# (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)
ser.write(input + '\r\n')
out = ''
# let's wait one second before reading output (let's give device time to answer)
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
if out != '':
print ">>" + out
serial.serialutil.SerialException: Port is already openal ejecutar este código. No estoy seguro de esto, pero creo que el puerto serie se abre automáticamente cuando se define explícitamente como lo ha hechoser. Después de comentar laser.open()línea funcionó.ser.open()use https://pythonhosted.org/pyserial/ para obtener más ejemplos
fuente
http://web.archive.org/web/20131107050923/http://www.roman10.net/serial-port-communication-in-python/comment-page-1/
fuente
No he usado pyserial pero me baso en la documentación de la API en https://pyserial.readthedocs.io/en/latest/shortintro.html , parece una interfaz muy agradable. Podría valer la pena verificar dos veces la especificación de los comandos AT del dispositivo / radio / lo que sea que esté tratando.
Específicamente, algunos requieren un período de silencio antes y / o después del comando AT para que ingrese al modo de comando. Me he encontrado con algunos a los que no les gustan las lecturas de la respuesta sin demora primero.
fuente