UART TCP client for LPC1786

Dependencies:   EthernetInterface mbed-rtos mbed

Fork of TCPSocket_HelloWorld by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 InterruptIn BTN_send(p14);
00005 DigitalOut led(LED1);
00006 DigitalOut led_rx(LED2);
00007 
00008 RawSerial device(p9, p10);  // tx, rx
00009 
00010 volatile int flagB = 0;
00011 volatile int flagS = 0;
00012 
00013 // Circular buffers for serial TX and RX data - used by interrupt routines
00014 const int buffer_size = 16;
00015 // might need to increase buffer size for high baud rates
00016 char rx_buffer[buffer_size+1];
00017 // Circular buffer pointers
00018 // volatile makes read-modify-write atomic 
00019 volatile int rx_in=0;
00020 
00021 EthernetInterface eth;
00022 TCPSocketConnection sock;
00023 
00024 void BTN_send_TCP()
00025 {
00026     led = !led;
00027     flagB = 200;
00028 }
00029 
00030 void UART_send_TCP()
00031 {
00032     int byte_counter = 0;
00033     
00034     led_rx = 1;
00035     while (device.readable() and (byte_counter != 5))
00036     {
00037         rx_buffer[rx_in] = device.getc();
00038         rx_in = (rx_in + 1) % buffer_size;
00039         
00040         byte_counter++;
00041     }
00042     flagS = 200;
00043     led_rx = 0;
00044 }
00045 
00046 int main() 
00047 {
00048     BTN_send.rise(&BTN_send_TCP);
00049     device.baud(9600);
00050     device.attach(&UART_send_TCP, Serial::RxIrq); 
00051     
00052     //device.printf("teste UART 2\n\r");  
00053     
00054     //EthernetInterface eth;
00055     eth.init(); //Use DHCP
00056     eth.connect();
00057     printf("IP Address is %s\n\r", eth.getIPAddress());
00058     
00059     //TCPSocketConnection sock;
00060     sock.connect("10.0.126.202", 8888);
00061     //sock.connect("192.168.1.7", 8888);
00062     
00063     char cmd[] = "teste ethernet MBED e C#! Visual Studio Rules!!!\n\r";
00064     char cmd2[] = "another test by button fuction flag\n\r";
00065     sock.send_all(cmd, sizeof(cmd)-1);
00066     
00067     while (true)
00068     {
00069         if(flagB == 200)
00070         {
00071             flagB = 0;
00072             sock.send_all(cmd2, sizeof(cmd2)-1);
00073         }  
00074         if(flagS == 200)
00075         {
00076             flagS = 0;
00077             sock.send_all(rx_buffer, 5);
00078             //sock.send_all(rx_buffer, sizeof(rx_buffer)-1);
00079             rx_in = 0;
00080         }  
00081     }   
00082       
00083     sock.close();
00084     
00085     eth.disconnect();
00086     
00087     while(1) {}
00088 }