subprocess 命令行使用

import serial, threading
import time, sys, subprocess, os
import queue

class auto_run():
    # 命令的结束标志
    end_sign = 'END'
    def __init__(self):
        self.flag = True
        self.popen = None
        self.Retry_num = None
        self.Result_Pass = True
        self.t = None
        self.if_repeat = True
        self.recv_pop = False

        self.check_print_data = False
        self.check_deta = False
        self.result = queue.Queue()

    def send_cmd(self):
        with open("SAV526_Flash - RCY2.txt", "r", encoding="utf8") as f:
            send_List = f.readlines()
            print(send_List)
            max_ = len(send_List) - 1
            for index, cmd in enumerate(send_List):
                if index == 0: continue
                cmd = cmd.strip()
                cmd = cmd.replace(r"\u", "")
                cmd = cmd.replace(r"\n", "")
                print(">>%s:" % cmd)
                self.popen = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
                if self.check_print_data == False:
                    self.result.put(True)
                    threading.Thread(target=self.read_response).start()
                    self.check_print_data = True
                time.sleep(1)
            self.exec_command("")

    def read_response(self):
        while self.Result_Pass:  # time out
            line = self.popen.stdout.readlines()
            if line == []:
                time.sleep(0.2)
                self.check_deta = False
                threading.Thread(target=self.recv_timeout).start()
            else:
                for linestr in line:
                    self.check_deta = True
                    try:
                        print(linestr.decode("utf8").strip())
                        time.sleep(0.2)
                    except UnicodeDecodeError as e:
                        print(linestr.decode("gb2312").strip())
                        time.sleep(0.2)
            try:
                self.Result_Pass = self.result.get()
            except:
                self.Result_Pass = True

    def close(self):
        try:
            self.popen.kill()
            self.popen.stdin.close()
            self.popen.stdout.close()
        except:
            pass

    def exec_command(self, comm):
        try:
            comm += os.linesep
            self.popen.stdin.write(comm.encode('utf8'))
            self.popen.stdin.write(('echo ' + self.end_sign + os.linesep).encode('utf8'))
            self.popen.stdin.flush()
        except:
            pass

    def recv_timeout(self):
        try:
            print("输出无数据开始计时>>>>>>>>>> ")
            t = 0
            while t <= 10:
                t += 1
                time.sleep(1)
                if self.check_deta == False:
                    continue
                if self.check_deta == True:
                    break

            if t > 10:
                print("超时无信息退出>>>>>>>>>>>>>>> ")
                self.result.put(False)
            else:
                self.result.put(True)
        except:
            self.close()
            return


# strip()
if __name__ == '__main__':
    shell = auto_run()
    result = shell.send_cmd()



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

九点半9527

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值