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
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.
ü 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.
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?
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?
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.