How can I detect a USB port being used for charging in Linux?

假装没事ソ 提交于 2020-01-15 06:54:11

问题


I'm using a USB port on the Raspberry Pi 3 Model B to charge an accessory via a USB magnetic charging cable. I want to detect when the cable is plugged in and removed from the Pi.

I've been plugging in and removing this cable and looking for any traces of this being detected on Linux. So far, I've tried:

  • monitoring udev events by udevadm monitor.
  • tailing the system logs in /var/log etc.
  • running an inotify watch on the /dev directory for new devices
  • looking around in /sys/bus/usb/devices for any clues

Is it possible to detect my simple charging cable being inserted/removed? By intuition I would think no (since the cable is purely for charging, and doesn't have drivers), however I observe that the RPI flashes its builtin LED red when I insert the cable.


回答1:


Without significant effort (hardware or hacks), No.

The red LED flash you observe is the power monitor for the Pi indicating a sudden voltage drop as current begins to flow to your accessory. This LED indicates voltage (electrical energy) has briefly dropped below stable levels, and system instability may result.

As the connection is purely charging (no USB data exchanged with the Pi) the operating system has no idea the device is connected. You could build an external current-measuring circuit to detect the peripheral drawing power, but such a question is better off in the electronics stack exchange.




回答2:


I think that you cand do it with the lsusb tools:

 lsusb -v

Or Filtering with:

 lsusb -v |grep 'Bus\|MaxPower'

With this command I can see how much power each USB is usign and which device is connected.



来源:https://stackoverflow.com/questions/45902959/how-can-i-detect-a-usb-port-being-used-for-charging-in-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!