Is there a way to use pyserial to read data from an infrared obstacle avoidance module?

51 Views Asked by At

I'm currently using Keyes infrared obstacle avoidance module connected to my laptop via PL2303 TA, and I'm using pyserial to read the data. Interestingly, when I pass an object in front of the module, the program successfully retrieves data (the detection light also illuminates).

However, when I place an object in front (without moving it), the detection light remains continuously on. Strangely, the data is only obtained while the object is in the process of becoming completely still. After the object is completely still, no further data is received. Does anyone know how to achieve a continuous data stream even when the detection light remains constantly on?

This is the infrared obstacle avoidance module and PL2303 TA that I'm using.

My python code :

import serial
import time
ser = serial.Serial('COM3',9600)
if ser.is_open:
    try:
        while True:
            size = ser.inWaiting()
            if size:
                data = ser.read(size).hex()
                print(len(data))
                print('get data')
            else:
                print("no data")
            time.sleep(0.3)
    except:
        ser.close()
else:
    print("not open serial")

And I have also tried using a Raspberry Pi, and its GPIO module indeed manages to continuously retrieve data. Here is my python code for the Raspberry:

import RPi.GPIO as GPIO
import time

pin_avoid_obstacle = 17
GPIO.setmode(GPIO.BCM)
GPIO.setup(pin_avoid_obstacle, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)
try:
    while True:
            status = GPIO.input(pin_avoid_obstacle)
            if status == 1:
                print('not found')
            else:
                print('found')
            time.sleep(0.5)
except:
    GPIO.cleanup()

Sorry for my bad english.

0

There are 0 best solutions below