momomo
Dependencies: mbed mbed-rtos EthernetInterface
main.cpp@9:39f59637a9f0, 2020-12-03 (annotated)
- Committer:
- momo7k
- Date:
- Thu Dec 03 03:42:52 2020 +0000
- Revision:
- 9:39f59637a9f0
- Parent:
- 8:23b1fba109b0
yo momo
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:38cbb854d85f | 1 | #include "mbed.h" |
emilmont | 1:5cebe0e38cd2 | 2 | #include "EthernetInterface.h" |
mbed_official | 8:23b1fba109b0 | 3 | |
momo7k | 9:39f59637a9f0 | 4 | #define ECHO_SERVER_PORT 3434 |
momo7k | 9:39f59637a9f0 | 5 | |
momo7k | 9:39f59637a9f0 | 6 | DigitalOut led1(LED1); |
momo7k | 9:39f59637a9f0 | 7 | DigitalOut led2(LED2); |
momo7k | 9:39f59637a9f0 | 8 | CAN can1(p30, p29); |
mbed_official | 8:23b1fba109b0 | 9 | |
emilmont | 1:5cebe0e38cd2 | 10 | int main (void) { |
momo7k | 9:39f59637a9f0 | 11 | |
emilmont | 1:5cebe0e38cd2 | 12 | EthernetInterface eth; |
emilmont | 1:5cebe0e38cd2 | 13 | eth.init(); //Use DHCP |
emilmont | 1:5cebe0e38cd2 | 14 | eth.connect(); |
momo7k | 9:39f59637a9f0 | 15 | printf("Server IP Address is %s\n\r", eth.getIPAddress()); |
emilmont | 1:5cebe0e38cd2 | 16 | |
emilmont | 1:5cebe0e38cd2 | 17 | TCPSocketServer server; |
emilmont | 3:36fd3cfad85a | 18 | server.bind(ECHO_SERVER_PORT); |
emilmont | 3:36fd3cfad85a | 19 | server.listen(); |
emilmont | 1:5cebe0e38cd2 | 20 | |
momo7k | 9:39f59637a9f0 | 21 | can1.frequency(125000); |
momo7k | 9:39f59637a9f0 | 22 | |
momo7k | 9:39f59637a9f0 | 23 | CANMessage msg; |
momo7k | 9:39f59637a9f0 | 24 | |
momo7k | 9:39f59637a9f0 | 25 | while (true) |
momo7k | 9:39f59637a9f0 | 26 | { |
momo7k | 9:39f59637a9f0 | 27 | printf("\nWait for new connection...\n\r"); |
emilmont | 1:5cebe0e38cd2 | 28 | TCPSocketConnection client; |
emilmont | 1:5cebe0e38cd2 | 29 | server.accept(client); |
emilmont | 3:36fd3cfad85a | 30 | client.set_blocking(false, 1500); // Timeout after (1.5)s |
emilmont | 1:5cebe0e38cd2 | 31 | |
momo7k | 9:39f59637a9f0 | 32 | printf("Connection from: %s\n\r", client.get_address()); |
emilmont | 1:5cebe0e38cd2 | 33 | char buffer[256]; |
momo7k | 9:39f59637a9f0 | 34 | while (true) |
momo7k | 9:39f59637a9f0 | 35 | { |
emilmont | 3:36fd3cfad85a | 36 | int n = client.receive(buffer, sizeof(buffer)); |
emilmont | 1:5cebe0e38cd2 | 37 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 38 | |
mbedAustin | 7:a5ead1402704 | 39 | // print received message to terminal |
mbedAustin | 7:a5ead1402704 | 40 | buffer[n] = '\0'; |
momo7k | 9:39f59637a9f0 | 41 | printf("Received message from Client :'%s'\n\r",buffer); |
momo7k | 9:39f59637a9f0 | 42 | |
momo7k | 9:39f59637a9f0 | 43 | |
momo7k | 9:39f59637a9f0 | 44 | if(can1.write(CANMessage(0x200,buffer,1))) |
momo7k | 9:39f59637a9f0 | 45 | { |
momo7k | 9:39f59637a9f0 | 46 | led1 = 1; |
momo7k | 9:39f59637a9f0 | 47 | } |
momo7k | 9:39f59637a9f0 | 48 | |
momo7k | 9:39f59637a9f0 | 49 | |
momo7k | 9:39f59637a9f0 | 50 | |
momo7k | 9:39f59637a9f0 | 51 | if(can1.read(msg)) |
momo7k | 9:39f59637a9f0 | 52 | { |
momo7k | 9:39f59637a9f0 | 53 | printf("Message received: %d\n", msg.data[0]); |
momo7k | 9:39f59637a9f0 | 54 | led2 = !led2; |
momo7k | 9:39f59637a9f0 | 55 | } |
momo7k | 9:39f59637a9f0 | 56 | |
mbedAustin | 7:a5ead1402704 | 57 | |
mbedAustin | 7:a5ead1402704 | 58 | // reverse the message |
mbedAustin | 7:a5ead1402704 | 59 | char temp; |
mbedAustin | 7:a5ead1402704 | 60 | for(int f = 0, l = n-1; f<l; f++,l--){ |
mbedAustin | 7:a5ead1402704 | 61 | temp = buffer[f]; |
mbedAustin | 7:a5ead1402704 | 62 | buffer[f] = buffer[l]; |
mbedAustin | 7:a5ead1402704 | 63 | buffer[l] = temp; |
mbedAustin | 7:a5ead1402704 | 64 | } |
mbedAustin | 7:a5ead1402704 | 65 | |
mbedAustin | 7:a5ead1402704 | 66 | // print reversed message to terminal |
momo7k | 9:39f59637a9f0 | 67 | printf("Sending message to Client: '%s'\n\r",buffer); |
momo7k | 9:39f59637a9f0 | 68 | |
mbedAustin | 7:a5ead1402704 | 69 | |
mbedAustin | 7:a5ead1402704 | 70 | // Echo received message back to client |
emilmont | 2:ec5ae99791da | 71 | client.send_all(buffer, n); |
emilmont | 1:5cebe0e38cd2 | 72 | if (n <= 0) break; |
emilmont | 1:5cebe0e38cd2 | 73 | } |
momo7k | 9:39f59637a9f0 | 74 | led1 = 0; |
emilmont | 1:5cebe0e38cd2 | 75 | client.close(); |
emilmont | 1:5cebe0e38cd2 | 76 | } |
emilmont | 1:5cebe0e38cd2 | 77 | } |
mbed_official | 8:23b1fba109b0 | 78 |