官术网_书友最值得收藏!

Using a virtual serial adapter on USB OTG

Some of the coolest hacks that are available on the Raspberry Pi Zero are to do with getting it to work with other computers using a single USB cable. In this recipe, we will create a "virtual" serial adapter, so instead of connecting with a specialized USB-to-serial adapter/cable, we can create the same connection using a standard regular-to-micro USB cable. I can carry around the Pi Zero and a cable in my backpack, and any time I need a Linux terminal, I just need a machine with a keyboard and a screen to plug into.

Getting ready

All you need to do to be set up here is perform the USB OTG previous functions recipe. After making the change, you can connect your Pi to any computer from the micro-USB connection. For most desktops, you won't even need additional power to run the Pi; it will all work over the one connection.

How to do it...

This is the most finicky file you will have to edit. Be very careful not to make any mistakes, as it will probably stop your Zero from starting until you fix it (fortunately, you made a backup copy earlier, right?):

  1. The unedited version of the file should look something like this:
            pi@rpz14101:~$ sudo cat /boot/cmdline.txt
            dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
    
  2. We're going to add one piece after rootwait to make the Pi ready for serial connections over USB:
            pi@rpz14101:~$ sudo cat /boot/cmdline.txt
            dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait modules- load=dwc2,g_serial
    
  3. Finally, you will enable the configuration service for the port in order to allow serial communication:
            pi@rpz14101:~ $ sudo systemctl enable getty@ttyGS0.service
            Created symlink from /etc/systemd/system/getty.target.wants/ getty@ttyGS0.service to /lib/systemd/system/getty@.service.
    
  4. After this, unplug your Raspberry Pi Zero, and reconnect it to a computer with a USB-to-micro cable. Connect using the USB power, not the power port. If you use a powered USB port on your computer, you shouldn't need to add additional power either. Once the Zero has a chance to boot up, you should see the following come up in Device Manager on a Windows system:
  5. On Linux or Max OS X, you should be able to find a new device just like you found the real serial device in the first recipe of this chapter. With PuTTY, you'll use the same serial connection, but with the COM port specified for Pi USB to serial, like this:
  6. Clicking on Open should get you to your familiar Raspberry Pi Zero terminal window:
主站蜘蛛池模板: 新乡市| 彭山县| 漯河市| 莆田市| 武隆县| 宝鸡市| 安远县| 冀州市| 隆化县| 隆林| 突泉县| 张家界市| 达拉特旗| 辽中县| 临湘市| 遂宁市| 龙胜| 娱乐| 龙游县| 枣庄市| 屏东县| 井陉县| 江阴市| 海原县| 邯郸市| 民丰县| 澄城县| 敖汉旗| 敖汉旗| 治多县| 华宁县| 平阴县| 泽州县| 西安市| 新龙县| 旌德县| 和静县| 枞阳县| 金湖县| 五家渠市| 江北区|