How to Store or save new line Serial Read and Print as String

I've this code to send Hex to external hardware using serial write (UART Communication) : {0xbb, 0x00, 0x22, 0x00, 0x00, 0x22, 0x7e}
, and then the serial hardware give reply to arduino and I print using Serial.print: BB 02 22 00 11 D0 34 14 00 00 00 00 00 00 00 24 12 14 11 62 77 69 EA 7E
The problem it's actually printed as new line/ the hardware sent hex code one by one
ex : BB
02
22
etc.
It's printed as no line ending because I use Serial.print.
How can I recieve and store all data/hex code from hardware reply until it's complete (until 7E or 0x7e). And then print it as string.
I've tried some buffer code or serial event it doesn't work properly. Thanks

#include <SoftwareSerial.h>
SoftwareSerial serial(D5,D6);

//#define DEBUG
unsigned char incomingByte;

void sendIdentifyCmd ()
{
  serial.write (0xbb);    
  serial.write ((byte)0x00);
  serial.write (0x22);  
  serial.write ((byte)0x00);                  
  serial.write ((byte)0x00);                  
  serial.write (0x22);
  serial.write (0x7e);              
#ifdef DEBUG
  Serial.print (0xbb);
  Serial.print (0x00);
  Serial.print (0x22);
  Serial.print (0x00);
  Serial.print (0x00);
  Serial.print (0x22);
  Serial.print (0x7e);
  Serial.println ();
#endif
}

void setup ()
{
  Serial.begin (115200);
  serial.begin (115200);
  Serial.println ("begin initial Serial!\n");
}

void loop ()
{
  sendIdentifyCmd ();
  delay (2);
  while(serial.available () > 0)
  {
      incomingByte=serial.read ();
      if (incomingByte <= 0x0F) Serial.print("0");
      Serial.print (incomingByte,HEX);
      Serial.print (' ');
      
  }
  Serial.println ();
  delay (1000);
}

Serial.readBytesUntil()

aarg:
Serial.readBytesUntil()

i've tried this, but it still doesn't work properly, it shows some error message
Could you give more specific code? thanks in advanced

nautical_space:
i've tried this, but it still doesn't work properly, it shows some error message
Could you give more specific code? thanks in advanced

How did you use it (post code)? and what error message? (post)

aarg:
How did you use it (post code)? and what error message? (post)

I've tried this, but it just print
"abc: 2abc: 5"
Do you have any clue?

void loop ()
{
  sendIdentifyCmd ();
  delay (2);
  while(serial.available () > 0)
  {
      incomingByte=serial.read ();
      
      if (incomingByte <= 0x0F) Serial.print("0");
      Serial.print (incomingByte,HEX);
      Serial.print (' ');
  }
  Serial.println();
  delay(1000);
  while(serial.available()>0)
  {
    abc=serial.readBytesUntil(0xFF, buffer, 24);
    Serial.print("abc: ");
    Serial.print(abc, HEX);
  }
  Serial.println ();
  delay (1000);
}

Please post a complete sketch that shows the problem

Serial is just a stream. Aka, there is nothing to indicate a start or an end, each char just arrives (and thus sets Serial.available() > 0) as it's transmitted. But compare to the speed of the micro controller serial is just slow. Aka, you're able to do all the serial reading and processing and thus clearing the receive buffer (Serial.available() == 0) even though the other end is still in the process of sending data.

So way's to overcome the issue:
A) Is the received message fixed length? If so, just wait until you received that many before proceeding.
B) Does the message always end with the same and unique (aka, will not happen as data before the end) character of character sequence? If so, scan for that character (sequence) and only proceed after receiving that.
C) Non of the above, then just set a timeout. Aka, save the time (hint!: millis()) every time a character is received. And only print newline when more time has passed then would happen between characters. For example, 100ms or so. (But value will depend on the device you communicate with!)

septillion:
Serial is just a stream. Aka, there is nothing to indicate a start or an end, each char just arrives (and thus sets Serial.available() > 0) as it's transmitted. But compare to the speed of the micro controller serial is just slow. Aka, you're able to do all the serial reading and processing and thus clearing the receive buffer (Serial.available() == 0) even though the other end is still in the process of sending data.

So way's to overcome the issue:
A) Is the received message fixed length? If so, just wait until you received that many before proceeding.
B) Does the message always end with the same and unique (aka, will not happen as data before the end) character of character sequence? If so, scan for that character (sequence) and only proceed after receiving that.
C) Non of the above, then just set a timeout. Aka, save the time (hint!: millis()) every time a character is received. And only print newline when more time has passed then would happen between characters. For example, 100ms or so. (But value will depend on the device you communicate with!)

How can I scan the character (point B), I assume the end character is 0x7e or 7E as Hex printed

How can I scan the character (point B), I assume the end character is 0x7e or 7E as Hex printed

Read the input character by character and save them. When you receive 0x7E you know that the message is complete so use the saved data

UKHeliBob:
Read the input character by character and save them. When you receive 0x7E you know that the message is complete so use the saved data

How can I save it? I tried using buffer but it doesn't work, I use buffer like the sketch/script above. Maybe there're some mistake in my sketch.
Would you like to give me some example to save the character?

Take a look at Serial input basics - updated

UKHeliBob:
Take a look at Serial input basics - updated

#include <SoftwareSerial.h>
SoftwareSerial serial(D5,D6);

//#define DEBUG
unsigned char incomingByte;
const byte numBytes=32;
byte receivedBytes[32];
byte numReceived = 0;
boolean newData = false;

void sendIdentifyCmd ()
{
  serial.write (0xbb);    
  serial.write ((byte)0x00);
  serial.write (0x22);  
  serial.write ((byte)0x00);                  
  serial.write ((byte)0x00);                  
  serial.write (0x22);
  serial.write (0x7e);              
/*#ifdef DEBUG
  Serial.print (0xbb);
  Serial.print (0x00);
  Serial.print (0x22);
  Serial.print (0x00);
  Serial.print (0x00);
  Serial.print (0x22);
  Serial.print (0x7e);
  Serial.println ();
#endif*/
}
void recvBytesWithStartEndMarkers(){
  static boolean recvInprogress = false;
  static byte ndx = 0;
  char startMarker = 187;// (0xBB in Dec)
  char endMarker = 126;// (0x7E in Dec)
  byte rb;
  while (serial.available()>0 && newData==false){
    rb=serial.read();
    if (recvInprogress == true){
      if(rb!=endMarker){
        receivedBytes[ndx]=rb;
        ndx++;
        if(ndx>= numBytes){
          ndx=numBytes-1;
        }
      }
      else{receivedBytes[ndx] ='\0';
      recvInprogress = false;
      numReceived = ndx;
      ndx=0;
      newData = true;
    }
  }
}
}
void showNewData(){
  if(newData==true){
    Serial.print("ab: ");
    for (byte n = 0; n<numReceived;n++){
      Serial.print(receivedBytes[n], HEX);
    }
    Serial.println();
    newData=false;
  }
}

void setup ()
{
  Serial.begin (115200);
  serial.begin (115200);
  Serial.println ("begin initial Serial!\n");
}

void loop ()
{
  sendIdentifyCmd ();
  delay (2);
  recvBytesWithStartEndMarkers();
  showNewData();
}

I've tried this code but it doesn't show anything in serial monitor print. Can you give me some advice? thanks

What do you see if you print rb in the recvInprogress() function ?

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