2017-06-26 22 views
-1

私は2つのarduinos、1つはイーサネットシールド、もう1つはENC28J60イーサネットモジュールと通信するコードを作成しています。私はarduinoの初心者でも賢明でも専門家でもない。しかし、私はUDP通信の初心者ではありません。パケット送信後のUDPポートarduinoインクリメント

ここに質問があります:私のコードはうまく動作し、それは1つからもう1つとviceversaにUDPパケットを送受信します。しかし、すべてのパケットが送信された後、 "Udp.remotePort"値( "udp-reader"側から見た値)が増えます。 1024から最大32000まで開始されます(最大値に達した後に開始されます)。私はUDPについて研究しており、最初の0〜1023は特定のサービスのために予約されていることを理解しています。 80 http、21 ftp。しかし、私は、それが毎回送信されるたびに増加してはならないと思う。それともすべき?

私はそれが正常に動作すると言ったので、私はコードを貼り付けません。あなたの経験から何が間違っているのかを知りたいだけです。

私がパケットを書くために使用している文は次のとおりです。

udp.beginPacket(IPAddress([ip address]), [port no]); 

私が使用しているライブラリ:

UIPEthernet.h https://github.com/UIPEthernet/UIPEthernet for ENC28J60 

Ethernet.hイーサネット用

EDITシールド:これをUDP送信者(ENC28J60)のコードです。基本的にはライブラリのサンプルコードですが、通信の観点からは正しく動作すると言います。 IPを変更したのは、UDP送信者である192.168.1.50とUDP宛先である192.168.1.51のみです。

#include <UIPEthernet.h> 

EthernetUDP udp; 
unsigned long next; 

void setup() { 

    Serial.begin(115200); 

    uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05}; 

    Ethernet.begin(mac,IPAddress(192,168,1,51)); 
// Also i used: Ethernet.begin(mac,IPAddress(192,168,1,51), 5000); 
// with the same result 

    next = millis()+2000; 
} 

void loop() { 

    int success; 
    int len = 0; 

    if (((signed long)(millis()-next))>0) 
    { 
     do 
     { 
      success = udp.beginPacket(IPAddress(192,168,1,50),5000); 
      Serial.print("beginPacket: "); 
      Serial.println(success ? "success" : "failed"); 
      //beginPacket fails if remote ethaddr is unknown. In this case an 
      //arp-request is send out first and beginPacket succeeds as soon 
      //the arp-response is received. 
     } 
     while (!success && ((signed long)(millis()-next))<0); 
     if (!success) 
     goto stop; 

     success = udp.write("hello world&from&arduino"); 

     Serial.print("bytes written: "); 
     Serial.println(success); 

     success = udp.endPacket(); 

     Serial.print("endPacket: "); 
     Serial.println(success ? "success" : "failed"); 

     do 
     { 
      //check for new udp-packet: 
      success = udp.parsePacket(); 
     } 
     while (!success && ((signed long)(millis()-next))<0); 
     if (!success) 
     goto stop; 

     Serial.print("received: '"); 
     do 
     { 
      int c = udp.read(); 
      Serial.write(c); 
      len++; 
     } 
     while ((success = udp.available())>0); 
     Serial.print("', "); 
     Serial.print(len); 
     Serial.println(" bytes"); 

     //finish reading this packet: 
     udp.flush(); 

     stop: 
     udp.stop(); 
     next = millis()+2000; 
    } 
} 

EDIT 2:これは、あなたが送信されたデータグラムごとに新しいUDPソケットを作成する必要がありますSocketTest listening on port 5000, and after a packet received, the next one arrives with the remote port incremented on 1 each time

+1

コードが100%OKの場合は、質問はありません。質問がある場合は、コードを投稿する必要があります。 – EJP

+0

OK、コードを投稿しました。 – charly989

答えて

1

とテストのキャプチャです。それをしないでください。アプリケーションの存続期間中は同じものを使用してください。

+0

私はそれを避ける方法を知らない、何が間違っているの? – charly989

+0

@ charly989、あなたはアプリケーションライフの間にファイルに書き込むたびにファイルを開くのですか?いいえ、それは愚かで遅いでしょう。アプリケーションのセットアップ中にソケットを開き、アプリケーションループ中にソケットを使用し、アプリケーションが完了したらソケットを閉じます。 –

+0

OK、良いアナロジー私はあなたのポイントを理解する(そして私はそれを適用する)。しかし、私はまだ、この特定のコードでは、各パケットが送信された後にポートが1ずつインクリメントする理由を知りません。アップロードした画像を確認してください。 (EDIT 2)。 – charly989

関連する問題