UDP通信のサンプルプログラム
Dependencies: EthernetInterface mbed-rtos mbed
main.cpp@1:617727625d66, 2015-04-06 (annotated)
- Committer:
- yusuke_kyo
- Date:
- Mon Apr 06 15:01:25 2015 +0000
- Revision:
- 1:617727625d66
- Parent:
- 0:e0ec056e8895
?????
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yusuke_kyo | 1:617727625d66 | 1 | /*UDP通信サンプルコード |
yusuke_kyo | 0:e0ec056e8895 | 2 | 自分:mbedAから相手:mbedBへのデータ送信方法、相手:mbedBから自分:mbedAへのデータ受信方法*/ |
yusuke_kyo | 0:e0ec056e8895 | 3 | #include "mbed.h" |
yusuke_kyo | 0:e0ec056e8895 | 4 | #include "EthernetInterface.h" |
yusuke_kyo | 0:e0ec056e8895 | 5 | |
yusuke_kyo | 0:e0ec056e8895 | 6 | Serial pc(USBTX, USBRX); // tx, rx |
yusuke_kyo | 0:e0ec056e8895 | 7 | |
yusuke_kyo | 0:e0ec056e8895 | 8 | const char* A_ADDRESS = "192.168.1.1"; |
yusuke_kyo | 0:e0ec056e8895 | 9 | const int A_PORT = 1000; |
yusuke_kyo | 0:e0ec056e8895 | 10 | |
yusuke_kyo | 0:e0ec056e8895 | 11 | const char* B_ADDRESS = "192.168.1.2"; |
yusuke_kyo | 0:e0ec056e8895 | 12 | const int B_PORT = 2000; |
yusuke_kyo | 0:e0ec056e8895 | 13 | |
yusuke_kyo | 0:e0ec056e8895 | 14 | int main() { |
yusuke_kyo | 0:e0ec056e8895 | 15 | |
yusuke_kyo | 0:e0ec056e8895 | 16 | char u_packet[2]; |
yusuke_kyo | 0:e0ec056e8895 | 17 | u_packet[0]=0x41; u_packet[1]=0x42; //ex. |
yusuke_kyo | 0:e0ec056e8895 | 18 | char u_buff[10]; |
yusuke_kyo | 0:e0ec056e8895 | 19 | |
yusuke_kyo | 0:e0ec056e8895 | 20 | EthernetInterface eth; |
yusuke_kyo | 0:e0ec056e8895 | 21 | eth.init(A_ADDRESS, "255.255.255.0", ""); // Use hard IP |
yusuke_kyo | 0:e0ec056e8895 | 22 | pc.printf("IP : %s\n", eth.getIPAddress()); |
yusuke_kyo | 0:e0ec056e8895 | 23 | if (eth.connect() < 0) { |
yusuke_kyo | 0:e0ec056e8895 | 24 | pc.printf("Failed to connect\n\r"); |
yusuke_kyo | 0:e0ec056e8895 | 25 | return -1; |
yusuke_kyo | 0:e0ec056e8895 | 26 | } |
yusuke_kyo | 0:e0ec056e8895 | 27 | |
yusuke_kyo | 0:e0ec056e8895 | 28 | UDPSocket sockA; |
yusuke_kyo | 0:e0ec056e8895 | 29 | if (sockA.bind(A_PORT) < 0) { |
yusuke_kyo | 0:e0ec056e8895 | 30 | pc.printf("Failed to bind UDP Socket to PORT : %d\n\r", A_PORT); |
yusuke_kyo | 0:e0ec056e8895 | 31 | return -1; |
yusuke_kyo | 0:e0ec056e8895 | 32 | } |
yusuke_kyo | 0:e0ec056e8895 | 33 | else pc.printf("Bind UDP Socket to PORT : %d\n\r", A_PORT); |
yusuke_kyo | 0:e0ec056e8895 | 34 | sockA.set_blocking(false); |
yusuke_kyo | 0:e0ec056e8895 | 35 | |
yusuke_kyo | 0:e0ec056e8895 | 36 | Endpoint EndpointA; |
yusuke_kyo | 0:e0ec056e8895 | 37 | EndpointA.set_address(A_ADDRESS, A_PORT); |
yusuke_kyo | 0:e0ec056e8895 | 38 | |
yusuke_kyo | 0:e0ec056e8895 | 39 | Endpoint EndpointB; |
yusuke_kyo | 0:e0ec056e8895 | 40 | EndpointB.set_address(B_ADDRESS, B_PORT); |
yusuke_kyo | 0:e0ec056e8895 | 41 | |
yusuke_kyo | 0:e0ec056e8895 | 42 | /*----------- |
yusuke_kyo | 0:e0ec056e8895 | 43 | MAIN ROOP |
yusuke_kyo | 0:e0ec056e8895 | 44 | -----------*/ |
yusuke_kyo | 0:e0ec056e8895 | 45 | while(1) { |
yusuke_kyo | 0:e0ec056e8895 | 46 | /*--send--*/ |
yusuke_kyo | 0:e0ec056e8895 | 47 | pc.printf("Send : %s\n", u_packet); |
yusuke_kyo | 0:e0ec056e8895 | 48 | int n_send=sockA.sendTo(EndpointB, (char*)u_packet, sizeof(u_packet)); |
yusuke_kyo | 0:e0ec056e8895 | 49 | wait_ms(1); |
yusuke_kyo | 0:e0ec056e8895 | 50 | |
yusuke_kyo | 0:e0ec056e8895 | 51 | if(n_send < 0){ |
yusuke_kyo | 1:617727625d66 | 52 | pc.printf("Sent unsuccessfully\n"); |
yusuke_kyo | 0:e0ec056e8895 | 53 | return 0; |
yusuke_kyo | 0:e0ec056e8895 | 54 | } |
yusuke_kyo | 0:e0ec056e8895 | 55 | else pc.printf("Sent %d bytes Successfully!\n",n_send); |
yusuke_kyo | 0:e0ec056e8895 | 56 | /*-receive--*/ |
yusuke_kyo | 0:e0ec056e8895 | 57 | int n_receive=sockA.receiveFrom(EndpointB, (char*)u_buff, sizeof(u_buff)); |
yusuke_kyo | 0:e0ec056e8895 | 58 | wait_ms(1); |
yusuke_kyo | 0:e0ec056e8895 | 59 | |
yusuke_kyo | 0:e0ec056e8895 | 60 | if(n_receive < 0){ |
yusuke_kyo | 1:617727625d66 | 61 | pc.printf("Received unsuccessfully\n"); |
yusuke_kyo | 0:e0ec056e8895 | 62 | return 0; |
yusuke_kyo | 0:e0ec056e8895 | 63 | } |
yusuke_kyo | 0:e0ec056e8895 | 64 | else { |
yusuke_kyo | 1:617727625d66 | 65 | pc.printf("Received %d byte Successfully!\n",n_receive); |
yusuke_kyo | 0:e0ec056e8895 | 66 | pc.printf("Received : %s\n",u_buff); |
yusuke_kyo | 0:e0ec056e8895 | 67 | } |
yusuke_kyo | 0:e0ec056e8895 | 68 | } |
yusuke_kyo | 0:e0ec056e8895 | 69 | |
yusuke_kyo | 0:e0ec056e8895 | 70 | sockA.close(false); |
yusuke_kyo | 0:e0ec056e8895 | 71 | eth.disconnect(); |
yusuke_kyo | 0:e0ec056e8895 | 72 | return 0; |
yusuke_kyo | 0:e0ec056e8895 | 73 | } |