TCP communication sample for mbed-os by using 3 GR-PEACHs.

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/structure2.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



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

  1. Please write bin file to GR-PEACH No1 after build by setting USE_PEACH to 1.
  2. Please write bin file to GR-PEACH No2 after build by setting USE_PEACH to 2.
  3. Please write bin file to GR-PEACH No3 after build by setting USE_PEACH to 3.
  4. Please connect GR-PEACH, ethernet cable and router like as structure picture.
  5. Please start terminal software.
  6. Please press the reset button of GR-PEACH in order of GR-PEACH No3、GR-PEACH No2、GR-PEACH No1.
  7. An IP address is output on terminal.
    Please refer to the following link about how to use the terminal.
    The default value of baud rate in mbed is 9600, and this application uses baud rate 9600.
    https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication
    https://developer.mbed.org/handbook/SerialPC
  8. After the IP address,
    in case of GR-PEACH No1, transmitted data is output, in case of GR-PEACH No2, received data and transmitted data is output, and in case of GR-PEACH No3, received data is output on terminal.
  9. GR-PEACH No1 sends 1 byte to GR-PEACH No2 every 1 second while incrementing.
    GR-PEACH No2 sends received data as is to GR-PEACH No3.
GR-PEACH No1 sample start!!
IP Address is 192.168.2.1
Now connecting...
Send message : 1
Now connecting...
Send message : 2
Now connecting...
Send message : 3
GR-PEACH No2 sample start!!
IP Address is 192.168.2.2
Received message : 1
Now connecting...
Send message : 1
Received message : 2
Now connecting...
Send message : 2
Received message : 3
Now connecting...
Send message : 3
GR-PEACH No3 sample start!!
IP Address is 192.168.2.3
Received message : 1
Received message : 2
Received message : 3



概要

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


構成

GR-PEACH、イーサーネットケーブル、ルータ。
/media/uploads/1050186/structure2.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



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) */
/*****************************/



使い方

  1. USE_PEACHを1に設定してビルドして、GR-PEACH No1に書き込みます。
  2. USE_PEACHを2に設定してビルドして、GR-PEACH No2に書き込みます。
  3. USE_PEACHを3に設定してビルドして、GR-PEACH No3に書き込みます。
  4. 構成図のようにGR-PEACHとイーサーネットケーブルとルータを接続します。
  5. Terminalソフトを立ち上げます。。
  6. GR-PEACH No3、GR-PEACH No2、GR-PEACH No1の順にGR-PEACHをリセットします。
  7. 3.Terminal上にIPアドレスが出力されます。
    Terminalの使い方は以下のリンクを参照下さい。
    mbedでのボーレートのデフォルト値は9600で、このサンプルではボーレート9600を使います。
    https://developer.mbed.org/teams/Renesas/wiki/GR-PEACH-Getting-Started#install-the-usb-serial-communication
    https://developer.mbed.org/handbook/SerialPC
    IPアドレスの後、GR-PEACH No1の場合は送信データ、GR-PEACH No2の場合は受信データと送信データ、GR-PEACH No3の場合は受信データがTerminalに出力されます。
  8. GR-PEACH No1は1秒間隔で1ByteのデータをインクリメントしながらGR-PEACH No2に送信します。
    GR-PEACH No2は受信したデータをそのままGR-PEACH No3に送信します。
GR-PEACH No1 sample start!!
IP Address is 192.168.2.1
Now connecting...
Send message : 1
Now connecting...
Send message : 2
Now connecting...
Send message : 3
GR-PEACH No2 sample start!!
IP Address is 192.168.2.2
Received message : 1
Now connecting...
Send message : 1
Received message : 2
Now connecting...
Send message : 2
Received message : 3
Now connecting...
Send message : 3
GR-PEACH No3 sample start!!
IP Address is 192.168.2.3
Received message : 1
Received message : 2
Received message : 3

Changes

RevisionDateWhoCommit message
1:cb7a74257b1e 2017-04-14 1050186 Modify main.cpp default tip
0:b7b0e737acd6 2017-04-13 1050186 first commit