This builds on TCPSocket_HelloWorld and others to read an in-coming buffer and then build and send a buffer. Very much work in progress

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPSocket_HelloWorld by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TCP_Serialisation.cpp Source File

TCP_Serialisation.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "NokiaLCD.h"
00004 
00005 NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type
00006 DigitalOut led1(LED1, "led1");
00007 DigitalOut led2(LED2, "led2");
00008 DigitalOut led3(LED3, "led3");
00009 DigitalOut led4(LED4, "led4");
00010 
00011 EthernetInterface eth; //, ethMask, ethNull, ethNull);
00012 
00013 typedef struct _data {
00014     int pan;
00015     int tilt;
00016 } data;
00017 data real;
00018 
00019 char InBound[300]; // incomming data
00020 char OutBound[300]; // outbound data
00021 char outpan[10];
00022 char outtilt[10];
00023 char header[] = "real ";
00024 char space[] = " ";
00025 
00026 void BuildOut(data outdata)
00027 {
00028     htons(outdata.pan);
00029     htons(outdata.tilt);
00030     sprintf(outpan, "%d", outdata.pan);
00031     sprintf(outtilt, "%d", outdata.tilt);
00032     strncpy(OutBound, header, sizeof(OutBound)-1);
00033     strcat(OutBound, outpan);
00034     strcat(OutBound, space);
00035     strcat(OutBound, outtilt);
00036 }
00037 
00038 int main()
00039 {
00040 
00041     eth.init("198.168.1.20","255.255.255.0","0.0.0.0");
00042     eth.connect();
00043 
00044     TCPSocketServer sock;
00045     if (sock.bind(80)>=0) {
00046         led4=1;
00047     }
00048     sock.listen(1);
00049     //create a TCPSocketConnection instance that will handle the incoming connection.
00050     TCPSocketConnection client;
00051 
00052     while (!client.is_connected()) {
00053         sock.accept(client);
00054     }
00055     led3 = 1;
00056 
00057     while(true) {
00058 
00059         int ret = client.receive(InBound, sizeof(InBound)-1);
00060 
00061         if (ret >=0) {
00062             led2=1;
00063             lcd.locate(0,1);
00064             lcd.printf(InBound);
00065             // Go head and do stuff the inbound data
00066             //Now send out your outbound packet with someinformation:
00067             real.pan = 321;  //as read by your devices
00068             real.tilt = 654; //as read by your devices
00069             BuildOut(real);
00070             lcd.locate(0,0);
00071             lcd.printf(OutBound);
00072             client.send(OutBound, sizeof(OutBound));
00073 
00074             ret = -1;
00075         }
00076     }
00077 }
00078 //lcd.printf("Values %d %d", real.pan, real.tilt);
00079 //strcat(OutBound, header);
00080 
00081 //char test[] = "123 456 789";
00082 
00083 //int first, sec, third;
00084 //if (sscanf(InBound, "%d %d %d", &first, &sec, &third) <= 3){
00085 //lcd.locate(0,1);
00086 //lcd.printf("first is %d", first);
00087 //lcd.locate(0,2);
00088 //lcd.printf("Sec is %d", sec);
00089 //lcd.locate(0,3);
00090 //lcd.printf("third is %d", third);
00091 //}