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

三、使用场景

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

相关推荐
轩情吖4 小时前
MySQL之事务管理
android·后端·mysql·adb·事务·隔离性·原子性
赶路人儿1 天前
常见的mcp配置
android·adb
ego.iblacat1 天前
MySQL 数据库操作
数据库·mysql·adb
路溪非溪1 天前
adb的安装和基本使用总结
adb
XDHCOM1 天前
MySQL报错LDAP认证初始化连接池失败,远程修复思路和故障排查分享
数据库·mysql·adb
闻哥1 天前
深入理解 MySQL InnoDB Buffer Pool 的 LRU 冷热数据机制
android·java·jvm·spring boot·mysql·adb·面试
炸炸鱼.1 天前
MySQL 数据库核心操作手册
数据库·adb·oracle
总要冲动一次1 天前
MySQL 5.7 全量 + 增量备份方案(本地执行 + 远程存储)
数据库·mysql·adb
mldlds2 天前
mysql9.0windows安装
windows·adb
pangares2 天前
MySQL四种备份表的方式
mysql·adb·oracle