Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: UIPEthernet mbed mbed-STM32F103C8T6
main.cpp
00001 /* 00002 * TCP/IP to Serial Converter. 00003 * 00004 * Created: 2015-04-10 00005 * Author: Zoltan Hudak 00006 * 00007 * ENC28J60 modul is connected to the mbed board to create a TCP/IP server. 00008 * The server is listening for clients at IP address 192.168.1.181, port 10001 (you can adapt it to your needs). 00009 * Binary data received from a client is transmitted over serial device and 00010 * in turn data received from the serial device is sent back to the TCP/IP client. 00011 * 00012 * How to use the converter: 00013 * Compile and save it to your mbed board. 00014 * Connect a ENC28J60 module to the mber board according to the pin assignment below. 00015 * Connect a serial device to the mbed serial port. Please notice that in case your serial device 00016 * is using an RS-232 connection a "serial TTL to serial RS-232" converter is needed! 00017 * To test or use this "TCP/IP to Serial Converter", create a virtual serial port on your PC as follows: 00018 * See "Using HW VSP powered by HW group" at <http://www.HW-group.com> 00019 * and download their free "HW VPS3 Virtual Serial Port" program from <http://www.hw-group.com/products/hw_vsp/index_en.html> 00020 * Install and run the application and go to tab "Virtual Serial Port". 00021 * Create a new virtual serial port, for example with name COM200, IP Address 192.168.1.181 and Port number 10001. 00022 * From now on, whatever data you send to COM200 on your PC will be redirected via Ethernet to this "TCP/IP to serial Converter" 00023 * which will pass it to the connected remote serial device. 00024 * In turn, the response from the remote serial device will be sent back to COM200 on your PC. 00025 * 00026 */ 00027 00028 #include "mbed.h" 00029 #include "UIPEthernet.h" 00030 00031 const int BAUD = 115200; // serial bit rate (change to match the speed of the connected serial device) 00032 uint8_t rxBuf[512]; // serial Rx buffer (adjust the size to your needs) 00033 uint8_t txBuf[512]; // serial Tx buffer (adjust the size to your needs) 00034 // MAC number must be unique within the connected network. Modify as appropriate. 00035 const uint8_t MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x06 }; 00036 // IP address must be also unique and compatible with your network. Change as appropriate. 00037 const IPAddress MY_IP(192, 168, 1, 181); 00038 // TCP/IP port (select a port) 00039 const uint16_t MY_PORT = 10001; 00040 00041 00042 // UIPEthernet is the name of a global instance of UIPEthernetClass. 00043 // Do not change the name! It is used within the UIPEthernet library. 00044 #if defined(TARGET_LPC1768) 00045 Serial serial(p9, p10); // serial: tx, rx 00046 UIPEthernetClass UIPEthernet(p11, p12, p13, p8); // spi: mosi, miso, sck, cs 00047 #elif defined(TARGET_NUCLEO_F103RB) || defined(TARGET_NUCLEO_L152RE) || defined(TARGET_NUCLEO_F030R8) \ 00048 || defined(TARGET_NUCLEO_F401RE) || defined(TARGET_NUCLEO_F302R8) || defined(TARGET_NUCLEO_L053R8) \ 00049 || defined(TARGET_NUCLEO_F411RE) || defined(TARGET_NUCLEO_F334R8) || defined(TARGET_NUCLEO_F072RB) \ 00050 || defined(TARGET_NUCLEO_F091RC) || defined(TARGET_NUCLEO_F303RE) || defined(TARGET_NUCLEO_F070RB) \ 00051 || defined(TARGET_KL25Z ) || defined(TARGET_KL46Z) || defined(TARGET_K64F) || defined(TARGET_KL05Z) \ 00052 || defined(TARGET_K20D50M) || defined(TARGET_K22F) \ 00053 || defined(TARGET_NRF51822) \ 00054 || defined(TARGET_RZ_A1H) 00055 Serial serial(PA_2, PA_3); // serial: tx, rx 00056 UIPEthernetClass UIPEthernet(D11, D12, D13, D10); // spi: mosi, miso, sck, cs 00057 #else 00058 // If your board/plaform is not present yet then uncomment 00059 // the following three lines and replace TARGET_YOUR_BOARD and other pins as appropriate. 00060 00061 //#elif defined(TARGET_YOUR_BOARD) 00062 //Serial serial(SERIAL_TX, SERIAL_RX); // serial: tx, rx 00063 //UIPEthernetClass UIPEthernet(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS); // spi: mosi, miso, sck, cs 00064 #endif 00065 00066 EthernetServer myServer = EthernetServer(MY_PORT); // create server 00067 volatile int rxLength = 0; // number of serial bytes received 00068 00069 /** 00070 * @brief Reads data from serial port 00071 * @note Called on arrival of new serial data 00072 * @param 00073 * @retval 00074 */ 00075 void onSerial(void) { 00076 while(serial.readable()) 00077 rxBuf[rxLength++] = serial.getc(); // read serial data 00078 } 00079 00080 /** 00081 * @brief Main 00082 * @note 00083 * @param 00084 * @retval 00085 */ 00086 int main(void) { 00087 serial.attach(&onSerial); 00088 00089 while(1) { 00090 EthernetClient client = myServer.available(); 00091 if(client) { // check for client connected 00092 size_t txLength = client.available(); 00093 if(txLength > 0) { 00094 rxLength = 0; // clear rx length 00095 txLength = client.read(txBuf, txLength); // read data received from the TCP/IP client 00096 for(int i = 0; i < txLength; i++) // send through serial 00097 serial.putc(txBuf[i]); 00098 wait_ms(50); // receive serial data from device 00099 client.write(rxBuf, rxLength); // send data to client over ethernet 00100 } 00101 } 00102 } 00103 }
Generated on Sun Jul 24 2022 02:48:09 by
1.7.2