簡體   English   中英

如何在Node.js的同一進程中偵聽同一地址上的不同UDP端口

[英]How to listen to different UDP ports on the same address within the same process in Node.js

我正在編寫一個Node.js應用程序來控制小型無人機。 以下是SDK中的說明:


使用Wi-Fi在Tello與PC,Mac或移動設備之間建立連接。

發送命令並接收響應

Tello IP:192.168.10.1 UDP端口:8889 <<->> PC / Mac / Mobile

步驟1:在PC,Mac或移動設備上設置UDP客戶端,以通過同一端口從Tello發送和接收消息。

步驟2:在發送任何其他命令之前,通過UDP PORT 8889將“命令”發送到Tello以啟動SDK模式。

接收泰洛州

Tello IP:192.168.10.1->> PC / Mac /移動UDP服務器:0.0.0.0 UDP端口:8890

步驟3:在PC,Mac或移動設備上設置UDP服務器,並通過UDP PORT 8890檢查來自IP 0.0.0.0的消息。在嘗試步驟3之前,必須完成步驟1和2。

接收Tello視頻流

Tello IP:192.168.10.1->> PC / Mac /移動UDP服務器:0.0.0.0 UDP端口:11111

步驟4:在PC,Mac或移動設備上設置UDP服務器,並通過UDP PORT 11111檢查IP 0.0.0.0中的消息。

步驟5:通過UDP PORT 8889將“ streamon”發送到Tello以開始流式傳輸。 在嘗試步驟5之前,必須完成步驟1和2。


命令和接收部分的工作原理就像一個超級按鈕,我正在端口8889上與無人機之間收發數據報。我的問題是,看起來我在其他端口上沒有收到任何狀態或視頻流消息,我我很確定這不是無人機問題,但我沒有使用Node正確設置某些問題。 誰能看到問題在我的實現中。 這是我的代碼:

tello.ts

import dgram from 'dgram';

export class Tello {
  private LOCAL_IP_ = '0.0.0.0';
  private TELLO_IP_ = '192.168.10.1';

  private COMMAND_PORT_ = 8889;
  private commandSocket_ = dgram.createSocket('udp4');

  private STATE_PORT_ = 8890;
  private stateSocket_ = dgram.createSocket('udp4');

  private VIDEO_PORT_ = 11111;
  private videoSocket_ = dgram.createSocket('udp4');

  constructor() {}

  startCommandSocket() {
    this.commandSocket_.addListener('message', (msg, rinfo) => {
      const message = msg.toString();
      console.log(`from ${rinfo.address}: ${message}`);
    });
    this.commandSocket_.bind(this.COMMAND_PORT_, this.LOCAL_IP_, () => {
      console.log('Started listening on the command socket');
    });
  }

  startStateSocket() {
    this.stateSocket_.addListener('message', (msg, rinfo) => {
      const message = msg.toString();
      console.log(`from ${rinfo.address}: ${message}`);
    });
    this.stateSocket_.bind(this.STATE_PORT_, this.LOCAL_IP_, () => {
      console.log('Started listening on the state socket');
    });
  }

  startVideoSocket() {
    this.videoSocket_.addListener('message', (msg, rinfo) => {
      console.log('receiving video');      
      const message = msg.toString();
      console.log(`from ${rinfo.address}: ${message}`);
    });
    this.videoSocket_.bind(this.VIDEO_PORT_, this.LOCAL_IP_, () => {
      console.log('Started listening on the video socket');
    });
  }

  private sendCommand_(command: string) {
    // As this is sent over UDP and we have no guarantee that the packet is received or a response given
    // we are sending the command 5 times in a row to add robustess and resiliency.
    //for (let i = 0; i < 5; i++) {
    this.commandSocket_.send(command, this.COMMAND_PORT_, this.TELLO_IP_);
    //}
    console.log(`sending command: ${command} to ${this.TELLO_IP_}`);
  }

  /**
   * Enter SDK mode.
   */
  command() {
    this.sendCommand_('command');
  }

  /**
   * Auto takeoff.
   */
  takeoff() {
    this.sendCommand_('takeoff');
  }

  /**
   * Auto landing.
   */
  land() {
    this.sendCommand_('land');
  }

  streamVideoOn() {
    this.sendCommand_('streamon');
  }

  streamVideoOff() {
    this.sendCommand_('streamoff');
  }

  ...

}

index.ts

import { waitForSeconds } from './utils';
import { Tello } from './tello'

const main = async () => {
  const tello = new Tello();

  tello.startCommandSocket();
  await waitForSeconds(1);
  tello.command();
  await waitForSeconds(1);  
  tello.startStateSocket();
  await waitForSeconds(1);
  tello.startVideoSocket();
  await waitForSeconds(1);
  tello.streamVideoOn();
  await waitForSeconds(1);

  tello.takeoff();
  await waitForSeconds(10);
  tello.land(); 
};

main();

這是使用“ streamon”命令接收和解碼Tello SDK團隊提供的h264視頻流的示例代碼。 https://github.com/dji-sdk/Tello-Python請參考DOC / reademe.pdf和源代碼H264解碼器的路徑下對所接收的視頻流數據的具體處理方法。

在運行示例代碼之前,應該使用安裝腳本安裝一些依賴項。

您是否打開防火牆接受UDP端口8890/11111?

在筆記本電腦防火牆中打開端口8890 / udp和11111 / udp,以接收Tello遙測數據。

在Linux上

$ sudo firewall-cmd --permanent --add-port = 8890 / udp

$ sudo firewall-cmd-永久--add-port = 11111 / udp

在Mac上,使用“系統偏好設置”打開端口。

Open System Preferences > Security & Privacy > Firewall > Firewall Options
Click the + / Add button
Choose 'node' application from the Applications folder and click Add.
Ensure that the option next to the application is set to Allow incoming connections.
Click OK.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM