外接串口板,通过串口打开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)

三、使用场景

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

相关推荐
安得权5 小时前
Ubunut18.04 离线安装MySQL 5.7.35
数据库·mysql·adb
遇见火星2 天前
日常真实工作环境,Mysql常用操作命令,笔记!
android·mysql·adb·常用命令·mysql日志
Android 小码峰啊4 天前
Android Dagger 2 框架的注解模块深入剖析 (一)
android·adb·android studio·android-studio·androidx·android runtime
暗碳5 天前
adb检测不到原来的设备List of devices attached解决办法
adb
星尘库6 天前
数据库部署在服务器表不存在解决方案
adb
遥不可及zzz6 天前
Android 应用程序包的 adb 命令
android·adb
快来卷java7 天前
MySQL篇(一):慢查询定位及索引、B树相关知识详解
java·数据结构·b树·mysql·adb
jiet_h8 天前
Android adb 的功能和用法
android·adb
tpoog8 天前
[MySQL]数据类型
android·开发语言·数据库·mysql·算法·adb·贪心算法