读取设备状态报告 ANSI 转义序列回复 [英] Reading the Device Status Report ANSI escape sequence reply

查看:23
本文介绍了读取设备状态报告 ANSI 转义序列回复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码检索 VT100 终端中光标的坐标:

I'm trying to retrieve the coordinates of cursor in a VT100 terminal using the following code:

void getCursor(int* x, int* y) {
  printf("33[6n");
   scanf("33[%d;%dR", x, y);
}

我正在使用以下 ANSI 转义序列:

I'm using the following ANSI escape sequence:

设备状态报告 - ESC[6n

Device Status Report - ESC[6n

将光标位置报告给应用程序(就像在键盘上输入一样)ESC[n;mR,其中 n 是行,m 是列.

Reports the cursor position to the application as (as though typed at the keyboard) ESC[n;mR, where n is the row and m is the column.

代码编译并发送 ANSI 序列,但是,在接收到它时,终端将 ^[[x;yR 字符串打印到 stdout 而不是 stdin 使我无法从程序中检索它:

The code compiles and the ANSI sequence is sent, but, upon receiving it, the terminal prints the ^[[x;yR string to the stdout instead of stdin making it imposible for me to retrieve it from the program:

显然,该字符串是为程序指定的,所以我一定是做错了什么.有人知道是什么吗?

Clearly, the string is designated for the program, though, so I must be doing something incorrectly. Does anybody know what it is?

推荐答案

您的程序正在运行,但正在等待 EOL 字符.

Your program is working but is waiting for an EOL character.

scanf 是面向行的,因此它在处理之前等待新行.尝试运行您的程序,然后按 Enter 键.

scanf is line oriented so it waits for a new line before processing. Try running your program and then hit the enter key.

解决方案是使用其他不需要换行的东西来读取输入,然后使用 sscanf 解析出值.

The solution is to use something else that doesn't need a new line to read the input and then use sscanf to parse the values out.

您还需要使 stdin 非阻塞,否则在缓冲区已满或 stdin 关闭之前您将无法获得输入.看到这个问题使标准输入非阻塞

You will also need to make stdin non-blocking or you won't get the input until the buffer is full or stdin is closed. See this question Making stdin non-blocking

您还应该在 printf 之后调用 fflush(stdout); 以确保它确实被写入(printf 通常是行缓冲的,因此如果没有换行符,它可能不会刷新缓冲区).

You should also call fflush(stdout); after your printf to ensure it is actually written (printf is often line buffered so without a newline it may not flush the buffer).

这篇关于读取设备状态报告 ANSI 转义序列回复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆