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

一、依赖库

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

二、代码

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)

三、使用场景

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值