0

I have attached a pulse oximeter (Nonin XPOD) to a Raspberry Pi zero UART.

According to the Nonin manual, I should get 5 bytes/75 times per second.

The code I used and resulting data is shown below.

Notice how I get a variable number of characters per read (e.g. b'\x84' versus b'~')

What might be the reason for this varying size of characters? Perhaps the Raspberry Pi cannot read 5 bytes/75 times per second?

My code:

import serial
sensor = serial.Serial('/dev/ttyS0',timeout=1,baudrate=9600, parity=serial.PARITY_NONE,stopbits=serial.STOPBITS_ONE,bytesize=serial.EIGHTBITS)
while True:
    rcv = sensor.read()
    print ("You read: ", rcv)

Results:

You read:  b'\x84'
You read:  b'\x98'
You read:  b'\xa1'
You read:  b'\xc6'
You read:  b'\x7f'
You read:  b'~'
You read:  b'\x98'
You read:  b'\xa1'
You read:  b'\xf7'
You read:  b'\x7f'
You read:  b'\xaf'
You read:  b'\x98'
You read:  b'\x9d'
You read:  b'\xf2'
You read:  b'\x00'
You read:  b"'"
You read:  b'\x98'
halfer
  • 19,824
  • 17
  • 99
  • 186
BobGaines
  • 45
  • 5
  • `print` slow down the execution of the code. rethink a test to check the readings by adding the results to a list and printing them later. or even better, increase a counter every time you get a value, close the execution when you reach 75 and evaluate how long it took – Wippo Dec 08 '20 at 21:38
  • if performance is very important for your project, since you're working on a raspberry, consider switching to C or C++: it's well known that python code execution is slower than the other two. [Is Python faster and lighter than C++?](https://stackoverflow.com/questions/801657/is-python-faster-and-lighter-than-c) – Wippo Dec 08 '20 at 21:41
  • Thank you all for the ideas. Slowly down the code helped. I think I am on to something but do not know what to do with it. I see what I think are hex characters (e.g. I get a b'\x7F') and then sometimes I get a single character (e.g. b'M'). So the 7F must be hexadecimal and the 'M' might be an ASCII version instead of showing me Hex. I am going to look for a way to convert both in python to an integer. – BobGaines Dec 08 '20 at 23:11
  • If you are supposed to get 5 bytes 75 times a second, you could try `sensor.read(5)` – Mark Setchell Dec 11 '20 at 10:52

1 Answers1

0

I figured it out. What is being read by my code from the Raspberry Pi UART comes in differently depending on the byte. There seems to be 3 formats: (1) b'\xHH' where HH are two hexadecimal characters to form 8 bits (2) b'A' where A is a character from the ASCII table - decode to get 8 bits (3) b'\' I do not understand this yet. Going to post answer question on it if I cannot figure it out.

BobGaines
  • 45
  • 5