Not all of message getting read on serial port

Hi.

I have a device that spits out some device info in the following format:

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
BLE XXXXXXX 2
SVN revision: 997
MAC Address: E541A0C2F04E

The following code reads the last 6 bytes only:

String inData;

void setup() {
    Serial.begin(9600);
    inData.reserve(200);
}

void loop() {
    while (Serial.available())
    {
        char recieved = Serial.read();
        inData += recieved;         
    }

    Serial.println(inData);
}

The output looks like this:

ü
ü
ü E
ü E5
ü E54
ü E541
ü E541A
ü E541A0
ü E541A0C
ü E541A0C2
ü E541A0C2F
ü E541A0C2F0
ü E541A0C2F04
ü E541A0C2F04E

How can I get it to read the entire message and what exactly is this "ü" ??

Thanks,

Mark.

How can I get it to read the entire message

When at least one byte is available read from serial and put the byte in an array or add it to the running total. Keep going until you reach the end of the message which you determine by reading a special end of message character such as carriage return. Once you have the whole message do what you want with it.

I also suggest that you don't use Strings, use an array of chars terminated with a zero
Look at Serial input basics for examples

while (!Serial); // while the serial stream is not open, do nothing:

    inData.reserve(200);

If you KNOW the maximum length of data that you want to deal with, and have the memory to reserve that much space, you CLEARLY do not need a String.

How can I get it to read the entire message

What part of the message are you not getting?

and what exactly is this "ü"

Where did you assign an initial value to the String instance?

When at least one byte is available read from serial and put the byte in an array or add it to the running total. Keep going until you reach the end of the message which you determine by reading a special end of message character such as carriage return. Once you have the whole message do what you want with it.

I have tried using arrays but are causing me heart ache (cos I suck at programming) so for the time being I would just like to read into one big long data string.

I have tried using char instead of string:

char inData;

but it gives wont let me.

request for member 'reserve' in 'inData', which is of non-class type 'char'
request for member 'reserve' in 'inData', which is of non-class type 'char'

while (!Serial); // while the serial stream is not open, do nothing:

It is when it is open I want to process the data. All of it not just the last bit.

Where did you assign an initial value to the String instance?

I didn't assign an itial value to it.

What part of the message are you not getting?

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
BLE XXXXXXX 2
SVN revision: 997
MAC Address:

I am only getting the last 12 characters, the actual mac address. I want to read it all.

If you KNOW the maximum length of data that you want to deal with, and have the memory to reserve that much space, you CLEARLY do not need a String.

What do I use instead and how do I declare it???

cos I suck at programming

Reaching for crutches is not how to get better.

I have tried using char instead of string:

That's like having one bite of dinner, instead of dinner.

I didn't assign an itial value to it.

And you wonder why it contains crap?

I am only getting the last 12 characters, the actual mac address. I want to read it all.

If you aren't, then something else is reading and printing the data.

Where are you seeing that data?

MarkG123:
I have tried using arrays but are causing me heart ache

Have you tried using one of the simple examples in serial input basics that @UKHeliBob mentioned. Heart ache gone. :slight_smile:

...R

And you wonder why it contains crap?

It doesn't contain crap, its partial data.

Where are you seeing that data?

ü E541A0C2F04E is displayed on the serial monitor. When I connect using V-terminal the full info

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
BLE XXXXXXX 2
SVN revision: 997
MAC Address: E541A0C2F04E

is displayed.

This is what I should be seeing but for some reason "ü E541A0C2F04E" is all I am seeing on the Arduino serial monitor. Why does it ignore those other characters. I would expect to see the beginning of the data initially if "inData" was too small but somehow it is only seeing/reading the very end.

Any ideas???

Have you tried using one of the simple examples in serial input basics that @UKHeliBob mentioned. Heart ache gone.

They wont work. I just get a blank screen. There is too much control on them. I just want to do a basic long read.

What terminates the "long message"? How do you know when the entire message has been sent?

The end of the mac address is the end of the message. I can see it in full in V terminal but not in Arduino.

The end of the mac address is the end of the message. I can see it in full in V terminal but not in Arduino.

That really doesn't answer my question. How does V terminal know where the end of the mac address is? Is it a null termination character, a newline, a carriage return...what?

There is no termination sent with the data. The curser just sits at the end of the address as shown in the image attached.

Meanwhile I have discovered is that there is a short delay between first chunk and the mac address.

Is somehow the buffer getting cleared during this delay???

terminal.jpg

That white space at the end of the mac address is probably a non-printing character; perhaps a newline character. As a test, replace your read code with:

void loop() {
   char inputMessage[100];    // Too big?? Adjust accordingly
   int charsRead;

    while (Serial.available())
    {
        charsRead = Serial.readBytesUntil('\n', inputMessage, sizeof(inputMessage) - 1);  // Room for null
        inputMessage[charsRead] = '\0';        
    }

    Serial.println(inputMessage);
}

The code reads characters until it finds a newline ('\n') character or 99 characters, whichever comes first. If it doesn't work, try changing the newline character to a linefeed character. What happens?

It doesn't contain crap, its partial data.

The "ü" is partial data?

When I connect using V-terminal

What is V-terminal? Is IT displaying the other data, while the MAC address part some from the device itself?

With that code there is rapid display of these sort of characters:

üüü
üüü
üüü
üüü
üüü
ø
ø
ø
ø

I changed to \t, nothing gets printed.

With \r\n, lots of the following:

þü
þü
þü
þü
þü
þü
þü

The data isn't even mixed in with it.

What is V-terminal? Is IT displaying the other data, while the MAC address part some from the device itself?

Its actually Tera Term that I am using, just simple terminal software to read the serial port.
It displays the following:

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
BLE XXXXXXX 2
SVN revision: 997
MAC Address:

then a slight delay then adds the address to complete the following.

UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU
BLE XXXXXXX 2
SVN revision: 997
MAC Address: E541A0C2F04E

Hope this makes sense.

Using my original code I have powered my device on and off at different times and viewed the serial terminal.

I can now get the following:

h MAC Address: E541A0C2F04E üüÿ which is better but it still does not capture it all. It has garbage at the beginning and end.

Is it possible for the Arduino buffer to get cleared or over written??

Is it possible for the Arduino buffer to get cleared or over written?

Not unless you are doing it.

Have you actually read Reply#1 and Reply #6 ?

...R

I have read them and although it would be great to have code ready to copy and paste each application is different and copying from one to another presents its own problems. Storing data in arrays and such is not the problem I am having. The problem I am having is that serial.read is only reading the very end of the message as if the beginning does not exist. Maybe the message contains more that 64 bytes and the data is overwritten somehow.

MarkG123:
I have read them and although it would be great to have code ready to copy and paste each application is different and copying from one to another presents its own problems.

You seem to be having problems anyway. Maybe using a ready made and tested solution would help ?

The problem I am having is that serial.read is only reading the very end of the message as if the beginning does not exist. Maybe the message contains more that 64 bytes and the data is overwritten somehow.

That is the problem the code in serial input basics is intended to solve.

...R