如何确定是否从Linux命令行打开LCD监视器

时间:2023-01-24 13:14:57

How do you tell if a computer's monitor(s) are turned on/off from the command line in Linux? I've traditionally thought of monitors as output-only devices, but I've noticed the Gnome Monitor Preferences dialog has a "detect monitor" function. Can this be generalized to determine if a monitor is physically turned off?

您如何判断一个计算机的监视器是否从Linux的命令行打开/关闭?我传统上认为监视器是只输出的设备,但是我注意到Gnome Monitor首选项对话框有一个“检测监视器”功能。是否可以将其概括为确定监视器是否在物理上被关闭?

7 个解决方案

#1


13  

The VESA DDC connection is an I2C connection that can be used to query the presence of the monitor.

VESA DDC连接是一个I2C连接,可以用来查询监视器的存在。

Linux exposes the I2C device and userland programs can communicate directly with the monitor with code such as that at http://jaffar.cs.msu.su/oleg/ddcci/

Linux公开了I2C设备,用户界面程序可以直接与监视器通信,代码如http://jaffar.cs.msu.su/oleg/ddcci/

Notice this below: Control 0xe1: +/1/1 [SAM: Power control (0 - off/1 - on)]

注意:控制0xe1: +/1/1 [SAM:电源控制(0 - off/1 - on)]

# ddcci-tool /dev/i2c-2 -e -c -d


ddcci-tool version 0.03

Reading EDID : 0x50@/dev/i2c-2
    Plug and Play ID: SAM00BA
    Input type: Analog

Using ddc/ci : 0x37@/dev/i2c-2

Capabilities:
(type(LCD)vcp(04 05 10 12 60(1 3) B0(1 2) B6 C6 C8 C9 D6(1 4) DC(1 2 3 4) DF))

Controls (valid/current/max):
Control 0x04: +/0/1 [Reset Factory Defaults]
Control 0x05: +/0/1 [SAM: Reset Brightness and Contrast]
Control 0x06: +/0/1 [Reset Factory Geometry]
Control 0x08: +/0/1 [Reset Factory Default Color]
Control 0x0e: +/60/120 [SAM: Image Lock Coarse]
Control 0x10: +/0/100 [Brightness]
Control 0x12: +/50/100 [Contrast]
Control 0x16: +/8/16 [Red Video Gain]
Control 0x18: +/8/16 [Green Video Gain]
Control 0x1a: +/8/16 [Blue Video Gain]
Control 0x1e: +/0/2 [SAM: Auto Size Center]
Control 0x20: +/50/100 [Horizontal Position]
Control 0x30: +/25/54 [Vertical Position]
Control 0x3e: +/39/50 [SAM: Image Lock Fine]
Control 0x60: +/1/3 [Input Source Select]
Control 0x62: +/0/100 [Audio Speaker Volume Adjust]
Control 0x6c: +/140/255 [Red Video Black Level]
Control 0x6e: +/127/255 [Green Video Black Level]
Control 0x70: +/121/255 [Blue Video Black Level]
Control 0xb0: +/0/2 [Settings]
Control 0xb6: +/3/8 [???]
Control 0xc6: +/1/1 [???]
Control 0xc8: +/5/16 [???]
Control 0xc9: +/1/0 [???]
Control 0xca: +/2/2 [On Screen Display]
Control 0xcc: +/2/11 [SAM: On Screen Display Language]
Control 0xd6: +/1/4 [SAM: DPMS control (1 - on/4 - stby)]
Control 0xdc: +/4/4 [SAM: MagicBright (1 - text/2 - internet/3 - entertain/4 - custom)]
Control 0xdf: +/512/0 [VCP Version]
Control 0xe0: +/0/2 [SAM: Color preset (0 - normal/1 - warm/2 - cool)]
Control 0xe1: +/1/1 [SAM: Power control (0 - off/1 - on)]
Control 0xe2: +/0/1 [???]
Control 0xed: +/108/255 [SAM: Red Video Black Level]
Control 0xee: +/101/255 [SAM: Green Video Black Level]
Control 0xef: +/103/255 [SAM: Blue Video Black Level]

An interesting question is whether or not your monitor returns that piece of data, and if not, whether it responds at all if it's currently turned off.

一个有趣的问题是,您的监视器是否返回该数据片段,如果不返回,如果它当前处于关闭状态,它是否会响应。

#2


8  

From systembash.com, here is the code taken from the link, in case it will be down some day:

从systembash.com中,这里是从链接中获取的代码,以防有朝一日它会被删除:

#!/bin/bash
export DISPLAY=:0.0

if [ $# -eq 0 ]; then
  echo usage: $(basename $0) "on|off|status"
  exit 1
fi

if [ $1 = "off" ]; then
  echo -en "Turning monitor off..."
  xset dpms force off
  echo -en "done.\nCheck:"
  xset -q|grep "Monitor is"
elif [ $1 = "on" ]; then
  echo -en "Turning monitor on..."
  xset dpms force on
  echo -en "done.\nCheck:"
  xset -q|grep "Monitor is"
elif [ $1 = "status" ]; then
  xset -q|sed -ne 's/^[ ]*Monitor is //p'
else 
  echo usage: $(basename $0) "on|off|status"
fi

#3


5  

You can get some info using the xrandr command-line utility, if your video driver supports this extension.

如果您的视频驱动程序支持此扩展,您可以使用xrandr命令行实用程序获得一些信息。

#4


3  

Not all monitors support vesa DDC. Thing might got even more complicated if you use a dock.

不是所有的监视器都支持vesa DDC。如果你使用码头,事情可能会变得更复杂。

On the other hand, there is a way to check whether your actions are detected by monitoring the kernel/udev events. To do this, for Fedora and RHEL, type following command:

另一方面,有一种方法可以检查您的操作是否通过监视内核/udev事件来检测。为此,对于Fedora和RHEL,输入以下命令:

sudo udevadm monitor --property

It will display every kernel and udev events it detected. From that, you can try plug/unplug the monitor data cable; plug/unplug the monitor power cable; toggle the stand-by/on states by pressing the power button.

它将显示它检测到的每个内核和udev事件。由此,您可以尝试插入/拔出监控数据电缆;插入/拔出监控电源电缆;按下电源按钮以切换待机状态。

If no output is generated after an action, then your system cannot detect it.

如果在操作之后没有生成输出,那么系统无法检测到它。

#5


2  

When using xset it always returns xset: unable to open display ""

当使用xset时,它总是返回xset:无法打开显示

However, "xset dpms force off" & "xset dpms force off" commands actually turn my monitor off and on. I am using the script outlined here -

然而,“xset dpms force off”和“xset dpms force off”命令实际上会关闭和打开我的监视器。我正在使用这里列出的脚本

http://systembash.com/content/how-to-turn-off-your-monitor-via-command-line-in-ubuntu/

http://systembash.com/content/how-to-turn-off-your-monitor-via-command-line-in-ubuntu/

#6


1  

You might want to look at the output of

你可能想看看输出

$ xset -q

I'm not sure if it will work but I think the line " Monitor is (on|off)" should tell you the answer.

我不确定它是否可行,但我认为“监视器”(on|off)应该告诉你答案。

#7


1  

xset -qis the way to go for a raspberry pi. A check to see if the reply contains 'Monitor is On' is a great way to use a gpio pin to turn off an LCD Backlight;

xset -qis the way to go for a raspberry pi。查看回复中是否包含“Monitor is On”是使用gpio引脚关闭LCD背光的好方法;

if(runOSCommand("xset -q").contains("Monitor is On")){
            System.out.println("Monitor is On");
            if screenLight.isHigh()) {
                screenLight.low();
            }
        }else{
            System.out.println("Monitor is Off");
            if (screenLight.isLow()) {
                screenLight.high();
            }
        }


     public static String runOSCommand(String command){
     String s = null;
     String string = "";
     Process p;
     try {
         p = Runtime.getRuntime().exec(command);
         BufferedReader br = new BufferedReader(
             new InputStreamReader(p.getInputStream()));
         while ((s = br.readLine()) != null){
 //            System.out.println("line: " + s);
             string += s;
         }
         p.waitFor();
//         System.out.println ("exit: " + p.exitValue());
         p.destroy();
     } catch (Exception e) {}
     return string;
 }

#1


13  

The VESA DDC connection is an I2C connection that can be used to query the presence of the monitor.

VESA DDC连接是一个I2C连接,可以用来查询监视器的存在。

Linux exposes the I2C device and userland programs can communicate directly with the monitor with code such as that at http://jaffar.cs.msu.su/oleg/ddcci/

Linux公开了I2C设备,用户界面程序可以直接与监视器通信,代码如http://jaffar.cs.msu.su/oleg/ddcci/

Notice this below: Control 0xe1: +/1/1 [SAM: Power control (0 - off/1 - on)]

注意:控制0xe1: +/1/1 [SAM:电源控制(0 - off/1 - on)]

# ddcci-tool /dev/i2c-2 -e -c -d


ddcci-tool version 0.03

Reading EDID : 0x50@/dev/i2c-2
    Plug and Play ID: SAM00BA
    Input type: Analog

Using ddc/ci : 0x37@/dev/i2c-2

Capabilities:
(type(LCD)vcp(04 05 10 12 60(1 3) B0(1 2) B6 C6 C8 C9 D6(1 4) DC(1 2 3 4) DF))

Controls (valid/current/max):
Control 0x04: +/0/1 [Reset Factory Defaults]
Control 0x05: +/0/1 [SAM: Reset Brightness and Contrast]
Control 0x06: +/0/1 [Reset Factory Geometry]
Control 0x08: +/0/1 [Reset Factory Default Color]
Control 0x0e: +/60/120 [SAM: Image Lock Coarse]
Control 0x10: +/0/100 [Brightness]
Control 0x12: +/50/100 [Contrast]
Control 0x16: +/8/16 [Red Video Gain]
Control 0x18: +/8/16 [Green Video Gain]
Control 0x1a: +/8/16 [Blue Video Gain]
Control 0x1e: +/0/2 [SAM: Auto Size Center]
Control 0x20: +/50/100 [Horizontal Position]
Control 0x30: +/25/54 [Vertical Position]
Control 0x3e: +/39/50 [SAM: Image Lock Fine]
Control 0x60: +/1/3 [Input Source Select]
Control 0x62: +/0/100 [Audio Speaker Volume Adjust]
Control 0x6c: +/140/255 [Red Video Black Level]
Control 0x6e: +/127/255 [Green Video Black Level]
Control 0x70: +/121/255 [Blue Video Black Level]
Control 0xb0: +/0/2 [Settings]
Control 0xb6: +/3/8 [???]
Control 0xc6: +/1/1 [???]
Control 0xc8: +/5/16 [???]
Control 0xc9: +/1/0 [???]
Control 0xca: +/2/2 [On Screen Display]
Control 0xcc: +/2/11 [SAM: On Screen Display Language]
Control 0xd6: +/1/4 [SAM: DPMS control (1 - on/4 - stby)]
Control 0xdc: +/4/4 [SAM: MagicBright (1 - text/2 - internet/3 - entertain/4 - custom)]
Control 0xdf: +/512/0 [VCP Version]
Control 0xe0: +/0/2 [SAM: Color preset (0 - normal/1 - warm/2 - cool)]
Control 0xe1: +/1/1 [SAM: Power control (0 - off/1 - on)]
Control 0xe2: +/0/1 [???]
Control 0xed: +/108/255 [SAM: Red Video Black Level]
Control 0xee: +/101/255 [SAM: Green Video Black Level]
Control 0xef: +/103/255 [SAM: Blue Video Black Level]

An interesting question is whether or not your monitor returns that piece of data, and if not, whether it responds at all if it's currently turned off.

一个有趣的问题是,您的监视器是否返回该数据片段,如果不返回,如果它当前处于关闭状态,它是否会响应。

#2


8  

From systembash.com, here is the code taken from the link, in case it will be down some day:

从systembash.com中,这里是从链接中获取的代码,以防有朝一日它会被删除:

#!/bin/bash
export DISPLAY=:0.0

if [ $# -eq 0 ]; then
  echo usage: $(basename $0) "on|off|status"
  exit 1
fi

if [ $1 = "off" ]; then
  echo -en "Turning monitor off..."
  xset dpms force off
  echo -en "done.\nCheck:"
  xset -q|grep "Monitor is"
elif [ $1 = "on" ]; then
  echo -en "Turning monitor on..."
  xset dpms force on
  echo -en "done.\nCheck:"
  xset -q|grep "Monitor is"
elif [ $1 = "status" ]; then
  xset -q|sed -ne 's/^[ ]*Monitor is //p'
else 
  echo usage: $(basename $0) "on|off|status"
fi

#3


5  

You can get some info using the xrandr command-line utility, if your video driver supports this extension.

如果您的视频驱动程序支持此扩展,您可以使用xrandr命令行实用程序获得一些信息。

#4


3  

Not all monitors support vesa DDC. Thing might got even more complicated if you use a dock.

不是所有的监视器都支持vesa DDC。如果你使用码头,事情可能会变得更复杂。

On the other hand, there is a way to check whether your actions are detected by monitoring the kernel/udev events. To do this, for Fedora and RHEL, type following command:

另一方面,有一种方法可以检查您的操作是否通过监视内核/udev事件来检测。为此,对于Fedora和RHEL,输入以下命令:

sudo udevadm monitor --property

It will display every kernel and udev events it detected. From that, you can try plug/unplug the monitor data cable; plug/unplug the monitor power cable; toggle the stand-by/on states by pressing the power button.

它将显示它检测到的每个内核和udev事件。由此,您可以尝试插入/拔出监控数据电缆;插入/拔出监控电源电缆;按下电源按钮以切换待机状态。

If no output is generated after an action, then your system cannot detect it.

如果在操作之后没有生成输出,那么系统无法检测到它。

#5


2  

When using xset it always returns xset: unable to open display ""

当使用xset时,它总是返回xset:无法打开显示

However, "xset dpms force off" & "xset dpms force off" commands actually turn my monitor off and on. I am using the script outlined here -

然而,“xset dpms force off”和“xset dpms force off”命令实际上会关闭和打开我的监视器。我正在使用这里列出的脚本

http://systembash.com/content/how-to-turn-off-your-monitor-via-command-line-in-ubuntu/

http://systembash.com/content/how-to-turn-off-your-monitor-via-command-line-in-ubuntu/

#6


1  

You might want to look at the output of

你可能想看看输出

$ xset -q

I'm not sure if it will work but I think the line " Monitor is (on|off)" should tell you the answer.

我不确定它是否可行,但我认为“监视器”(on|off)应该告诉你答案。

#7


1  

xset -qis the way to go for a raspberry pi. A check to see if the reply contains 'Monitor is On' is a great way to use a gpio pin to turn off an LCD Backlight;

xset -qis the way to go for a raspberry pi。查看回复中是否包含“Monitor is On”是使用gpio引脚关闭LCD背光的好方法;

if(runOSCommand("xset -q").contains("Monitor is On")){
            System.out.println("Monitor is On");
            if screenLight.isHigh()) {
                screenLight.low();
            }
        }else{
            System.out.println("Monitor is Off");
            if (screenLight.isLow()) {
                screenLight.high();
            }
        }


     public static String runOSCommand(String command){
     String s = null;
     String string = "";
     Process p;
     try {
         p = Runtime.getRuntime().exec(command);
         BufferedReader br = new BufferedReader(
             new InputStreamReader(p.getInputStream()));
         while ((s = br.readLine()) != null){
 //            System.out.println("line: " + s);
             string += s;
         }
         p.waitFor();
//         System.out.println ("exit: " + p.exitValue());
         p.destroy();
     } catch (Exception e) {}
     return string;
 }