serial input discharge overload input

redowl
Posts: 5
Joined: Fri Jul 12, 2024 4:52 pm

serial input discharge overload input

Postby redowl » Fri Jul 12, 2024 5:01 pm

I'm looking for a solution to ensure that if a user continuously sends requests to the serial line the user finds himself having to wait for the end of the previously given command. for example, 3 seconds can pass for a response, any input sent to the serial port in this period of time should be discarded, this is to prevent bufferflow or panic attacks

Code: Select all


TaskHandle_t ccc_usb_task;
USBCDC USBSerial_cc;


char *ltrim(char *s)
{
    while(isspace(*s)) s++;
    return s;
}

char *rtrim(char *s)
{
    char* back = s + strlen(s);
    while(isspace(*--back));
    *(back+1) = '\0';
    return s;
}

char *trim(char *s)
{
    return rtrim(ltrim(s)); 
}


TaskHandle_t Task1;
void Task1code( void * parameter) {
  for(;;) {

  vTaskDelay(3000);
  Task1 = NULL;
  vTaskDelete(Task1);
  }
}

char * COMMAND;
char * VALUE;
void usb_as_serial_read(uint8_t * buf, size_t len){
          int kk = len;
          if(kk>60){
             kk  = 60;
          }
          char gg;
          char * command = "\0";
          char * value = "\0";
          char * result = "\0";

          int zzz = 0;
          int ccc = 0;
          bool pip = false;
          for(int i=0; i<kk; i++){
            gg = buf[i];
            result[i] = gg;
          }  
    command = std::strtok(result,"=");
	  value = std::strtok(nullptr, "=");


    COMMAND = trim(command);
    VALUE = trim(value);



    Serial.println(command);   
    Serial.println(value);      
    
    if(Task1!=NULL){
      xTaskCreatePinnedToCore(Task1code, "Task1", 4096, NULL, 70, &Task1, 0); 
    }
    return;
}

static void usb_serial_callback(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) {
if (event_base == ARDUINO_USB_CDC_EVENTS) {
    arduino_usb_cdc_event_data_t *data = (arduino_usb_cdc_event_data_t *)event_data;
    switch (event_id) {
      case ARDUINO_USB_CDC_CONNECTED_EVENT:    Serial.println("CDC CONNECTED"); break;
      case ARDUINO_USB_CDC_DISCONNECTED_EVENT: Serial.println("CDC DISCONNECTED"); break;
      case ARDUINO_USB_CDC_LINE_STATE_EVENT:   Serial.printf("CDC LINE STATE: dtr: %u, rts: %u\n", data->line_state.dtr, data->line_state.rts); break;
      case ARDUINO_USB_CDC_LINE_CODING_EVENT:
        Serial.printf(
          "CDC LINE CODING: bit_rate: %lu, data_bits: %u, stop_bits: %u, parity: %u\n", data->line_coding.bit_rate, data->line_coding.data_bits,
          data->line_coding.stop_bits, data->line_coding.parity
        );
        break;
        case ARDUINO_USB_CDC_RX_EVENT:
        {
          uint8_t buf[data->rx.len];
          size_t len = USBSerial_cc.read(buf, data->rx.len);
          usb_as_serial_read(buf, len);
          USBSerial_cc.flush();
        }
        break;
      case ARDUINO_USB_CDC_RX_OVERFLOW_EVENT: 
        Serial.printf("CDC RX Overflow of %d bytes", data->rx_overflow.dropped_bytes); 
      break;

      default: 
      break;
    }
  }
}


Who is online

Users browsing this forum: No registered users and 79 guests