【问题标题】:Capture ANSI escape sequence response using Micropython ESP8266使用 Micropython ESP8266 捕获 ANSI 转义序列响应
【发布时间】:2021-06-30 09:22:29
【问题描述】:

我正在使用带有 Micropython 的 ESP8266 与使用 Putty 的串行终端进行通信。使用这些问题中引用的转义序列:

Read ANSI escape from terminal

How do I determine size of ANSI terminal?

转义序列列表https://vt100.net/docs/vt100-ug/chapter3.html

我正在尝试读回一个转义序列,它是对找到光标位置的回复。 我可以在终端中看到响应,但我需要捕获它以便解析它。我熟悉 Python,但对 Micropython 和 ESP8266 (ESP01) 完全陌生。我正在使用 webrepl 上传代码。我看到了这个,我不确定它是否相关:https://forum.micropython.org/viewtopic.php?t=5359

我尝试在未连接 webrepl 的情况下运行我的代码,但仍然无法正常工作。

我做错了什么,如何捕获响应?

根据终端大小,响应应该如下所示,即 ; 之前的内容。将不可见。 ^[[45;157R

#import esp
import network
import machine
from machine import UART

uart = UART(0, 115200)

x=uart.write("\033[500;500H\033[6n")
if uart.any():
  print(":> ", uart.read())

编辑:来自此链接的尝试示例https://forum.micropython.org/viewtopic.php?t=5359#p30867

也没有运气(是的,我知道它会阻塞)。在这里,如果我输入字母“R”,它会退出循环,但不会捕获转义序列的输出。

from machine import UART
import machine
import network
import uos

uos.dupterm(None, 1)
uart = UART(0, 115200)

charbuf = None

uart.write("\033[500;500H\033[6n")

while charbuf != b"R":
  if uart.any():
    charbuf = uart.read()
    uart.write(charbuf)
uos.dupterm(UART(0, 115200), 1)

【问题讨论】:

    标签: esp8266 micropython


    【解决方案1】:

    解决这个问题是一场噩梦,WebRepl 控制台复制一直是我存在的祸根,可能还有许多其他人。我尝试了多种 read() 方法,但都失败了。

    uart.read()
    sys.stdin.read()
    sys.stdin.buffer.read()
    

    这些是为 webrepl 启用/禁用控制台复制的组合。 最终对我有用的咒语如下。有关详细信息,请参阅内联 cmets。

    import machine
    import network
    #import sys
    import uos
    from machine import UART
    
    uos.dupterm(None, 1)  # Disable console duplication for Webrepl
    uart = UART(0, 115200, timeout=100, timeout_char=100)  #Instantiate uart, increase timeout for good measure
    uart.write("\033[2J\033[1;1f") # Clear screen
    uart.write("\033[500;500H\033[6n") # Move cursor to bottom right, send query.
    
    # Wait for uart to collect response. This will block further execution until we get a reply.
    while True:
      if uart.any() != 0:
        break
    
    uos.dupterm(UART(0, 115200), 1)  # Re-enable console redirection so print() will work
    
    # Read the uart buffer with response, convert from binary to string, replace ESC character.
    # First ^ gets eaten somewhere!
    buf = str(uart.read(), 'UTF-8').replace('\x1b', '^^[')
    
    if len(buf) > 0:  # If response isn't empty, print buffer
      print(buf)
    

    这是输出:

    ^[[43;157R
    

    【讨论】:

      猜你喜欢
      • 2021-11-17
      • 1970-01-01
      • 2020-04-23
      • 1970-01-01
      • 2011-05-27
      • 1970-01-01
      • 2012-04-14
      • 2010-11-25
      • 2011-06-18
      相关资源
      最近更新 更多