Tomo Yamanaka / Mbed OS TCP_Comm_sample

You are viewing an older revision! See the latest version

Homepage

For mbed classic sample

For mbed classic sample, please refer to the following.
mbed-os用サンプルについては以下を参照ください。

https://developer.mbed.org/users/1050186/code/TCP_Comm_sample_mbed_classic/

Warning! 注意!

When exporting and using it, increase the following stack size.
エクスポートして使用する際は、以下のスタックサイズを増やしてください。

mbed-os/features/FEATURE_LWIP/lwip-interface/lwipopts.h

#define TCPIP_THREAD_STACKSIZE      1200
->
#define TCPIP_THREAD_STACKSIZE      2400

Information

Japanese version is available in lower part of this page.
このページの後半に日本語版が用意されています.

What is this ?

TCP communication sample program by using three GR-PEACHs.


Structure

GR-PEACH, ethernet cable and router.
/media/uploads/1050186/structure3.png


The IP address uses static IP address in this sample program. DHCP function of router does not use.

TCPPacket.h (IP address config)

/* GR-PEACH No1 */
#define PEACH_1_IP_ADDRESS      "192.168.2.1"
#define PEACH_1_PORT            11000

/* GR-PEACH No2 */
#define PEACH_2_IP_ADDRESS      "192.168.2.2"
#define PEACH_2_PORT            12000

/* GR-PEACH No3 */
#define PEACH_3_IP_ADDRESS      "192.168.2.3"
#define PEACH_3_PORT            13000

/* Other */
#define SUB_NET_MASK            "255.255.255.0"
#define DEFAULT_GATEWAY         "192.168.2.200"


In order to specify each GR-PEACH's own MAC address, the following function is added to main.cpp.

GR-PEACH NoMAC address
10x74, 0x90, 0x50, 0x00, 0x56, 0xA1
20x74, 0x90, 0x50, 0x00, 0x56, 0xA2
30x74, 0x90, 0x50, 0x00, 0x56, 0xA3

Specify MAC address

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA1;
}

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA2;
}

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA3;
}


In sample program, the communication among GR-PEACH do by Ethernet and use TCP. Therefore, in this program, it is defined send side is client and receive side is server side.
In addition, to send data in TCP communication, client side needs a port number that identifies the application of server side.
The port number of server side in TCP communication is shown below.

GR-PEACH NoRole (Client / Server)port number
1Client11000(unused)
2Client / Server12000
3Server13000

/media/uploads/1050186/client_server_en.png

Code of three PEACHs

There is the code of three GR-PEACHs in main.cpp. You can specify GR-PEACH to use by changing the following macro.

Specify GR-PEACH

/**** Selection of PEACH *****/
#define USE_PEACH       (1)     /* 1(PEACH No1) or 2(PEACH No2) or 3(PEACH No3) */
/*****************************/



How to use




概要

GR-PEACHを3台使ったTCP通信のサンプルプログラムです。


構成

GR-PEACH、イーサーネットケーブル、ルータ。
/media/uploads/1050186/structure3.png


IPアドレスは固定アドレスを使います。ルータのDHCP機能は使用しません。

TCPPacket.h (IP address config)

/* GR-PEACH No1 */
#define PEACH_1_IP_ADDRESS      "192.168.2.1"
#define PEACH_1_PORT            11000

/* GR-PEACH No2 */
#define PEACH_2_IP_ADDRESS      "192.168.2.2"
#define PEACH_2_PORT            12000

/* GR-PEACH No3 */
#define PEACH_3_IP_ADDRESS      "192.168.2.3"
#define PEACH_3_PORT            13000

/* Other */
#define SUB_NET_MASK            "255.255.255.0"
#define DEFAULT_GATEWAY         "192.168.2.200"


GR-PEACHそれぞれのMACアドレスを指定するために、以下の関数をmain.cppに追加しています。

GR-PEACH NoMAC address
10x74, 0x90, 0x50, 0x00, 0x56, 0xA1
20x74, 0x90, 0x50, 0x00, 0x56, 0xA2
30x74, 0x90, 0x50, 0x00, 0x56, 0xA3

Specify MAC address

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA1;
}

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA2;
}

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x74;
    mac[1] = 0x90;
    mac[2] = 0x50;
    mac[3] = 0x00;
    mac[4] = 0x56;
    mac[5] = 0xA3;
}


サンプルプログラムでは、GR-PEACH 間の通信は全てEtherで行いTCPを用います。 そのため、予めTCP通信におる送信側をクライアント、受信側をサーバと定義します。
またTCP通信でデータ送信するためには、クライアントはサーバのアプリケーションを特定する番号、つまり、ポート番号が必要です。
そこで、TCP通信でサーバとなるGR-PEACH のポート番号を以下に示します。

GR-PEACH NoTCP通信の役割(クライアント/サーバ)ポート番号
1クライアント11000(未使用)
2クライアント / サーバ12000
3サーバ13000

/media/uploads/1050186/client_server_jp.png

3台のコードについて

GR-PEACH3台のコードはmain.cppにあります。
以下のマクロを変更することで、使用するGR-PEACHを指定できます。

Specify GR-PEACH

/**** Selection of PEACH *****/
#define USE_PEACH       (1)     /* 1(PEACH No1) or 2(PEACH No2) or 3(PEACH No3) */
/*****************************/



使い方


All wikipages