Missing data using pyserial on two beaglebone blacks - python

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

Related

Does this show the signal strength/quality from GSM modems? why does it print 8?

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"))

Weird strings when reading the GPS data from the receiver

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)

Python Serial: having trouble reading the port

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

python send and receive data serially

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

How to write on serial port in python that ttyUSB0 will be interpreted commands?

I have raspberry PI B+ with connected Telegesis ZigBee module(ETRX3 USB sticks) via USB. Using commands:
debian:~# stty -F /dev/ttyUSB0 -raw ispeed 19200 ospeed 19200
debian:~# cat < /dev/ttyUSB0 &
debian:~# echo "ATI" > /dev/ttyUSB0
the ZigBee module executed ATI command and I can see the correct output:
Telegesis ETRX357
R308C
OK
The same thing I want to do with python script. I was written python script with code:
#!/usr/bin/env python
# based on tutorials:
# http://www.roman10.net/serial-port-communication-in-python/
# http://www.brettdangerfield.com/post/raspberrypi_tempature_monitor_project/
import serial, time
SERIALPORT = "/dev/ttyUSB0"
BAUDRATE = 19200
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.timeout = 0 #non-block read
ser.timeout = 2 #timeout 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, e:
print "error open serial port: " + str(e)
exit()
if ser.isOpen():
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput()#flush output buffer, aborting current output
ser.write("ATI")
print("write data: ATI")
time.sleep(0.5)
numberOfLine = 0
while True:
response = ser.readline()
print("read data: " + response)
numberOfLine = numberOfLine + 1
if (numberOfLine >= 5):
break
ser.close()
except Exception, e:
print "error communicating...: " + str(e)
else:
print "cannot open serial port "
and get results as on the screen
ATI
but I want to command be execute by ZigBee module, as like in shell commands. What am I doing wrong?
you need to append an end-of-line to your write()
ser.write("ATI\r\n")
you should change the timeout to:
ser.timeout = None
Otherwise readline() will return after 2 seconds, even if nothing has been read.

Categories

Resources