发布于2025-01-04 12:57 阅读(958) 评论(0) 点赞(26) 收藏(0)
I'm trying to use the pySerial library to access an FPV controller through the COM
port. I have this code:
port = '/dev/cu.usbmodem0x80000001'
baudrate = 115200
import serial
import time
def read_vtxtable():
try:
ser = serial.Serial(port, baudrate, timeout=5, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS)
time.sleep(2)
except serial.SerialException as e:
print(f"Cant open this port {port}: {e}")
return []
# Trying to initialize process and make controller alive
ser.write('status\n'.encode())
time.sleep(0.05)
# Asking controller about vtx data
ser.write('vtxtable\n'.encode())
time.sleep(0.05)
response = ""
start_time = time.time()
while time.time() - start_time < 5:
if ser.in_waiting > 0:
response += ser.read(ser.in_waiting).decode()
time.sleep(0.05)
ser.close()
return response
vtxtable_data = read_vtxtable()
print(vtxtable_data)
When I try this code, the connection appears to be successful, but when I request some data, the response is empty.
Interestingly, after I run some commands in the BetaFlight CLI, and disconnect to release port access, my own code starts working. However, if I unplug the USB and plug it back in again, the code stops working again.
Could it be that the code fails to "wake up" the controller, such that it ignores the commands? It seems like the BetaFlight CLI is able to do this, if so.
How can I get a non-empty response from the connection?
UPDATED Below I have given the main idea of what I changed in the code
Code
....
cli_prompt = "Entering CLI Mode, type 'exit' to return, or 'help'"
timeout_time = time.time() + timeout
while time.time() < timeout_time:
write('#')
if ser.in_waiting > 0:
buffer += ser.read(ser.in_waiting).decode()
if cli_prompt in buffer:
print("# Found wakeup phrase:", cli_prompt)
return True
else:
print("# No response..")
write('status')
time.sleep(0.1)
output = get_info()
if len(output) > 0:
print("# The board is alive")
override_wakeup_detection = True
return True
else:
write('#')
time.sleep(0.1)
Output.
% python3 get_data.py
# Using first usbmodem found: /dev/cu.usbmodem0x80000001
# No response..
# No response..
# No response..
# The board is alive
[
'vtxtable',
'vtxtable bands 8',
'vtxtable channels 8',
'vtxtable ...
]
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/2046716/40022bd7b617bc11c9d7/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!