外接串口板,通过串口打开adb模式

一、依赖库

复制代码
import subprocess
import serial
from serial.tools import list_ports
import logging
import time

二、代码

python 复制代码
import subprocess

import serial
from serial.tools import list_ports
import logging
import time

def openAdb(com):
    # com = []
    # for i in list_ports.comports():
    #     if 'USB Serial' in i.description:
    #         print(i.description)
    #         com = i.name
    #         print(com)
    #         logging.debug(f'serial port:{com}')
    #         break

    print("try to open adb")

    ser = serial.Serial(com, 115200, timeout=0.1)
    if ser.is_open:
        ser.write(b"\n")
        time.sleep(1)
        ser.write(b"root\n")
        ser.write(b"\n")
        time.sleep(1)
        ser.write(b"Visteon@JMC23651\n")
        time.sleep(1)
        ser.write(b"dtach -a /tmp/glconsole\n")
        ser.write(b"su\n")
        ser.write(b"Vist@jmc789\n")
        time.sleep(1)
        ser.write(b"start setmode_device\n")
        time.sleep(1)
    ser.close()

def check_adb(com):
    count = 0
    while True:
        try:
            subprocess.run(["adb", "kill-server"], check=True)
            subprocess.run(["adb", "start-server"], check=True)

            output = subprocess.check_output("adb devices", shell=True).decode('utf-8')
            # print("Output of adb devices:", output.strip())
            devices = []
            lines = output.strip().split('\n')
            for line in lines[1:]:
                if '\tdevice' in line:
                    device_info = line.split('\t')[0]
                    devices.append(device_info)
            print(devices)

            if devices is not None and len(devices) > 0:
                print("Device found. Performing operations...")

                break
            else:
                print("Device not found. Switching modes and retrying...")
                openAdb(com)
                count += 1
                if count >= 5:
                    print("Please check if the environment is OK")
                    break

        except subprocess.CalledProcessError as e:
            print("An error occurred while executing adb command:", e)
            break

if __name__ == '__main__':
    com = 'COM24'
    # openAdb(com)
    check_adb(com)

三、使用场景

需要外接继电器,通过串口命令打开车机调试模式

相关推荐
@杰克成19 小时前
Java学习31
java·学习·adb
萑澈2 天前
如何在Rocky Linux 8单节点集群上安装Apache Hadoop
adb
禁默3 天前
解密 LangChain:LLM 应用开发的核心框架与“超级武器”
android·adb·langchain·vibe coding
Waay4 天前
MySQL基础高频考点
运维·mysql·adb
蜀道山老天师5 天前
从零搭建 Prometheus 监控 MySQL:含二进制安装、授权、exporter 配置全流程
运维·数据库·mysql·adb·云原生·prometheus
qq_297574676 天前
MySQL核心技术实战系列(第一篇):MySQL零基础入门:安装、配置与客户端工具使用 一、前言
数据库·mysql·adb
Languorous.6 天前
MySQL 零基础安装教程(Windows11/10,图文分步,新手零失败)
数据库·mysql·adb
huaiixinsi6 天前
Canal + Outbox、Kafka 选型与高可用、Caffeine 底层原理总结
java·数据库·分布式·mysql·spring·adb·kafka
Languorous.6 天前
MySQL 登录报错排查:1045、2003 错误,新手快速解决
数据库·mysql·adb
shaoming37766 天前
浏览器动作开发:地址栏图标点击事件、弹出页面设计
android·mysql·adb