简体   繁体   English

如何在树莓派上找到 USB 调制解调器的 tty?

[英]How to find tty of USB modem on raspberry pi?

I want send AT commands to USB modem connected to raspberry pi.我想向连接到树莓派的 USB 调制解调器发送 AT 命令。 I have found some helps mentioning that USB modem is usually connected as ttyACM or ttyUSB device, but I have no ttyACM or ttyUSB device.我发现一些帮助提到 USB 调制解调器通常连接为 ttyACM 或 ttyUSB 设备,但我没有 ttyACM 或 ttyUSB 设备。 I have devices from tty1 to tty63, but I do not know how to identify which one is used by the modem.我有从 tty1 到 tty63 的设备,但我不知道如何识别调制解调器使用的是哪一个。

I have tried to find the device by storing devices list with the modem unconnected and then compare device list after the modem is pluged in:我试图通过在未连接调制解调器的情况下存储设备列表来查找设备,然后在插入调制解调器后比较设备列表:

ls /dev/ > dev_list_1.txt
(plug the modem in)
ls /dev/ | diff --suppress-common-lines -y - dev_list_.txt

returns to me:返回给我:

bsg                               <
sda                               <
sg0 

I have tried to connect the modem with cu tool:我尝试使用 cu 工具连接调制解调器:

sudo cu -l sda
sudo cu -l sg0

but both returns:但两者都返回:

cu: open (/dev/sg0): Permission denied
cu: sg0: Line in use

So I tried also use minicom and configure serial communication to /dev/sg0 or /dev/sda but it does not work either.所以我也尝试使用 minicom 并配置与 /dev/sg0 或 /dev/sda 的串行通信,但它也不起作用。

So I think I need to find right tty device used by the modem to be able to communicate with it.所以我想我需要找到调制解调器使用的正确的 tty 设备才能与之通信。 But how to find it?但是怎么找呢?

You can look for /sys/class/tty/*/device entries and ignore all the links that points to serial8250 since those are not USB devices.您可以查找/sys/class/tty/*/device条目并忽略所有指向 serial8250 的链接,因为它们不是 USB 设备。

With shell script:使用 shell 脚本:

for device in /sys/class/tty/*/device
do
    case $(readlink $device) in
        *serial8250)    # e.g. ../../../serial8250
            ;;
        *)              # e.g. ../../../1-3:3.1
            echo $device | rev | cut -d/ -f2 | rev
            ;;
    esac;
done | sed 's@^@/dev/@'

which produces产生

/dev/ttyACM0
/dev/ttyACM1
/dev/ttyS0

with my phone connected.与我的手机连接。 You might get more than the usb devices (eg ttyS0) but at least this should give you all the usb serial devices the kernel know about (and if the device does not populate /sys/class/tty/ it almost certainly is not a serial device).您可能得到的不仅仅是 usb 设备(例如 ttyS0),但至少这应该为您提供所有 usb 串行设备 kernel 不知道/tty/它几乎肯定不会填充串行/设备)。

This is based on the list_ports function logic in the libserialport library .这是基于libserialport 库中的list_ports function 逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM