日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

树莓派 4b 配置 USB 网络连接

發(fā)布時(shí)間:2024/3/13 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 树莓派 4b 配置 USB 网络连接 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

樹莓派 4b 配置 USB 網(wǎng)絡(luò)連接

  • 樹莓派鏡像: 2022-09-22-raspios-bullseye-arm64.img

  • 電腦: windows 10

1 打開樹莓派的 USB 網(wǎng)絡(luò)

  • 在 /boot/config.txt 最后一行添加 dtoverlay=dwc2。

例如:

# ....[all] dtoverlay=dwc2
  • 在 /boot/cmdline.txt 中 rootwait 后添加 modules-load=dwc2,g_ether。

例如:

console=serial0,115200 console=tty1 root=PARTUUID=3d7323dc-02 rootfstype=ext4 fsck.repair=yes rootwait modules-load=dwc2,g_ether quiet splash plymouth.ignore-serial-consoles
  • 重啟樹莓派
sudo reboot

2 在 windows 上安裝 Linux USB Ethernet/RNDIS Gadget 驅(qū)動(dòng)

  • 將樹莓派充電的 type-c 口,使用手機(jī)數(shù)據(jù)線連接到電腦上。打開設(shè)備管理器,此時(shí) windows 系統(tǒng)將設(shè)備識(shí)別成 COM 口。

2.1 下載驅(qū)動(dòng)

驅(qū)動(dòng)下載鏈接:

RPI Driver OTG

百度網(wǎng)盤(提取碼: e69j)

如果沒有安裝 VS 或者不用自己編譯驅(qū)動(dòng),建議直接跳到 2.3 小節(jié)。

2.2 自己編譯驅(qū)動(dòng)(需要安裝VS)

  • 編寫 linux.inf 文件,內(nèi)容為
; Based on template INF file found at ; <https://msdn.microsoft.com/en-us/library/ff570620.aspx> ; which was: ; Copyright (c) Microsoft Corporation ; and released under the MLPL as found at: ; <http://msdn.microsoft.com/en-us/cc300389.aspx#MLPL>. ; For use only on Windows operating systems.[Version] Signature = "$Windows NT$" Class = Net ClassGUID = {4d36e972-e325-11ce-bfc1-08002be10318} Provider = %Linux% DriverVer = 10/10/2020,6.1.6000.16384 CatalogFile = linux.cat[Manufacturer] %Linux% = LinuxDevices,NTx86,NTamd64,NTia64; Decoration for x86 architecture [LinuxDevices.NTx86] %LinuxDevice% = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00; Decoration for x64 architecture [LinuxDevices.NTamd64] %LinuxDevice% = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00; Decoration for ia64 architecture [LinuxDevices.NTia64] %LinuxDevice% = RNDIS.NT.5.1, USB\VID_0525&PID_a4a2, USB\VID_1d6b&PID_0104&MI_00;@@@ This is the common setting for setup [ControlFlags] ExcludeFromSelect=*; DDInstall section ; References the in-build Netrndis.inf [RNDIS.NT.5.1] Characteristics = 0x84 ; NCF_PHYSICAL + NCF_HAS_UI BusType = 15 ; NEVER REMOVE THE FOLLOWING REFERENCE FOR NETRNDIS.INF include = netrndis.inf needs = Usb_Rndis.ndi AddReg = Rndis_AddReg_Vista; DDInstal.Services section [RNDIS.NT.5.1.Services] include = netrndis.inf needs = Usb_Rndis.ndi.Services; Optional registry settings. You can modify as needed. [RNDIS_AddReg_Vista] HKR, NDI\params\VistaProperty, ParamDesc, 0, %Vista_Property% HKR, NDI\params\VistaProperty, type, 0, "edit" HKR, NDI\params\VistaProperty, LimitText, 0, "12" HKR, NDI\params\VistaProperty, UpperCase, 0, "1" HKR, NDI\params\VistaProperty, default, 0, " " HKR, NDI\params\VistaProperty, optional, 0, "1"; No sys copyfiles - the sys files are already in-build ; (part of the operating system). ; We do not support XP SP1-, 2003 SP1-, ME, 9x.[Strings] Linux = "Linux Developer Community" LinuxDevice = "Linux USB Ethernet/RNDIS Gadget" Vista_Property = "Optional Vista Property"
  • 編寫 DriverHelper.ps1 文件,內(nèi)容為
# script parameters param([ValidateSet("Sign", "UntrustCertificates")][string] $Mode = "Sign",[string] $InfFile,[string] $CommonName = "linux.local",[switch] $Force )$ErrorActionPreference = "Stop"# check if this script is run as administrator If (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {Write-Error "You are not an Administrator. This script needs to be run by an Adminsitrator!" }# check if InfFile exists if(-NOT (Test-Path -Path $InfFile)) {Write-Error "$InfFile not found!" }# utility functions function Step {param([string] $Desc, [System.Management.Automation.ScriptBlock] $Code)Write-Output "[*] $Desc";Invoke-Command -ScriptBlock $Code }function Get-CertificateThumbprint {param([string] $FilePath)$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2$cert.Import($(Resolve-Path $FilePath))return $cert.Thumbprint }# Get Kits root try {$InstalledRoots = Get-ItemProperty -Path "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows Kits\Installed Roots"$WinKits = $InstalledRoots.KitsRoot10$ToolsPrefix = "$($WinKits)\bin\10.0.18362.0\x64" } catch {Write-Error "Could not locate the Windows Driver Kit. Head to https://docs.microsoft.com/en-us/windows-hardware/drivers/download-the-wdk and download the WDK" }function RunKit {param([Parameter(Mandatory=$true)][string] $ToolName, [string[]] $Arguments)Write-Output "Running $($ToolsPrefix)\$($ToolName)"$Proc = Start-Process -FilePath "$($ToolsPrefix)\$($ToolName)" -ArgumentList $Arguments -NoNewWindow -PassThru -Waitif ($Proc.ExitCode -NE 0){Write-Error "Process failed with exit code: $($Proc.ExitCode)"} }# Script vars $CatFile = [io.path]::GetFileNameWithoutExtension($InfFile) + ".cat" $CertFile = [io.path]::GetFileNameWithoutExtension($InfFile) + ".cer"# Scripts function DoSign {# verify that the Inf file contains a "Catalog" entryif (-NOT (Select-String -Path $InfFile -Pattern "CatalogFile" -CaseSensitive -SimpleMatch -Quiet)){Write-Error "Could not find the 'CatalogFile' entry inside the INF file! Make sure it points to a certificate with the same Name."}Step -Desc "Create Catalog file" -Code {New-FileCatalog -Path $InfFile -CatalogFilePath $CatFile -CatalogVersion 2.0}if (-NOT (Test-Path -Path $CertFile) -OR $Force){Step -Desc "Create a Test Certificate" -Code {# see https://docs.microsoft.com/en-us/windows-hardware/drivers/install/creating-test-certificates# This will create a self-signed certificate and store it in the Personal certificate stora on the local machine.RunKit -ToolName "makecert.exe" -Arguments "-r -sr LocalMachine -ss My -pe -n CN=$($CommonName) -eku 1.3.6.1.5.5.7.3.3 $CertFile"}Step -Desc "Import the Test Certificate into the Local Computer Trusted Root CAs and Trusted Publishers stores" -Code {Import-Certificate -FilePath $CertFile -CertStoreLocation Cert:\LocalMachine\RootImport-Certificate -FilePath $CertFile -CertStoreLocation Cert:\LocalMachine\TrustedPublisher}}else{Write-Output "Certificate already exists. Using $CertFile. If you still want to create a new one, use the -Force option"}Step -Desc "Sign the Catalog with the Test Certificate" -Code {# see https://docs.microsoft.com/en-us/windows-hardware/drivers/install/test-signing-a-catalog-fileRunKit -ToolName "signtool.exe" -Arguments "sign /v /sm /s My /n $($CommonName) /t http://timestamp.digicert.com $($CatFile)"}Write-Output "Successfully singed the driver and added the certificate to the trusted store." }function DoUntrustCertificates {Step -Desc "Remove the Test Certificate from the Local Computer Trusted Root CAs and Trusted Publishers stores" -Code {$thumbprint = Get-CertificateThumbprint -FilePath $CertFileRemove-Item -Path ("Cert:\LocalMachine\My\$($thumbprint)")Remove-Item -Path ("Cert:\LocalMachine\Root\$($thumbprint)")Remove-Item -Path ("Cert:\LocalMachine\TrustedPublisher\$($thumbprint)")}Write-Output "Sucessfully removed all certificates from the trusted certificate stores." }# entry Write-Output "Selected Mode: $Mode"Switch($Mode) {"Sign" { DoSign }"UntrustCertificates" { DoUntrustCertificates } }
  • 以管理員的身份打開 PowerShell,切換到當(dāng)前目錄,運(yùn)行 .\DriverHelper.ps1 -Mode Sign -InfFile .\linux.inf 命令,得到 linux.cat 和 linux.cer 文件。

2.3 安裝驅(qū)動(dòng)

  • 打開設(shè)備管理器,選擇中樹莓派的 COM 口,選擇更新驅(qū)動(dòng)程序。

  • 選擇 游覽我的電腦以查找驅(qū)動(dòng)程序

  • 目錄調(diào)整到創(chuàng)建前面文件所在目錄,點(diǎn)擊下一頁

  • 安裝完成

  • 此時(shí)任務(wù)管理器驅(qū)動(dòng)顯示正常

3 在樹莓派上安裝并打開 DHCP 服務(wù)器

  • 在 /etc/modules 末尾添加 libcomposite 。

例如:

i2c-dev libcomposite
  • 在 /etc/dhcpcd.conf 末尾添加 denyinterfaces usb0 。

例如:

#.... # fallback to static profile on eth0 #interface eth0 #fallback static_eth0 denyinterfaces usb0
  • 安裝 dnsmasq
sudo apt update sudo apt install dnsmasq
  • 創(chuàng)建 /etc/dnsmasq.d/usb 文件,內(nèi)容為
interface=usb0 dhcp-range=10.55.0.2,10.55.0.254,255.255.255.0,12h dhcp-option=3 leasefile-ro

其中 dhcp-range 設(shè)置表示 DHCP 服務(wù)分配的 IP 地址范圍從 10.55.0.2-10.55.0.254,子網(wǎng)掩碼為 255.255.255.0,有效期為 12 小時(shí)。

  • 創(chuàng)建 /etc/network/interfaces.d/usb0 文件,內(nèi)容為
auto usb0 allow-hotplug usb0 iface usb0 inet staticaddress 10.55.0.1netmask 255.255.255.0

其中,address 表示 USB 接口的 IP 地址為 10.55.0.1,子網(wǎng)掩碼為 255.255.255.0。

  • 創(chuàng)建 /root/usb.sh ,內(nèi)容為
#!/bin/bash cd /sys/kernel/config/usb_gadget/ mkdir -p pi4 cd pi4 echo 0x1d6b > idVendor # Linux Foundation echo 0x0104 > idProduct # Multifunction Composite Gadget echo 0x0100 > bcdDevice # v1.0.0 echo 0x0200 > bcdUSB # USB2 echo 0xEF > bDeviceClass echo 0x02 > bDeviceSubClass echo 0x01 > bDeviceProtocol mkdir -p strings/0x409 echo "fedcba9876543211" > strings/0x409/serialnumber echo "Ben Hardill" > strings/0x409/manufacturer echo "PI4 USB Device" > strings/0x409/product mkdir -p configs/c.1/strings/0x409 echo "Config 1: ECM network" > configs/c.1/strings/0x409/configuration echo 250 > configs/c.1/MaxPower # Add functions here # see gadget configurations below # End functions mkdir -p functions/ecm.usb0 HOST="00:dc:c8:f7:75:14" # "HostPC" SELF="00:dd:dc:eb:6d:a1" # "BadUSB" echo $HOST > functions/ecm.usb0/host_addr echo $SELF > functions/ecm.usb0/dev_addr ln -s functions/ecm.usb0 configs/c.1/ udevadm settle -t 5 || : ls /sys/class/udc > UDC ifup usb0 service dnsmasq restart
  • 給腳本添加可執(zhí)行權(quán)限
sudo chmod +x /root/usb.sh
  • 添加腳本開機(jī)啟動(dòng),在 /etc/rc.local 中 exit 0 上一行添加 /root/usb.sh。

例如

_IP=$(hostname -I) || true if [ "$_IP" ]; thenprintf "My IP address is %s\n" "$_IP" fi/root/usb.shexit 0
  • 重啟樹莓派,在樹莓派上看到 USB 接口的 IP 地址。

  • 在 windows 電腦上查看到樹莓派 type-c 接口分配的 IP 地址。

此時(shí)樹莓派 IP 地址為 10.55.0.1,window 電腦的 IP 地址為 10.55.0.2。

4 測(cè)試連接

  • 用 windows 電腦 ping 樹莓派。

  • 如果樹莓派上打開了 ssh,則可以使用 ssh 連接。

  • 如果樹莓派上打開了VNC,也可以用 VNC 連接

參考鏈接

Ethernet Gadget
Pi4 USB-C Gadget
Fix USB connection from Raspberry Pi on Windows

總結(jié)

以上是生活随笔為你收集整理的树莓派 4b 配置 USB 网络连接的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。