I am using a BananaPi to read UART data coming from a device. I know for sure what is the data generated by the device, it's the same 40 byte sequence sent in a loop, I know for sure because I mesure it with an oscilloscope. The data sent looks like(on the oscilloscope): 6A000496ED47.... 6A000496ED47....
I want to read this data in the BananaPi and I write a python script that looks like:
import serial
import time
if __name__ == '__main__':
connection = serial.Serial()
connection.port = "/dev/ttyS2"
connection.baudrate = 4000000
connection.timeout = 1
try:
connection.open()
print("serial port open")
idx = 0
while(idx < 10):
data = connection.read(size=40)
newData = data.encode('hex')
print newData
idx += 1
connection.close()
except:
print("Opening serial error")
The output is:
b777fbdc63e76....
So I get different sets of 40 bytes data on every of the 10 while loops.
Any ideea what might be wrong?
The serial setup is the same as in the oscilloscope meaning the baudrate is 4000000
Related
I'm reading data in from a machine to windows 7. Using python, I read the serial port, process the data then write the data to a different serial port. Using com0com null modem emulator, the data is sent to another program. Here is the code I'm using:
import serial
import time
ser = serial.Serial(port='COM7', baudrate=9600)
ser2 = serial.Serial(port='COM8', baudrate=9600)
value_one = None
while (True):
# Check if incoming bytes are waiting to be read from the serial input
# buffer.
# NB: for PySerial v3.0 or later, use property `in_waiting` instead of
# function `inWaiting()` below!
if (ser.in_waiting > 16):
# read the bytes and convert from binary array to ASCII
data_str = ser.read(ser.in_waiting).decode('ascii')
if (value_one == None):
time.sleep(1)
print(data_str)
value_one_parse = data_str[7:9]
print(value_one_parse)
value_one = float(value_one_parse)
print(value_one)
else:
time.sleep(1)
print(data_str)
value_two_parse = data_str[7:9]
print(value_two_parse)
value_two = float(value_two_parse)
print(value_two)
avg = ((value_one + value_two)/2)
print(avg)
avgprep = str(avg) + '\r\n'
print(avgprep)
ser2.write(avgprep.encode('utf-8'))
value_one = None
value_two = None
time.sleep(0.01)
So if avgprep = 71.1, why am I only receiving the first digit 7 to the program?
I changed ser.in_waiting > 16 to ser.in_waiting > 0 and put a time.sleep(5) after that.
I am using 2 XBee pro S1, I want to read the packets received by the co-ordinator on my PC , it is enabled with API_2 and all other connections are done properly, I can see the packets with XCTU, I am using the python xbee library , but it gives no output :
The Code :
import serial.tools.list_ports
from xbee import XBee
import serial
ports = list(serial.tools.list_ports.comports())
for p in ports: #print the list of ports
print p
def toHex(s):
lst = []
for ch in s:
hv = hex(ord(ch)).replace('0x', '')
if len(hv) == 1:
hv = '0'+hv
hv = '0x' + hv
lst.append(hv)
def decodeReceivedFrame(data):
source_addr_long = toHex(data['source_addr_long'])
source_addr = toHex(data['source_addr'])
id = data['id']
samples = data['samples']
options = toHex(data['options'])
return [source_addr_long, source_addr, id, samples]
PORT = '/dev/ttyUSB0'
BAUD_RATE = 9600
ser = serial.Serial(PORT, BAUD_RATE)
print "Serial ports initialised...."
xbee = XBee(ser,escaped=True)
print "XBee object created"
while True:
try:
response = xbee.wait_read_frame()
sleep(0.5)
decodedData = decodeReceivedFrame(response)
print decodedData
print "data decoded"
except KeyboardInterrupt:
break
ser.close()
The port number and baudrate are connect, I change it to the appropriate portnumber every time I replug the coordinator to my PC.
My output looks like :
Serial ports initialised....
XBee object created
It stays like that and gives no output, even if I see the RX led blinking.
Below is the code written with only pyserial :
import serial
from time import sleep
port = '/dev/ttyUSB0'
baud = 9600
ser = serial.Serial(port, baud)
data = ""
while True:
try:
while ser.in_waiting:
sleep(1)
data = ser.read()
print data
except KeyboardInterrupt:
break
ser.close()
It gives the following output.
Could someone kindly help.
Are you sure you have the correct serial port and baud rate? Does the xbee package support API mode 2? It might only work with API mode 1.
Does that package have methods for accessing the raw byte stream instead of trying to read frames? Can you configure it to throw exceptions on parsing errors?
I would start with just printing response until you see that you're receiving data. And why include the sleep() call in that loop?
I'm not sure what you're trying to accomplish in toHex() but you might want to look at the Python method struct.unpack() or replace all of the work you do on hv with '0x%02X' % ord(ch).
Hi I'm working with Python 2 and I'm trying to receive data from an Arduino via Serial Port.
I'm using PySerial to collect the data but I get it chopped and I can't use readline. The size of the array send via serial changes along time.
When I put a very large value to read, it doesn't return a value. I think it keeps waiting until that number of bytes is received.
I would like to know if is a way to get the array complete each time I execute the script.
The code:
class SerialReceiver(serial.Serial):
def __init__(self, portName):
super(SerialReceiver, self).__init__()
self.port = portName
self.baudrate = 115200
self.timeout = None
self.xonxoff = 1
try:
self.open()
except serial.SerialException:
sys.stderr.write("Could not open serial port %s\n" % (portName))
sys.exit(1)
if __name__ == '__main__':
serialReceiver = SerialReceiver('COM3')
while True:
sended = raw_input("Pulsar 'O' para recibir data:")
serialReceiver.write(sended)
data = serialReceiver.read(2048*2)
print data
I have an Arduino that reports time (in seconds), voltage, current and joules ever 60 seconds. In the serial monitor like this:
time,voltage,current,joules
60,1.45,0.39,0.57
120,1.45,0.39,1.13
180,1.45,0.39,1.70
240,1.45,0.39,2.26
...
However the following python script I don't get this result:
import serial
ser = serial.Serial('COM5', 9600)
logfile = open("batterytest.log", 'w')
while True:
if ser.readline() == b'Test Complete!':
logfile.close()
exit()
logfile.write(ser.readline().decode("utf-8"))
logfile.flush()
Instead I see results every 120 seconds:
time,voltage,current,joules
120,1.13,0.02,0.05
240,1.13,0.02,0.09
360,1.13,0.02,0.14
480,1.13,0.02,0.19
....
Looks like it may miss the in-between data point due to some timing issue. You can try to use putty to see if your arduino in fact output the right data points.
For your PySerial program, I would add a variable "data" to store your serial readline first, then perform your logic on it.
import serial
ser = serial.Serial('COM5', 9600)
logfile = open("batterytest.log", 'w')
while True:
data = ser.readline()
if data == b'Test Complete!':
logfile.close()
exit()
logfile.write(data.decode("utf-8"))
logfile.flush()
Also, depending on your Arduino output timing, you may consider adding a timeout value for your serial read by:
ser = serial.Serial('COM5', 9600, timeout = 1 )
# Here the time out is 1 second
I am aiming to write a code that will be indefinitely listening and reading from to a serial port that will have this output produced every few seconds
serial port output:
aaaa::abcd:0:0:0
//printf("%d\n",data[0]);
2387
//printf("%d\n",data[1]);
14
-9
244
-44
108
I want the data to be appended in a list like this, python supposed output
[abcd::abcd:0:0:0, 2387, 14, -9, 244, -44, 108]
I tried this code amongst many others but nothing worked, I keep on getting no output
EDIT- the code below gives me this output
'''[['abcd::', 'abcd::', 'abcd::', 'abcd::', 'abcd::']] #or
[['abcd::abcd:0:0:c9\n', '2406\n', '14\n', '-7\n']] # and so on, different output for each iteration'''
#[['aaaa::c30c:0:0:c9\n', '2462\n', '11\n', '-9\n', '242\n', '-45\n', '106\n']] apparently it worked only once.
ser = serial.Serial('/dev/ttyUSB1',115200, timeout=10)
print ser.name
while True:
data = []
data.append(ser.readlines())
print data
# further processing
# send the data somewhere else etc
print data
ser.close()
readline will keep reading data until read a terminator(new line). please try: read.
UPDATED:
use picocom -b 115200 /dev/ttyUSB0 or putty(serial model) to detect the port and baud-rate is right. I got two different ports in your two questions.if open error port, read() will keep waiting until read a byte. like this:
import serial
# windows 7
ser = serial.Serial()
ser.port = 'COM1'
ser.open()
ser.read() # COM1 has no data, read keep waiting until read one byte.
if you type this code in console, console will no output like this:
>>> import serial
>>> ser = serial.Serial()
>>> ser.port = 'COM1'
>>> ser.open()
>>> ser.read()
_
we need add timeout for read to fix it.
you can try this:
import serial
import time
z1baudrate = 115200
z1port = '/dev/ttyUSB0' # set the correct port before run it
z1serial = serial.Serial(port=z1port, baudrate=z1baudrate)
z1serial.timeout = 2 # set read timeout
# print z1serial # debug serial.
print z1serial.is_open # True for opened
if z1serial.is_open:
while True:
size = z1serial.inWaiting()
if size:
data = z1serial.read(size)
print data
else:
print 'no data'
time.sleep(1)
else:
print 'z1serial not open'
# z1serial.close() # close z1serial if z1serial is open.