UART TCP client for LPC1786

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPSocket_HelloWorld by mbed official

Committer:
rodolforbcoutinho
Date:
Thu Oct 26 16:52:09 2017 +0000
Revision:
15:e6c1c6be2f44
Parent:
11:59dcefdda506
UART TCP client for LPC1786

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:bb128f0e952f 1 #include "mbed.h"
donatien 0:bb128f0e952f 2 #include "EthernetInterface.h"
donatien 0:bb128f0e952f 3
rodolforbcoutinho 15:e6c1c6be2f44 4 InterruptIn BTN_send(p14);
rodolforbcoutinho 15:e6c1c6be2f44 5 DigitalOut led(LED1);
rodolforbcoutinho 15:e6c1c6be2f44 6 DigitalOut led_rx(LED2);
rodolforbcoutinho 15:e6c1c6be2f44 7
rodolforbcoutinho 15:e6c1c6be2f44 8 RawSerial device(p9, p10); // tx, rx
rodolforbcoutinho 15:e6c1c6be2f44 9
rodolforbcoutinho 15:e6c1c6be2f44 10 volatile int flagB = 0;
rodolforbcoutinho 15:e6c1c6be2f44 11 volatile int flagS = 0;
rodolforbcoutinho 15:e6c1c6be2f44 12
rodolforbcoutinho 15:e6c1c6be2f44 13 // Circular buffers for serial TX and RX data - used by interrupt routines
rodolforbcoutinho 15:e6c1c6be2f44 14 const int buffer_size = 16;
rodolforbcoutinho 15:e6c1c6be2f44 15 // might need to increase buffer size for high baud rates
rodolforbcoutinho 15:e6c1c6be2f44 16 char rx_buffer[buffer_size+1];
rodolforbcoutinho 15:e6c1c6be2f44 17 // Circular buffer pointers
rodolforbcoutinho 15:e6c1c6be2f44 18 // volatile makes read-modify-write atomic
rodolforbcoutinho 15:e6c1c6be2f44 19 volatile int rx_in=0;
rodolforbcoutinho 15:e6c1c6be2f44 20
rodolforbcoutinho 15:e6c1c6be2f44 21 EthernetInterface eth;
rodolforbcoutinho 15:e6c1c6be2f44 22 TCPSocketConnection sock;
rodolforbcoutinho 15:e6c1c6be2f44 23
rodolforbcoutinho 15:e6c1c6be2f44 24 void BTN_send_TCP()
rodolforbcoutinho 15:e6c1c6be2f44 25 {
rodolforbcoutinho 15:e6c1c6be2f44 26 led = !led;
rodolforbcoutinho 15:e6c1c6be2f44 27 flagB = 200;
rodolforbcoutinho 15:e6c1c6be2f44 28 }
rodolforbcoutinho 15:e6c1c6be2f44 29
rodolforbcoutinho 15:e6c1c6be2f44 30 void UART_send_TCP()
rodolforbcoutinho 15:e6c1c6be2f44 31 {
rodolforbcoutinho 15:e6c1c6be2f44 32 int byte_counter = 0;
rodolforbcoutinho 15:e6c1c6be2f44 33
rodolforbcoutinho 15:e6c1c6be2f44 34 led_rx = 1;
rodolforbcoutinho 15:e6c1c6be2f44 35 while (device.readable() and (byte_counter != 5))
rodolforbcoutinho 15:e6c1c6be2f44 36 {
rodolforbcoutinho 15:e6c1c6be2f44 37 rx_buffer[rx_in] = device.getc();
rodolforbcoutinho 15:e6c1c6be2f44 38 rx_in = (rx_in + 1) % buffer_size;
rodolforbcoutinho 15:e6c1c6be2f44 39
rodolforbcoutinho 15:e6c1c6be2f44 40 byte_counter++;
rodolforbcoutinho 15:e6c1c6be2f44 41 }
rodolforbcoutinho 15:e6c1c6be2f44 42 flagS = 200;
rodolforbcoutinho 15:e6c1c6be2f44 43 led_rx = 0;
rodolforbcoutinho 15:e6c1c6be2f44 44 }
rodolforbcoutinho 15:e6c1c6be2f44 45
rodolforbcoutinho 15:e6c1c6be2f44 46 int main()
rodolforbcoutinho 15:e6c1c6be2f44 47 {
rodolforbcoutinho 15:e6c1c6be2f44 48 BTN_send.rise(&BTN_send_TCP);
rodolforbcoutinho 15:e6c1c6be2f44 49 device.baud(9600);
rodolforbcoutinho 15:e6c1c6be2f44 50 device.attach(&UART_send_TCP, Serial::RxIrq);
rodolforbcoutinho 15:e6c1c6be2f44 51
rodolforbcoutinho 15:e6c1c6be2f44 52 //device.printf("teste UART 2\n\r");
rodolforbcoutinho 15:e6c1c6be2f44 53
rodolforbcoutinho 15:e6c1c6be2f44 54 //EthernetInterface eth;
donatien 0:bb128f0e952f 55 eth.init(); //Use DHCP
donatien 0:bb128f0e952f 56 eth.connect();
rodolforbcoutinho 15:e6c1c6be2f44 57 printf("IP Address is %s\n\r", eth.getIPAddress());
donatien 0:bb128f0e952f 58
rodolforbcoutinho 15:e6c1c6be2f44 59 //TCPSocketConnection sock;
rodolforbcoutinho 15:e6c1c6be2f44 60 sock.connect("10.0.126.202", 8888);
rodolforbcoutinho 15:e6c1c6be2f44 61 //sock.connect("192.168.1.7", 8888);
donatien 0:bb128f0e952f 62
rodolforbcoutinho 15:e6c1c6be2f44 63 char cmd[] = "teste ethernet MBED e C#! Visual Studio Rules!!!\n\r";
rodolforbcoutinho 15:e6c1c6be2f44 64 char cmd2[] = "another test by button fuction flag\n\r";
rodolforbcoutinho 15:e6c1c6be2f44 65 sock.send_all(cmd, sizeof(cmd)-1);
emilmont 7:65188f4a8c25 66
rodolforbcoutinho 15:e6c1c6be2f44 67 while (true)
rodolforbcoutinho 15:e6c1c6be2f44 68 {
rodolforbcoutinho 15:e6c1c6be2f44 69 if(flagB == 200)
rodolforbcoutinho 15:e6c1c6be2f44 70 {
rodolforbcoutinho 15:e6c1c6be2f44 71 flagB = 0;
rodolforbcoutinho 15:e6c1c6be2f44 72 sock.send_all(cmd2, sizeof(cmd2)-1);
rodolforbcoutinho 15:e6c1c6be2f44 73 }
rodolforbcoutinho 15:e6c1c6be2f44 74 if(flagS == 200)
rodolforbcoutinho 15:e6c1c6be2f44 75 {
rodolforbcoutinho 15:e6c1c6be2f44 76 flagS = 0;
rodolforbcoutinho 15:e6c1c6be2f44 77 sock.send_all(rx_buffer, 5);
rodolforbcoutinho 15:e6c1c6be2f44 78 //sock.send_all(rx_buffer, sizeof(rx_buffer)-1);
rodolforbcoutinho 15:e6c1c6be2f44 79 rx_in = 0;
rodolforbcoutinho 15:e6c1c6be2f44 80 }
rodolforbcoutinho 15:e6c1c6be2f44 81 }
donatien 0:bb128f0e952f 82
emilmont 7:65188f4a8c25 83 sock.close();
donatien 0:bb128f0e952f 84
emilmont 7:65188f4a8c25 85 eth.disconnect();
donatien 5:01f6c3e112af 86
emilmont 9:4757a976148d 87 while(1) {}
donatien 0:bb128f0e952f 88 }