Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Impedance_Fast_Circuitry by
Diff: main.cpp
- Revision:
- 16:c3f922f61b8f
- Parent:
- 11:59dcefdda506
- Child:
- 17:2f978f823020
--- a/main.cpp Sun Sep 21 05:55:13 2014 +0000 +++ b/main.cpp Fri Nov 14 18:26:39 2014 +0000 @@ -1,31 +1,223 @@ +/* +#include "mbed.h" +#include "EthernetInterface.h" + +#define IP "127.0.0.5" +#define MASK "255.255.255.0" +#define GATEWAY "10.1.1.1" +#define DES_ADDR "127.0.0.6" + +#define PORT 80 + +Serial pc(USBTX, USBRX); +EthernetInterface eth; +UDPSocket point1; + + +int main () { + wait_ms(500); + // intitialize Serial connection + pc.baud(230400); + + // initialize IP connection + eth.init(IP, MASK, GATEWAY); + wait_ms(100); + + // setup UDP socket + point1.init(); + wait_ms(1); + point1.bind(PORT); + + +} +*/ + #include "mbed.h" #include "EthernetInterface.h" + +#define SWITCHER 0 +#if SWITCHER == 1 + +#define IP "127.0.0.5" +#define MASK "255.255.255.0" +#define GATEWAY "10.1.1.1" +#define DES_ADDR "127.0.0.6" +//#define DES_ADDR "128.187.113.146" //"utcnist.colorado.edu" +#define PORT 80 + +DigitalOut led_blue(LED_BLUE); +EthernetInterface eth; +Serial pc(USBTX, USBRX); +Timer timer; + int main() { - EthernetInterface eth; - eth.init(); //Use DHCP - eth.connect(); - printf("IP Address is %s\n", eth.getIPAddress()); + led_blue = 1; + pc.baud(230400); - TCPSocketConnection sock; - sock.connect("mbed.org", 80); + timer.stop(); + pc.printf("\r\nStarting program1...\r\n"); - char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; - sock.send_all(http_cmd, sizeof(http_cmd)-1); + eth.init(IP, MASK, GATEWAY); //Use DHCP + eth.connect(); + + pc.printf("IP Address is %s\r\n", eth.getIPAddress()); - char buffer[300]; - int ret; - while (true) { - ret = sock.receive(buffer, sizeof(buffer)-1); - if (ret <= 0) + if(1) + { + UDPSocket sock; + sock.init(); + + Endpoint nist; + nist.set_address(DES_ADDR, PORT)? pc.printf("Endpoint address NOT set\r\n") : pc.printf("Endpoint address set\r\n"); + led_blue = 0; + while(!pc.getc()); + led_blue = 1; + char out_buffer[] = "password"; // Does not matter + int returnVar = sock.sendTo(nist, out_buffer, sizeof(out_buffer)); + led_blue = 0; + switch(returnVar) { + case -1: + pc.printf("Password send failure\r\n"); break; - buffer[ret] = '\0'; - printf("Received %d chars from server:\n%s\n", ret, buffer); + default: + pc.printf("Password sent %i chars\r\n", returnVar); + } + wait(1); + led_blue = 1; + char in_buffer[400]; + int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer)); + + unsigned int timeRes = ntohl( *((unsigned int*)in_buffer)); + pc.printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes); + sock.close(); } - - sock.close(); + else + { + TCPSocketConnection sock; + //sock.connect("mbed.org", 80); + sock.connect(DES_ADDR, 80); + + char http_cmd[] = "GET htdocs/index.html HTTP/1.1"; + sock.send_all(http_cmd, sizeof(http_cmd)-1); + + char buffer[30000]; + int ret; + while (true) { + timer.reset(); + timer.start(); + ret = sock.receive(buffer, sizeof(buffer)-1); + timer.stop(); + pc.printf("Timer: %i\r\n", timer.read()); + if (ret <= 0) + break; + buffer[ret] = '\0'; + pc.printf("Received %d chars from server:\n%s\n", ret, buffer); + } + sock.close(); + } eth.disconnect(); - + pc.printf("\r\nEnding program\r\n"); while(1) {} + } + + + + + + + + + + + + +#else + +#define IP "127.0.0.6" +#define MASK "255.255.255.0" +#define GATEWAY "10.1.1.1" +#define DES_ADDR "127.0.0.5" +//#define DES_ADDR "128.187.113.146" //"utcnist.colorado.edu" +#define PORT 80 + +DigitalOut led_blue(LED_BLUE); +EthernetInterface eth; +Serial pc(USBTX, USBRX); +Timer timer; + +int main() { + led_blue = 1; + pc.baud(230400); + + timer.stop(); + pc.printf("\r\nStarting program2...\r\n"); + + eth.init(IP, MASK, GATEWAY); //Use DHCP + eth.connect(); + + pc.printf("IP Address is %s\r\n", eth.getIPAddress()); + + if(1) + { + UDPSocket sock; + sock.init(); + + Endpoint nist; + nist.set_address(DES_ADDR, PORT)? pc.printf("Endpoint address NOT set\r\n") : pc.printf("Endpoint address set\r\n"); + led_blue = 0; + while(!pc.getc()); + led_blue = 1; + char out_buffer[] = "blah"; // Does not matter + int returnVar = sock.sendTo(nist, out_buffer, sizeof(out_buffer)); + led_blue = 0; + switch(returnVar) { + case -1: + pc.printf("Password send failure\r\n"); + break; + default: + pc.printf("Password sent %i chars\r\n", returnVar); + } + wait(1); + led_blue = 1; + + char in_buffer[400]; + int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer)); + + unsigned int timeRes = ntohl( *((unsigned int*)in_buffer)); + pc.printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes); + sock.close(); + } + else + { + TCPSocketConnection sock; + //sock.connect("mbed.org", 80); + sock.connect(DES_ADDR, 80); + + char http_cmd[] = "GET htdocs/index.html HTTP/1.1"; + sock.send_all(http_cmd, sizeof(http_cmd)-1); + + char buffer[30000]; + int ret; + while (true) { + timer.reset(); + timer.start(); + ret = sock.receive(buffer, sizeof(buffer)-1); + timer.stop(); + pc.printf("Timer: %i\r\n", timer.read()); + if (ret <= 0) + break; + buffer[ret] = '\0'; + pc.printf("Received %d chars from server:\n%s\n", ret, buffer); + } + sock.close(); + } + + eth.disconnect(); + pc.printf("\r\nEnding program\r\n"); + while(1) {} + +} +#endif \ No newline at end of file