完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
|
|
相关推荐
1个回答
|
|
测试环境
Window 10、Python 3.8.6 需求库 numpy pyserial 下载与Python版本匹配的pyserial与numpy模块,将其源文件复制至Python文件目录下,随后执行 ‘pip install 模块路径’指令(需要正确设置pip环境才可安装) 例程 例程 #创建日期:2020年10月10日 #版本:初版 #此程序对应北醒TF系列默认配置下串口版本有效 #此程序只提供参考和学习 # -*- coding: utf-8 -*- import serial.tools.list_ports import time import numpy as np ser = serial.Serial() ser.port = 'COM55' #设置端口 ser.baudrate = 115200 #设置雷达的波特率 def getTFminiData(): while True: count = ser.in_waiting #获取接收到的数据长度 if count > 8: recv = ser.read(9)#读取数据并将数据存入recv #print('get data from serial port:', recv) ser.reset_input_buffer()#清除输入缓冲区 if recv[0] == 0x59 and recv[1] == 0x59: # python3 distance = np.int16(recv[2] + np.int16(recv[3] << 8)) strength = recv[4] + recv[5] * 256 temp = (np.int16(recv[6] + np.int16(recv[7] << 8)))/8-256 #计算芯片温度 print('distance = %5d strengh = %5d temperature = %5d' % (distance, strength, temp)) ser.reset_input_buffer() if recv[0] == 'Y' and recv[1] == 'Y': # python2 //此处标示出文件读取成功 lowD = int(recv[2].encode('hex'), 16) highD = int(recv[3].encode('hex'), 16) lowS = int(recv[4].encode('hex'), 16) highS = int(recv[5].encode('hex'), 16) lowT = int(recv[6].encode('hex'), 16) highT = int(recv[7].encode('hex'), 16) distance = np.int16(lowD + np.int16(highD << 8)) strength = lowS + highS * 256 temp = (np.int16(lowD + np.int16(highD << 8)))/8-256 #计算芯片温度 print('distance = %5d strengh = %5d temperature = %5d' % (distance, strength, temp)) else: time.sleep(0.005) #50ms if __name__ == '__main__': try: if ser.is_open == False: try: ser.open() except: print('Open COM failed!') getTFminiData() except KeyboardInterrupt: # Ctrl+C 停止输出数据 if ser != None: ser.close() 输出如下: distance = 205 strengh = 5224 temperature = 52 distance = 205 strengh = 5231 temperature = 52 distance = 205 strengh = 5230 temperature = 52 distance = 205 strengh = 5218 temperature = 52 distance = 205 strengh = 5226 temperature = 52 distance = 205 strengh = 5221 temperature = 52 distance = 205 strengh = 5227 temperature = 52 distance = 205 strengh = 5224 temperature = 52 distance = 205 strengh = 5223 temperature = 52 distance = 205 strengh = 5223 temperature = 52 distance = 205 strengh = 5226 temperature = 52 distance = 205 strengh = 5225 temperature = 52 运行过程中可能存在的问题: 1、 Traceback (most recent call last): File "C:/Users/Administrator/AppData/Local/Programs/Python/Python37/test.py", line 1, in import serial.tools.list_ports ModuleNotFoundError: No module named 'serial' 解决办法:安装Python-serial 模块 2、 Traceback (most recent call last): File "C:/Users/Administrator/Desktop/test1.py", line 3, in import numpy as np ModuleNotFoundError: No module named 'numpy' 解决办法:安装numpy模块 3、 Traceback (most recent call last): File "C:/Users/Administrator/Desktop/test1.py", line 39, in getTFminiData() File "C:/Users/Administrator/Desktop/test1.py", line 11, in getTFminiData count = ser.in_waiting #获取接收到的数据长度 File "E:Pythonlibsite-packagesserialserialwin32.py", line 259, in in_waiting raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError())) serial.serialutil.SerialException: ClearCommError failed (OSError(9, '句柄无效。', None, 6)) 解决办法:设置端口名称为实验设备端口号 例:本次实验中接收端口号为COM55,则修改设置端口语句为ser.port = 'COM55' |
|
|
|
只有小组成员才能发言,加入小组>>
4714个成员聚集在这个小组
加入小组3402 浏览 0 评论
航顺(HK)联合电子发烧友推出“近距离体验高性能Cortex-M3,免费申请价值288元评估板
4317 浏览 1 评论
4356 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-2-12 17:17 , Processed in 0.618106 second(s), Total 43, Slave 37 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191