Serial monitor endless loop

Hi there, I am trying to send hex codes over the serial port. I am using the serial monitor to send these and then my code recieves the hex code and stores the binary version in an array and then it prints this binary version to the serial as a response .

void loop() {

   if(Serial.available() != 0){
    Serial.print("Recieved data!");
    //char hex_number = Serial.read();
    char hex_number = 0x2f; //0010 1111

    Serial.printf("Data recieved: %c", hex_number);

    char bit_number[8]={0,0,0,0,0,0,0,0};
    HexToBin(hex_number,bit_number);

    for(int i = 7 ; i >-1 ; i--){
        Serial.printf("%d",bit_number[i]);
    }

    Serial.write('\r\n');
    
   }

    Serial.flush();
   delay(200);
}

However ! with my current code above, it gets stuck in a loop of receiving and sending serial messages, My thoughts is that when it sends the reply over serial it is triggering the code to run again, I have added a delay and serial.flush() to try and resolve the issue but nothing seems to be fixing it.

Any help would be great thanks.

Welcome to the forum

The flush() does not remove characters from the Serial receive buffer, rather it waits until all data has been sent from the Serial transmit buffer. The delay() will just wait for 200 milliSeconds doing nothing. Neither will be helpful

Please post your complete sketch and confirm that you are using an ESP32 or ESP8266 board

Hi there, Thanks :slight_smile:
Here is my full code and im using it with an ESP32 Dev Module

#include <stdio.h>
#include <string.h>
void setup() {
Serial.begin(9600);
}

void HexToBin(char hex_number, char* bit_number) {
    int max = 128;
    for(int i = 7 ; i >-1 ; i--){
        bit_number [i] = (hex_number & max ) ? 1 : 0;
        max >>=1;
    }
}


void loop() {

   if(Serial.available() != 0){
    Serial.print("Recieved data!");
    //char hex_number = Serial.read();
    char hex_number = 0x2f; //0010 1111

    Serial.printf("Data recieved: %c", hex_number);

    char bit_number[8]={0,0,0,0,0,0,0,0};
    HexToBin(hex_number,bit_number);

    for(int i = 7 ; i >-1 ; i--){
        Serial.printf("%d",bit_number[i]);
    }

    Serial.write('\r\n');
    
   }

    Serial.flush();
   delay(200);
}

The bulk of the code runs when there is Serial data to be read but as currently written the Serial data is never removed from the buffer so once it is available() it remains available() so the code runs again, and again, and again.....

Right okay, What would you suggest as a fix then as an alternative for Serial.flush()?

Serial.flush() waits until the Serial output buffer is empty so is doing you no good

while (Serial.available())
{
  char junk = Serial.read();
}

will empty the Serial input buffer and you could, of course, put it in a function to call when needed

Amazing, thats fixed the issue! thank you.

I dont know if youd be up for offering a thought to my new issue ?..

Sending the hex string 0x2f ( Same as what i had hard coded here char hex_number = Serial.read(); //char hex_number = 0x2f; //0010 1111) does not seem to be working the same as hard coding the char to 0x2f

In fact sending any hex string 0x00 - 0xff prints the same reply "000110000"

Start by posting your revised sketch in a new reply here

Adding some serial prints i think that char hex_number is not being assigned properly from Serial.read

void loop() {

   if(Serial.available() != 0){
    Serial.print("Recieved data!\t");
    char hex_number = Serial.read();
    //char hex_number = 0x2f; //0010 1111

    Serial.printf("Data recieved: %c\n", hex_number);

    char bit_number[8]={0,0,0,0,0,0,0,0};
    HexToBin(hex_number,bit_number);

    Serial.printf("Transmitted reply:\t");
    for(int i = 7 ; i >-1 ; i--){
        Serial.printf(".%d",bit_number[i]);
    }

    Serial.write('\r\n');
    
    while(Serial.available()){
      char junk = Serial.read();
    }


    }

  
   delay(200);
}

Please post your whole sketch so that it can be copied with single click rather than having to copy/paste bits and pieces of it

Sorry here it is

#include <stdio.h>
#include <string.h>

void setup() {
Serial.begin(9600);
}

void HexToBin(char hex_number, char* bit_number) {
    int max = 128;
    for(int i = 7 ; i >-1 ; i--){
        bit_number [i] = (hex_number & max ) ? 1 : 0;
        max >>=1;
    }
}

void loop() {

   if(Serial.available() != 0){
    Serial.print("Recieved data!\t");
    char hex_number = Serial.read();
    //char hex_number = 0x2f; //0010 1111

    Serial.printf("Data recieved: %c\n", hex_number);

    char bit_number[8]={0,0,0,0,0,0,0,0};
    HexToBin(hex_number,bit_number);

    Serial.printf("Transmitted reply:\t");
    for(int i = 7 ; i >-1 ; i--){
        Serial.printf(".%d",bit_number[i]);
    }

    Serial.write('\r\n');
    
    while(Serial.available()){
      char junk = Serial.read();
    }


    }

  
   delay(200);
}

char hex_number = Serial.read();

This will read a single character into the hex_number variable. Is that really what you want to do ?

See Serial input basics - updated for some useful techniques to read a string into a variable

Right okay im going to do some reading, I think I understand the issue and solution now, thanks for your help :smiley:

Good luck. Come back if you need any more help

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.