跳到主要内容

Raspberry Pi · code

TF-Luna 接入 树莓派 UART · Raspberry Pi:开发示例

TF-Luna 接入 树莓派 的准备、连接、配置与代码要点。原始文件可在资料下载区获取。涉及接口:UART。

接口
UART
硬件目标
Raspberry Pi
整理日期
2026-08-31
资料说明
资料版本日期:2026-08-31。

适用范围

TF-Luna · 树莓派 · UART · Raspberry Pi

原始英文技术说明

# -- coding: utf-8 - # This script for Linux environment import time import serial

# Checked with TFLuna

# ser = serial.Serial("/dev/ttyUSB1", 115200)

ser = serial.Serial("/dev/ttyAMA0", 115200)

# ser = serial.Serial("/dev/tty0", 115200) # ser = serial.Serial("COM14", 115200)

# we define a new function that will get the data from LiDAR and publish it def readdata(): while True: counter = ser.inwaiting # count the number of bytes of the serial port if counter > 8: bytesserial = ser.read(9) ser.resetinput_buffer()

if bytesserial[0] == 0x59 and bytesserial[1] == 0x59: # python3 print("TF-Luna python3 portion") distance = bytesserial[2] + bytesserial[3]256 strength = bytesserial[4] + bytesserial[5]256 temperature = bytesserial[6] + bytesserial[7]*256 # For TFLuna temperature = (temperature/8) - 256 print("Distance:"+ str(distance)) print("Strength:" + str(strength)) if temperature != 0: print("Chip Temperature:" + str(temperature)) ser.resetinputbuffer()

if bytes_serial[0] == "Y" and bytes_serial[1] == "Y":
distL = int(bytes_serial[2].encode("hex"), 16)
distH = int(bytes_serial[3].encode("hex"), 16)
stL = int(bytes_serial[4].encode("hex"), 16)
stH = int(bytes_serial[5].encode("hex"), 16)
distance = distL + distH*256
strength = stL + stH*256
tempL = int(bytes_serial[6].encode("hex"), 16)
tempH = int(bytes_serial[7].encode("hex"), 16)
temperature = tempL + tempH*256
temperature = (temperature/8) - 256
print("TF-Luna python2 portion")
print("Distance:"+ str(distance) + "\n")
print("Strength:" + str(strength) + "\n")
print("Chip Temperature:" + str(temperature) + "\n")
ser.reset_input_buffer()

if name == "main": try: if ser.isOpen() == False: ser.open() read_data() except KeyboardInterrupt(): # ctrl + c in terminal. if ser != None: ser.close() print("program interrupted by the user")

使用说明

按原始资料可读取的技术内容编排;具体的产品硬件版本、飞控/软件版本和现场安全边界仍应以原始资料及目标平台当前文档为准。

资料与下载

产品咨询

咨询产品方案

留下需求,我们会尽快与您联系。

产品咨询

提交成功

已收到您的询盘,我们会尽快与您联系。