![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
エレキジャック Web版 mbedで初めてのマイコン開発 mbedをLANに接続してみよう!のプログラムです。 http://www.eleki-jack.com/arm/mbed/cat691/mbedlan/ <1/3>に RJ45モジュラージャックの加工について <3/3>にプログラムの解説があります。
Dependencies: EthernetNetIf mbed
main.cpp@0:ea83e758df10, 2010-10-07 (annotated)
- Committer:
- sunifu
- Date:
- Thu Oct 07 09:23:04 2010 +0000
- Revision:
- 0:ea83e758df10
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sunifu | 0:ea83e758df10 | 1 | #include "mbed.h" |
sunifu | 0:ea83e758df10 | 2 | #include "EthernetNetIf.h" |
sunifu | 0:ea83e758df10 | 3 | #include "UDPSocket.h" |
sunifu | 0:ea83e758df10 | 4 | |
sunifu | 0:ea83e758df10 | 5 | int main() { |
sunifu | 0:ea83e758df10 | 6 | |
sunifu | 0:ea83e758df10 | 7 | EthernetNetIf eth; // (1) -- DHCP |
sunifu | 0:ea83e758df10 | 8 | // EthernetNetIf eth( // (2) -- static IP address |
sunifu | 0:ea83e758df10 | 9 | // IpAddr(192,168,0,2), // IP Address |
sunifu | 0:ea83e758df10 | 10 | // IpAddr(255,255,255,0), // Subnet Mask |
sunifu | 0:ea83e758df10 | 11 | // IpAddr(192,168,0,1), // Default Gateway |
sunifu | 0:ea83e758df10 | 12 | // IpAddr(192,168,0,1) // DNS Server |
sunifu | 0:ea83e758df10 | 13 | // ) ; |
sunifu | 0:ea83e758df10 | 14 | UDPSocket udp; |
sunifu | 0:ea83e758df10 | 15 | char msg[64] ; |
sunifu | 0:ea83e758df10 | 16 | Timer tmr; |
sunifu | 0:ea83e758df10 | 17 | int i = 1 ; |
sunifu | 0:ea83e758df10 | 18 | |
sunifu | 0:ea83e758df10 | 19 | printf("Setting up...\r\n"); |
sunifu | 0:ea83e758df10 | 20 | |
sunifu | 0:ea83e758df10 | 21 | EthernetErr ethErr = eth.setup(); |
sunifu | 0:ea83e758df10 | 22 | |
sunifu | 0:ea83e758df10 | 23 | if( ethErr != ETH_OK ) |
sunifu | 0:ea83e758df10 | 24 | { |
sunifu | 0:ea83e758df10 | 25 | printf("Error %d in setup.\r\n", ethErr); |
sunifu | 0:ea83e758df10 | 26 | return -1; |
sunifu | 0:ea83e758df10 | 27 | } |
sunifu | 0:ea83e758df10 | 28 | |
sunifu | 0:ea83e758df10 | 29 | printf("Setup OK\r\n"); |
sunifu | 0:ea83e758df10 | 30 | |
sunifu | 0:ea83e758df10 | 31 | Host host(IpAddr(192,168,0,3), 12345, NULL); |
sunifu | 0:ea83e758df10 | 32 | |
sunifu | 0:ea83e758df10 | 33 | udp.bind(host); |
sunifu | 0:ea83e758df10 | 34 | |
sunifu | 0:ea83e758df10 | 35 | tmr.start(); |
sunifu | 0:ea83e758df10 | 36 | |
sunifu | 0:ea83e758df10 | 37 | while(true) |
sunifu | 0:ea83e758df10 | 38 | { |
sunifu | 0:ea83e758df10 | 39 | Net::poll(); |
sunifu | 0:ea83e758df10 | 40 | |
sunifu | 0:ea83e758df10 | 41 | if(tmr.read() > 5){ |
sunifu | 0:ea83e758df10 | 42 | tmr.reset(); |
sunifu | 0:ea83e758df10 | 43 | |
sunifu | 0:ea83e758df10 | 44 | if ( i % 5 != 0 ){ |
sunifu | 0:ea83e758df10 | 45 | sprintf(msg, "No.%3d --- Hello World! ---\r\n",i); |
sunifu | 0:ea83e758df10 | 46 | udp.sendto( msg, strlen(msg), &host ); |
sunifu | 0:ea83e758df10 | 47 | printf("%s\r\n", msg); |
sunifu | 0:ea83e758df10 | 48 | }else{ |
sunifu | 0:ea83e758df10 | 49 | sprintf(msg, "No.%3d --- End! ---\r\n",i); |
sunifu | 0:ea83e758df10 | 50 | udp.sendto( msg, strlen(msg), &host ); |
sunifu | 0:ea83e758df10 | 51 | printf("%s\r\n", msg); |
sunifu | 0:ea83e758df10 | 52 | } |
sunifu | 0:ea83e758df10 | 53 | i++; |
sunifu | 0:ea83e758df10 | 54 | } |
sunifu | 0:ea83e758df10 | 55 | } |
sunifu | 0:ea83e758df10 | 56 | } |