I tried to read the GPS data from the receiver. I got strange strings as a result. Where is the problem?
import serial
port = "/dev/ttyUSB0" # Raspberry Pi 3
def parseGPS(data):
print(data)
#...
ser = serial.Serial(port, baudrate = 9600, timeout = 0.5)
while True:
data = ser.readline()
parseGPS(data)
result
The Problem was the wrong baud rate. Next example works problem-free:
import serial
port = "/dev/ttyUSB0" # Raspberry Pi 3
def parseGPS(data):
print(data)
# ...
ser = serial.Serial(port, baudrate=4800, timeout=0.5)
while True:
data = ser.readline()
parseGPS(data)
Related
Im trying to read the signal strength/quality from gsm modems, so i used this AT+CSQ command.
from time import sleep
import serial
from curses import ascii
ser = serial.Serial()
ser.port = "COM10"
ser.baudrate = 9600
ser.open()
print(ser.write('AT+CSQ=?'.encode("ascii")))
ser.write returns the number of bytes written (on the port).
(https://pyserial.readthedocs.io/en/latest/pyserial_api.html#serial.Serial.write)
You need to call ser.read afterward to read the answear.
Something like :
from time import sleep
import serial
ser = serial.Serial()
ser.port = "COM10"
ser.baudrate = 9600
ser.open()
try:
res_write = ser.write('AT+CSQ=?'.encode("ascii"))
sleep(0.01)
res_read = b""
while ser.inWaiting() > 0:
res_read += ser.read(1)
finally : # ensure you close the use of the port in case of crash
ser.close()
print(res_read.decode("ascii"))
I am checking the incoming data by reading continuously in while true in a separate thread. I don't want it to work constantly. Is there a function that will only be triggered when data arrives?
my code:
import threading
import serial
connected = False
port = "COM3"
baud = 115200
serial_port = serial.Serial(port, baud, timeout=0)
def handle_data(data):
print(data)
def read_from_port(ser):
select_read(serial_port)
serin = ser.read()
connected = True
while True:
reading = serial_port.in_waiting
reading = ser.readline().decode()
if len(reading) > 5:
handle_data(reading)
def set_data():
while True:
print("input: ")
num1 = str(input())
serial_port.write(num1.encode());
thread = threading.Thread(target=read_from_port, args=(serial_port,))
thread2 = threading.Thread(target=set_data)
thread.start()
thread2.start()
I am trying to make two beaglebone microcontrollers to communicate with each other using UART.
When there is an input by user on TX side of microcntroller, RX side has to get that input but my RX side is missing some of the data sent by TX side.
Here is my TX side code
import serial
import time
UART.setup("UART1")
ser = serial.Serial(
port = "/dev/ttyO1", #enable UART1
baudrate = 9600, #Set Baudrate = 9600
)
time.sleep(3)
ser.close() #Close serial port
ser.open() #Open serial port
ser.flushInput() #Flush the input of serial port
ser.flushOutput() #Flush the output of serial port
print ser.name #Print serial port being used in the beginning
while True: #Set the infinite loop for the input
print('enter the data')
dataToSend = raw_input()
tempData = str(dataToSend )
ser.write(tempData)
And this is my RX side code.
import Adafruit_BBIO.UART as UART
import serial
import time
UART.setup("UART1")
ser = serial.Serial(
port = "/dev/ttyO1", #enable UART1
baudrate = 9600 #Set Baudrate = 9600
)
time.sleep(3)
ser.close() #Close serial port
ser.open() #Open serial port
ser.flushInput() #Flush the input of serial port
ser.flushOutput() #Flush the output of serial port
msg_count = 0
slaveAddress = '1'
addressConfirmed = False
print ser.name #Print serial port being used in the beginning
while True:
size = ser.inWaiting()
if size > 0 :
rawData = ser.read(size)
rawData = str(rawData)
print(rawData)
else:
pass
Can someone please tell me what the problem is with my code?
Thank you
I'm attempting to read the values of some GPIO. Here's the code:
import serial
import codecs
import time
ser = serial.Serial(port = 'COM4', baudrate = 9600, \
parity = serial.PARITY_NONE, \
stopbits = serial.STOPBITS_ONE, \
bytesize = serial.EIGHTBITS, \
timeout = 0, \
)
print('connected to: ',ser.name)
ser.close()
def SSend(input):
ser.write(codecs.decode(input, "hex_codec")) #send as ASCII
print('sent: ', input)
def ReadIO():
#open the port
try:
ser.open()
except:
print('error opening serial port')
exit()
#flush the buffers
ser.flushInput()
ser.flushOutput()
#write data to read from GPIO
#causes SC18IM to return a byte containing each of the 8 I/O values
SSend(b'4950')
time.sleep(0.1) #allow time for the data to be received
#read the data
serialData = False
serialData = ser.readline()
ser.close()
return serialData
while 1:
print(ReadIO())
time.sleep(0.5)
This prints the following:
sent:
b'4950'
b''
(I am expecting back either 0x00 or 0x20 instead of an empty byte)
I know my hardware is good as is what I'm sending because it get back what I expect when using Realterm and have successful write commands in my script elsewhere.
I had some luck using this
#read the data
serialData = False
for c in ser.readline():
print('in loop')
print(c)
serialData = c
ser.close()
However, I don't really understand why it worked and it only appears to work intermittently.
Thanks for reading.
readline() assumes that there is some end-of-line symbol, like \n or \r. You should read data byte-wise:
serialData = ''
while ser.inWaiting() > 0:
c=ser.read(1)
# or c=ser.read(1).decode('latin1')
serialData += c
I'm trying communicate with STM32L152RB board through COM port 4 which accepts commands from COM4 and displays result in terminal using this code but it's not working ... I'm new to python please let me know what I'm doing wrong.
#Global Variables
ser = 0
def init_serial():
COMNUM = 4 #Enter Your COM Port Number Here.
global ser #Must be declared in Each Function
ser = serial.Serial()
ser.baudrate = 9600
ser.port = COMNUM - 1 #COM Port Name Start from 0
#ser.port = '/dev/ttyUSB0' #If Using Linux
#Specify the TimeOut in seconds, so that SerialPort
#Doesn't hangs
ser.timeout = 10
ser.open() #Opens SerialPort
# print port open or closed
if ser.isOpen():
print 'dis' + ser.portstr
init_serial()
temp = raw_input('Type what you want to send, hit enter:\r\n')
ser.write('dis') #Writes to the SerialPort
while 1:
bytes = ser.read() #Read from Serial Port
print 'You sent: ' + bytes #Print What is Read from Port