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
TCP_Serialisation.cpp@12:cce8fa67de18, 2013-02-09 (annotated)
- Committer:
- acodd
- Date:
- Sat Feb 09 22:29:18 2013 +0000
- Revision:
- 12:cce8fa67de18
First main release
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
acodd | 12:cce8fa67de18 | 1 | #include "mbed.h" |
acodd | 12:cce8fa67de18 | 2 | #include "EthernetInterface.h" |
acodd | 12:cce8fa67de18 | 3 | #include "NokiaLCD.h" |
acodd | 12:cce8fa67de18 | 4 | |
acodd | 12:cce8fa67de18 | 5 | NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type |
acodd | 12:cce8fa67de18 | 6 | DigitalOut led1(LED1, "led1"); |
acodd | 12:cce8fa67de18 | 7 | DigitalOut led2(LED2, "led2"); |
acodd | 12:cce8fa67de18 | 8 | DigitalOut led3(LED3, "led3"); |
acodd | 12:cce8fa67de18 | 9 | DigitalOut led4(LED4, "led4"); |
acodd | 12:cce8fa67de18 | 10 | |
acodd | 12:cce8fa67de18 | 11 | EthernetInterface eth; //, ethMask, ethNull, ethNull); |
acodd | 12:cce8fa67de18 | 12 | |
acodd | 12:cce8fa67de18 | 13 | typedef struct _data { |
acodd | 12:cce8fa67de18 | 14 | int pan; |
acodd | 12:cce8fa67de18 | 15 | int tilt; |
acodd | 12:cce8fa67de18 | 16 | } data; |
acodd | 12:cce8fa67de18 | 17 | data real; |
acodd | 12:cce8fa67de18 | 18 | |
acodd | 12:cce8fa67de18 | 19 | char InBound[300]; // incomming data |
acodd | 12:cce8fa67de18 | 20 | char OutBound[300]; // outbound data |
acodd | 12:cce8fa67de18 | 21 | char outpan[10]; |
acodd | 12:cce8fa67de18 | 22 | char outtilt[10]; |
acodd | 12:cce8fa67de18 | 23 | char header[] = "real "; |
acodd | 12:cce8fa67de18 | 24 | char space[] = " "; |
acodd | 12:cce8fa67de18 | 25 | |
acodd | 12:cce8fa67de18 | 26 | void BuildOut(data outdata) |
acodd | 12:cce8fa67de18 | 27 | { |
acodd | 12:cce8fa67de18 | 28 | htons(outdata.pan); |
acodd | 12:cce8fa67de18 | 29 | htons(outdata.tilt); |
acodd | 12:cce8fa67de18 | 30 | sprintf(outpan, "%d", outdata.pan); |
acodd | 12:cce8fa67de18 | 31 | sprintf(outtilt, "%d", outdata.tilt); |
acodd | 12:cce8fa67de18 | 32 | strncpy(OutBound, header, sizeof(OutBound)-1); |
acodd | 12:cce8fa67de18 | 33 | strcat(OutBound, outpan); |
acodd | 12:cce8fa67de18 | 34 | strcat(OutBound, space); |
acodd | 12:cce8fa67de18 | 35 | strcat(OutBound, outtilt); |
acodd | 12:cce8fa67de18 | 36 | } |
acodd | 12:cce8fa67de18 | 37 | |
acodd | 12:cce8fa67de18 | 38 | int main() |
acodd | 12:cce8fa67de18 | 39 | { |
acodd | 12:cce8fa67de18 | 40 | |
acodd | 12:cce8fa67de18 | 41 | eth.init("198.168.1.20","255.255.255.0","0.0.0.0"); |
acodd | 12:cce8fa67de18 | 42 | eth.connect(); |
acodd | 12:cce8fa67de18 | 43 | |
acodd | 12:cce8fa67de18 | 44 | TCPSocketServer sock; |
acodd | 12:cce8fa67de18 | 45 | if (sock.bind(80)>=0) { |
acodd | 12:cce8fa67de18 | 46 | led4=1; |
acodd | 12:cce8fa67de18 | 47 | } |
acodd | 12:cce8fa67de18 | 48 | sock.listen(1); |
acodd | 12:cce8fa67de18 | 49 | //create a TCPSocketConnection instance that will handle the incoming connection. |
acodd | 12:cce8fa67de18 | 50 | TCPSocketConnection client; |
acodd | 12:cce8fa67de18 | 51 | |
acodd | 12:cce8fa67de18 | 52 | while (!client.is_connected()) { |
acodd | 12:cce8fa67de18 | 53 | sock.accept(client); |
acodd | 12:cce8fa67de18 | 54 | } |
acodd | 12:cce8fa67de18 | 55 | led3 = 1; |
acodd | 12:cce8fa67de18 | 56 | |
acodd | 12:cce8fa67de18 | 57 | while(true) { |
acodd | 12:cce8fa67de18 | 58 | |
acodd | 12:cce8fa67de18 | 59 | int ret = client.receive(InBound, sizeof(InBound)-1); |
acodd | 12:cce8fa67de18 | 60 | |
acodd | 12:cce8fa67de18 | 61 | if (ret >=0) { |
acodd | 12:cce8fa67de18 | 62 | led2=1; |
acodd | 12:cce8fa67de18 | 63 | lcd.locate(0,1); |
acodd | 12:cce8fa67de18 | 64 | lcd.printf(InBound); |
acodd | 12:cce8fa67de18 | 65 | // Go head and do stuff the inbound data |
acodd | 12:cce8fa67de18 | 66 | //Now send out your outbound packet with someinformation: |
acodd | 12:cce8fa67de18 | 67 | real.pan = 321; //as read by your devices |
acodd | 12:cce8fa67de18 | 68 | real.tilt = 654; //as read by your devices |
acodd | 12:cce8fa67de18 | 69 | BuildOut(real); |
acodd | 12:cce8fa67de18 | 70 | lcd.locate(0,0); |
acodd | 12:cce8fa67de18 | 71 | lcd.printf(OutBound); |
acodd | 12:cce8fa67de18 | 72 | client.send(OutBound, sizeof(OutBound)); |
acodd | 12:cce8fa67de18 | 73 | |
acodd | 12:cce8fa67de18 | 74 | ret = -1; |
acodd | 12:cce8fa67de18 | 75 | } |
acodd | 12:cce8fa67de18 | 76 | } |
acodd | 12:cce8fa67de18 | 77 | } |
acodd | 12:cce8fa67de18 | 78 | //lcd.printf("Values %d %d", real.pan, real.tilt); |
acodd | 12:cce8fa67de18 | 79 | //strcat(OutBound, header); |
acodd | 12:cce8fa67de18 | 80 | |
acodd | 12:cce8fa67de18 | 81 | //char test[] = "123 456 789"; |
acodd | 12:cce8fa67de18 | 82 | |
acodd | 12:cce8fa67de18 | 83 | //int first, sec, third; |
acodd | 12:cce8fa67de18 | 84 | //if (sscanf(InBound, "%d %d %d", &first, &sec, &third) <= 3){ |
acodd | 12:cce8fa67de18 | 85 | //lcd.locate(0,1); |
acodd | 12:cce8fa67de18 | 86 | //lcd.printf("first is %d", first); |
acodd | 12:cce8fa67de18 | 87 | //lcd.locate(0,2); |
acodd | 12:cce8fa67de18 | 88 | //lcd.printf("Sec is %d", sec); |
acodd | 12:cce8fa67de18 | 89 | //lcd.locate(0,3); |
acodd | 12:cce8fa67de18 | 90 | //lcd.printf("third is %d", third); |
acodd | 12:cce8fa67de18 | 91 | //} |