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

三、使用场景

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

相关推荐
她叫我大水龙9 小时前
ADB 入门指南:安装与常用命令速查手册
adb
wb0430720116 小时前
仓库搬家不停业——从阿明的“在线换仓库“,看数据库迁移与 Schema 演进的实战方法论
数据库·adb·架构
Mr -老鬼18 小时前
EasyClick 入门指南:Shell 命令与 ADB 完全指南
android·adb·自动化·shell·easyclick·易点云测
feifeigo12318 小时前
C# ADB 安卓设备数据传输工具
android·adb·c#
2301_7736436219 小时前
mysql5.7稳定版使用
adb
ULIi096kr1 天前
MySQL解决Too many connections报错:连接数爆满排查、优化与永久解决方案
数据库·mysql·adb
ha_lydms1 天前
AnalyticDB基本概念
mysql·adb·analyticdb
会Tk矩阵群控的小木2 天前
小红书矩阵软件:基于Python+ADB的多设备批量管理自动化脚本实战
运维·python·adb·矩阵·自动化·新媒体运营·个人开发
IT界的老黄牛2 天前
手机 Chrome 远程调试实战:adb + DevTools,localhost 就是你的测试服
chrome·测试工具·adb
会Tk矩阵群控的小木2 天前
安卓群控系统对于游戏工作室实战教程
android·运维·游戏·adb·开源软件·个人开发