I am currently trying to read the output of writing a command to the serial port where my USB device is connected.
import pyserial
ser = serial.Serial(port=portName,baudrate=19200,parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS)
ser.xonxoff = 0
ser.rtscts = 0
ser.set_buffer_size(rx_size=256, tx_size=256)
ser.writeTimeout = 2
ser.timeout = 2
ser.write("\r".encode())
if not ser.is_open:
ser.open()
try:
ser.flushInput()
ser.flushOutput()
ser.write(command.encode())
return ser.readlines()
finally:
ser.close()
I always have to run this code twice in order to execute a command. What parameter should I change here in order to not have this issue?
Related
m trying to create a tool that help to simulate key press
but at part of this code mentioned below, the program gets stuck and its not proceeding
ser = serial.Serial(ACM)
ser.baudrate = 115200
ser.timeout = None
ser.flushInput()
ser.flushOutput()
ser.write(cmd)
p = ser.readline()
pr = p.decode("utf-8")
print(cmd)
print(pr)
ser.close()
print("Closed")
" ser.readline() is not reading the output of serial com /dev/ttyACM*
Thanks
The PySerial documentation is unmistakable:
Be careful when using readline(). Do specify a timeout when opening
the serial port otherwise it could block forever if no newline
character is received.
You define: ser.timeout = None so what exactly do you expect your program to do, but block forever if you don't receive a newline?
Either make sure the sent lines are terminated or use a reasonable timeout.
Devices used : Beaglebone_AI and 2 STM32 nucleos connected to the Beaglebone AI using USB hub.
The idea:
The Beaglebone uses a python3 script that uses multiprocessing and pyserial to create 3 processes:
Process 1 - Request a string from STM32 #1 and adds the string to FIFO Queue
Process 2 - Function that writes all responses from STM32 #2
Process 3 - .get() from FIFO Queue go through a switch statement to process either strings from STM32 #1 or user commands.
The Problem:
- It seems like sometimes pyserial .write() dosnt send the user command or data.
can Pyserial .read() and .write() at the same time ??
This is my function process that prints everything that it receives:
def writeall(serial_port, q2):
all_bytes = []
while True:
try:
bytesToRead = serial_port.inWaiting()
data = serial_port.read(bytesToRead).decode('utf-8')
if not data:
pass
else:
if data == '\n':
data = ''.join(all_bytes)
#Print to screen everything
print(data, end = '\033[K\n\n', flush=True)
all_bytes.clear()
#serial_port.reset_input_buffer()
else:
#append all the bytes that are being received
all_bytes.append(str(data))
except (OSError,serial.serialutil.SerialException) as e:
print(f'Actuator Controller: {e}') ```
**This is my serial port config:**
``` def opening_port(q2):
global serial_port
try:
a =""
for p in ports:
#if 'COM9' in p.device: #for windows
if "066BFF343633464257245637" in p.serial_number: #my nucleo
a = p.device
# Port initialization
serial_port = serial.Serial(
port= a,
baudrate=9600,
bytesize= serial.EIGHTBITS,
parity= serial.PARITY_NONE,
stopbits= serial.STOPBITS_ONE,
timeout= 1)
# Discarding anything in the I/O buffer.
#serial_port.reset_output_buffer()
#serial_port.reset_input_buffer()
return serial_port ```
**ANd this is pyserial .write() function that uses the same port as the writeall function process**
``` def write_comand(comand):
try:
#Reset the buffer before sending a command
serial_port.reset_output_buffer()
serial_port.reset_input_buffer()
serial_port.write(bytes(comand,'utf8'))
# Testing wheter the Queue is getting the user commands and if we are actually sending them
if '3E12' in str(comand):
pass
else :
print(f" cmd sent {comand}\n")
#time.sleep(0.005)
except (OSError,serial.serialutil.SerialException) as e:
print (f" There was a problem communicating with AC: {e}\n")
print('Resetting Actuator Controller...\n')
write_comand('B6D8\r')# reset command ```
I have light detector sensors connected to a data acquisition box and it is connected to my laptop via RS232 usb cable. I have stabilized serial communication to that port in python. But when I try to read the data it just keeps on running and it doesn't display any value. I have tried this same think in MATALB and it works properly, so I know that port and sensors are working fine. I am just not able to read the data in python. I have three ways(shown below in python code) but nothing works. Please help me.
Here is my python code:
import serial
from serial import Serial
s = serial.Serial('COM3') # open serial port
print(ser.name)
# Set the serial port to desired COM
s = serial.Serial(port = 'COM3', bytesize = serial.EIGHTBITS, parity = serial.PARITY_NONE, baudrate = 9600, stopbits = serial.STOPBITS_ONE)
# Read the data(method 1)
while 1:
while (s.inWaiting() > 0):
try:
inc = s.readline().strip()
print(inc)
# Read the data(method 2)
data = str(s.read(size = 1))
print(data)
# Read all the data(method 3)
while i <10:
b = s.readlines(100) # reading all the lines
in matlab it gave values with a space but in same line
That indicates there are no newline characters sent by the device.
I've never used your device or the Python serial module. But from the docs, this is what I would do:
from time import sleep
while s.is_open():
n = s.in_waiting()
if n == 0:
print "no data"
sleep(1)
continue
try:
inc = s.read(n)
print(inc)
catch serial.SerialException as oops:
print(oops)
catch serial.SerialTimeoutException as oops:
print(oops)
print("serial port closed")
This gives you feedback for each condition: port open/closed, data ready/not, and the data, if any. From there you can figure out what to do.
The documentation says inWaiting is deprecated, so I used in_waiting.
I wouldn't be surprised if the problem lies in configuring the serial port in the first place. Serial ports are tricky like that.
I'm trying to write test code for sending data via UART on my Raspberry PI 3 b+ , but I cannot receive back the data I've sent . Raspberry is connected via UART module to my laptop , so I can see in Putty results. Anyone can tell me what Am I doing wrong?
I've checked if port isOpen and it returned True , msg=b'Hello' returned Hello showed hello , but no bytes received . Sending single bytes give also no bytes detected . Erasing the timeout showed that it is reached.
Edit: did little testing if port's are working properly
from __future__ import print_function
import serial
test_string = "Testing 1 2 3 4".encode('utf-8')
#test_string = b"Testing 1 2 3 4" ### Will also work
port_list = ["/dev/serial0", "/dev/ttyS0"]
for port in port_list:
try:
serialPort = serial.Serial(port, 9600, timeout = 2)
serialPort.flushInput()
serialPort.flushOutput()
print("Opened port", port, "for testing:")
bytes_sent = serialPort.write(test_string)
print ("Sent", bytes_sent, "bytes")
loopback = serialPort.read(bytes_sent)
if loopback == test_string:
print ("Received", len(loopback), "valid bytes, Serial port", port, "working \n")
else:
print ("Received incorrect data", loopback, "over Serial port", port, "loopback\n")
serialPort.close()
except IOError:
print ("Failed at", port, "\n")
That give me information that tty0 is not working properly but , also got absolutly no answer about correctness on port serial0
import serial
import struct
import time
port = serial.Serial("/dev/ttyS0", baudrate=115200, timeout=2.0)
i = 0
while True:
msg = struct.pack('>HBBB', 3000, 243, 234, 254)
port.write(msg)
time.sleep(0.3)
bytesToRead = port.inWaiting()
print("Found {} bytes in serial".format(bytesToRead))
if bytesToRead == 5:
rcv = port.read(5)
# port.write('\r\nYou sent:' + repr(rcv))
for i in range(5):
print('\r {} - {}'.format(i, bytes(rcv[i])))
idCode = struct.pack('BB', rcv[0], rcv[1])
idCode = struct.unpack('>H', idCode)
idCode = idCode[0]
# value = struct.unpack_from('HBBB', decode)
i += 1
if i == 4:
exit()
Expected Results:
Found 5 bytes in serial
(index) - (byte at that index)
Got:
Found 0 bytes in serial
Solved. It was just matter of wiring .
If someone in future would like to run test like that - they must remember to connect RX with TX line . Or if it connected with laptop there's it should be better to create code responsible for communication on it (but instead of ttyS0 or Serial0 , port should be set to proper COM e.g. "COM3")
I am trying to establish a two-way communication via Python3. There is a laser range finder plugged into one of my USB ports and I'd like to send/receive commands to that. I have a sheet of commands which can be sent and what they would return, so this part is already there.
What I need is a convenient way to do it in real-time. So far I have the following code:
import serial, time
SERIALPORT = "/dev/ttyUSB0"
BAUDRATE = 115200
ser = serial.Serial(SERIALPORT, BAUDRATE)
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
ser.timeout = None #block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 0 #timeout for write
print ("Starting Up Serial Monitor")
try:
ser.open()
except Exception as e:
print ("Exception: Opening serial port: " + str(e))
if ser.isOpen():
try:
ser.flushInput()
ser.flushOutput()
ser.write("1\r\n".encode('ascii'))
print("write data: 1")
time.sleep(0.5)
numberOfLine = 0
while True:
response = ser.readline().decode('ascii')
print("read data: " + response)
numberOfLine = numberOfLine + 1
if (numberOfLine >= 5):
break
ser.close()
except Exception as e:
print ("Error communicating...: " + str(e))
else:
print ("Cannot open serial port.")
So in the above code I am sending "1" which should trigger "getDistance()" function of the laser finder and return the distance in mm. I tried this on Putty and it works, returns distances up to 4 digits. However, when I launch the above Python script, my output is only the following:
Starting Up Serial Monitor
Exception: Opening serial port: Port is already open.
write data: 1
read data:
and it goes forever. There is no read data or whatsoever.
Where am I mistaken?
Apparently much more simpler version of the code solved the issue.
import serial
import time
ser = serial.Serial('/dev/ttyUSB0', 115200, timeout = 1) # ttyACM1 for Arduino board
readOut = 0 #chars waiting from laser range finder
print ("Starting up")
connected = False
commandToSend = 1 # get the distance in mm
while True:
print ("Writing: ", commandToSend)
ser.write(str(commandToSend).encode())
time.sleep(1)
while True:
try:
print ("Attempt to Read")
readOut = ser.readline().decode('ascii')
time.sleep(1)
print ("Reading: ", readOut)
break
except:
pass
print ("Restart")
ser.flush() #flush the buffer
Output, as desired:
Writing: 1
Attempt to Read
Reading: 20
Restart
Writing: 1
Attempt to Read
Reading: 22
Restart
Writing: 1
Attempt to Read
Reading: 24
Restart
Writing: 1
Attempt to Read
Reading: 22
Restart
Writing: 1
Attempt to Read
Reading: 26
Restart
Writing: 1
Attempt to Read
Reading: 35
Restart
Writing: 1
Attempt to Read
Reading: 36
It seems to me that your ser.timeout = None may be causing a problem here. The first cycle of your while loop seems to go through fine, but your program hangs when it tries ser.readline() for the second time.
There are a few ways to solve this. My preferred way would be to specify a non-None timeout, perhaps of one second. This would allow ser.readline() to return a value even when the device does not send an endline character.
Another way would be to change your ser.readline() to be something like ser.read(ser.in_waiting) or ser.read(ser.inWaiting()) in order to return all of the characters available in the buffer.
try this code
try:
ser = serial.Serial("/dev/ttyS0", 9600) #for COM3
ser_bytes = ser.readline()
time.sleep(1)
inp = ser_bytes.decode('utf-8')
print (inp)
except:
pass