I've connected an ultrasonic distance sensor to a raspberry pi 2. When triggered the sensor is supposed to send the following 6 parameters back via serial:
- frame header: 0xFF
- data 1: 0x07
- data 2: 0xD0
- data 3: 0x01
- data 4: 0x19
- checksum: 0xF0
However when I'm trying to read the output I get something like this: ['\x00\xff\x01V\x00\xce']
Here are some information about the serial frame format of the sensor:
The checksum is calculated like this:
SUM =( Frame header + Data_H+ Data_L+ Temp_H+ Temp_L)&0x00FF
Note: The checksum keeps the low accumulative value of 8 bits only;
And this is the code I've written so far:
import RPi.GPIO as GPIO
import time
from serial import Serial
GPIO.setmode(GPIO.BCM) #GPIO mode
GPIO_TRIGGER = 18 #assign GPIO pins
GPIO.setup(GPIO_TRIGGER, GPIO.OUT) #direction of GPIO-Pins (IN / OUT)
data_output=[]
def uss_funct():
ser = Serial('/dev/ttyAMA0', baudrate=9600, bytesize=8, parity='N', stopbits=1)
# set trigger HIGH, sensor is waiting for falling edge
time.sleep(0.01000)
GPIO.output(GPIO_TRIGGER, True)
# set trigger LOW after 10ms -> Falling Edge
time.sleep(0.01000)
GPIO.output(GPIO_TRIGGER, False)
# set trigger back HIGH after 2ms, as LOW is supposed to be between 0.1-10ms
time.sleep(0.00200)
GPIO.output(GPIO_TRIGGER, True)
#read from rx
data_output.append(ser.read(6))
ser.close()
#clean up GPIO pins
GPIO.cleanup()
print (data_output)
if __name__ == '__main__':
uss_funct()