Jump to content
43oh

drosophila

Members
  • Content Count

    2
  • Joined

  • Last visited

Posts posted by drosophila

  1. Hi all!

    I started a tcp server on my TM4C1294. At the beginning, my code works. Then I get new ip, dns, gateway and subnet mask from serial port. 

    I want to restart my server with the new ip,dns,gw and subnet values. For this reason, I tried the below code...

    Unfortunately, the program stuck in "Ethernet.begin". 

    Is there anything in order to restart the ethernet and tcp server with new ip?

    
    //LIBRARIES
    #include <stdint.h>
    #include <stdbool.h>
    //#include "inc/hw_memmap.h"
    //#include "driverlib/debug.h"
    #include <Ethernet.h>
    #include <Wire.h>
    //#include "driverlib/pin_map.h"
    //#include "driverlib/gpio.h"
    //#include "driverlib/sysctl.h"
    //#include "driverlib/i2c.h"
    //#include "inc/hw_i2c.h"
    //#include "inc/hw_types.h"
    ////#include "XIO.h"
    //#include "string.h"
    
    
    
    byte mac[] = { 0x00, 0x1A, 0xB6, 0x02, 0xF0, 0x80 };
    
    int PortNumber = 1213;
    byte ip_arr[]  = { 77, 99, 0, 66 }; 
    byte dns_arr[]  = { 77, 99, 1, 1 }; 
    byte gw_arr[]  = { 77, 99, 1, 1 }; 
    byte mask_arr[]  = { 255, 0, 0, 0 }; 
    EthernetServer server = EthernetServer(PortNumber);
    
    
    
    void setup() {
      Serial.begin(19200);
      /*ip = ip_arr;
      dns = dns_arr;
      gw = gw_arr;
      mask = mask_arr;*/
      Ethernet.begin(mac, ip_arr, dns_arr, gw_arr, mask_arr);
      server.begin();
      delay(100);
    
    }
    
    int serial_read_data[25];
    int serial_index = 0;
    void get_serial_data(String serial_data)
    {
      unsigned char  incomingByte;
      char *sr_data = &serial_data[0];
      char str[40];
      //char *tab2 = new char [serial_data.length()+1];
      byte ip1 = 0;
      byte ip2 = 0;
      byte ip3 = 0;
      byte ip4 = 0;
    
      if(serial_data == "ver" )
      {
        Serial.println("\nVer: 1.01");
      }
      else if(serial_data == "show" )
      {
        Serial.print("IP = ");
        Serial.println(Ethernet.localIP());
        Serial.print("Subnet = ");
        Serial.println(Ethernet.subnetMask());
        Serial.print("Gateway = ");
        Serial.println(Ethernet.gatewayIP());
        Serial.print("Port = ");
        Serial.println(PortNumber, DEC); 
      }
      else if(serial_data == "U" )
      {
        //Ethernet.stop();
        Serial.println("\nserver rebooting-1");
        server.available().stop();
        Serial.println("\nserver rebooting-2");
        delay(100);
        //try this!!!!
        
        SysCtlReset();
        delay(100);
        
        Ethernet.begin(mac, ip_arr, dns_arr, gw_arr, mask_arr);//STUCKS HERE!!!!!!!!!
     
        delay(100);
        Serial.println("\nserver rebooting-3");
        server.begin();
        Serial.println("\n server rebooted...");
        Serial.print("IP = ");
        Serial.println(Ethernet.localIP());
        Serial.print("Subnet = ");
        Serial.println(Ethernet.subnetMask());
        Serial.print("Gateway = ");
        Serial.println(Ethernet.gatewayIP());
        Serial.print("Port = ");
        Serial.println(PortNumber, DEC); 
      }
      else
      {
        //char serial_data[serial_data.length()];
        //strcpy (tab2, serial_data.c_str());
        memset(str, '\0', sizeof(str));
        strncpy ( str, sr_data, 3);
        if(strcmp(str, "IP=") == 0)
        {
          Serial.println("\nIP Changing!!");
          ip1 = 100 * (sr_data[3] - 0x30) + 10 * (sr_data[4] - 0x30) + (sr_data[5] - 0x30);
          ip2 = 100 * (sr_data[7] - 0x30) + 10 * (sr_data[8] - 0x30) + (sr_data[9] - 0x30);
          ip3 = 100 * (sr_data[11] - 0x30) + 10 * (sr_data[12] - 0x30) + (sr_data[13] - 0x30);
          ip4 = 100 * (sr_data[15] - 0x30) + 10 * (sr_data[16] - 0x30) + (sr_data[17] - 0x30);
          Serial.println(ip1, DEC);
          Serial.println(ip2, DEC);
          Serial.println(ip3, DEC);
          Serial.println(ip4, DEC);
          ip_arr[0] = ip1;
          ip_arr[1] = ip2;
          ip_arr[2] = ip3;
          ip_arr[3] = ip4;
        }
        else if(strcmp(str, "SN=") == 0)
        {
          Serial.println("\nSubnet Changing!");
          ip1 = 100 * (sr_data[3] - 0x30) + 10 * (sr_data[4] - 0x30) + (sr_data[5] - 0x30);
          ip2 = 100 * (sr_data[7] - 0x30) + 10 * (sr_data[8] - 0x30) + (sr_data[9] - 0x30);
          ip3 = 100 * (sr_data[11] - 0x30) + 10 * (sr_data[12] - 0x30) + (sr_data[13] - 0x30);
          ip4 = 100 * (sr_data[15] - 0x30) + 10 * (sr_data[16] - 0x30) + (sr_data[17] - 0x30);
          Serial.println(ip1, DEC);
          Serial.println(ip2, DEC);
          Serial.println(ip3, DEC);
          Serial.println(ip4, DEC);
          mask_arr[0] = ip1;
          mask_arr[1] = ip2;
          mask_arr[2] = ip3;
          mask_arr[3] = ip4;
        }
        else if(strcmp(str, "GW=") == 0)
        {
          Serial.println("\nGW Changing!!");
          ip1 = 100 * (sr_data[3] - 0x30) + 10 * (sr_data[4] - 0x30) + (sr_data[5] - 0x30);
          ip2 = 100 * (sr_data[7] - 0x30) + 10 * (sr_data[8] - 0x30) + (sr_data[9] - 0x30);
          ip3 = 100 * (sr_data[11] - 0x30) + 10 * (sr_data[12] - 0x30) + (sr_data[13] - 0x30);
          ip4 = 100 * (sr_data[15] - 0x30) + 10 * (sr_data[16] - 0x30) + (sr_data[17] - 0x30);
          Serial.println(ip1, DEC);
          Serial.println(ip2, DEC);
          Serial.println(ip3, DEC);
          Serial.println(ip4, DEC);
          gw_arr[0] = ip1;
          gw_arr[1] = ip2;
          gw_arr[2] = ip3;
          gw_arr[3] = ip4;
        }
        else
        {
          strncpy ( str, sr_data, 5);
          if(strcmp(str, "Port=") == 0)
          {
            Serial.println("\nPort Changing!!");
            PortNumber = 10000 * (sr_data[5] - 0x30) + 1000 * (sr_data[6] - 0x30) 
                          + 100 * (sr_data[7] - 0x30) + 10 * (sr_data[8] - 0x30)
                          + (sr_data[9] - 0x30);
            Serial.println(PortNumber, DEC);
          }
        }
      }
    }
    
    //int i=0;
    void loop() {
    
    
       /*   Serial.println("\nI2C SEND");  
        delay(100);
        uint32_t pui32DataTx[3];
    
        I2CMasterSlaveAddrSet(I2C0_BASE, 0x20, false);
          Serial.println("\nMASTER SLAVE ADdRES SET"); 
         delay(100); 
        pui32DataTx[0] = 0x80 | 0x08;
        pui32DataTx[1] = 0x01;
          int i;
        for(i = 0; i < 2; i++){
          
          
          I2CMasterDataPut(I2C0_BASE, pui32DataTx[i]);
          Serial.println("\ncount: "); 
          Serial.println(i);
          I2CMasterControl(I2C0_BASE, I2C_MASTER_CMD_SINGLE_SEND);
          delay(100); 
          while(I2CMasterBusBusy(I2C0_BASE)); //Loop until the bus is no longer busy
          Serial.println("\neheheef: "); 
          Serial.println(i);
        }*/
    
            if (Serial.available() > 0) {
                    // read the incoming byte:
                  String incomingByte = Serial.readString();
    
                    // say what you got:
                    get_serial_data(incomingByte);
            }
    }

     

×
×
  • Create New...