I tried to read bytes from Serial and put it into uint8_t and I tried to print the contents of uint8_t using Serial.print(); and it worked but there was a problem when I wanted to take part of the bytes in uint8_t to compare, but it didn't work even though what was sent by Serial was correct,
void RecvCommand()
{
uint8_t readBuffer[256];
int indexBuffer = 0;
while (Serial.available() > 0) {
if (indexBuffer < sizeof(readBuffer)) {
readBuffer[indexBuffer] = Serial.read();
indexBuffer++;
}
}
if (indexBuffer > 0) {
Serial.print("Received bytes: ");
for (int i = 0; i < indexBuffer; i++) {
Serial.print(readBuffer[i], HEX);
Serial.print(" ");
}
Serial.println();
}
if (indexBuffer > 0) {
if (readBuffer[0] == 0xFF && readBuffer[1] == 0x01) {
Serial.write(readBuffer, indexBuffer);
}
}
}
output from Serial Terminal
Success Send Command
Received response: "52656365697665642062797465733a204646200d0a52656365697665642062797465733a2031200d0a"
if changing “52656365697665642062797465733a204646200d0a52656365697665642062797465733a2031200d0a” to ascii then the output is
“
Bytes received: FF
Byte received: 1
”
here only the print loop part works, not the condition part.
So here uint8_t is not used for comparison, I don't or what the real problem is that the way arduino works is different,
that is the hex received from the arduino to the Serial Terminal if you change the hex to ascii it will look String
Bytes received: FF
Bytes received: 1
after this string there should be a return byte that has been sent from the arduino so Serial.write(readBuffer, indexBuffer); but the Serial Terminal does not receive this at all,, I hope you understand
Which Arduino are you using? In my experience, both are unsigned integers, so they are essentially the same. Are you perhaps thinking of a nibble, which is 4 bits? UARTs typically operate in 8-bit units.
We only knew what you are writing.
We don't sit next to your computer.
is this really that what you see in the serial monitor?
The code-snippet that you posted has different fixed text.
The code snippet prints
Bytes received:
versus
Received bytes:
do you see the difference.
It is unclear if your text
means that you changed the code posted above to be different.
or do you mean this part of the code
does not print?
You should be more careful and spend some more time for write a precise description of what you really did.
You should always post complete sketches. From the very first to the very last line.
If you used different code-versions post each and every code-version that you used
Give each code-version some keywords what this code-version is doing.
As you have only posted a code-snippet I am forced to post a code-snipped too
For debugging I would print the received bytes like this
that's why what is received is hex not string, sorry I forgot to explain about this,
so I tried to send 0xFF,0x01 from Serial to arduino whether arduino responds to this command.
back to the arduino code
if I make a condition like this, it doesn't work.
if (readBuffer[0] == 0xFF && readBuffer[1] == 0x01)
this doesn't work even if given the correct command
but if I create a condition like this
if (readBuffer[1] == 0x01)
it works but if I send a byte whose position 0x01 is not at address 01 in uint8_t the condition still works for example I send ,0xCE,0xFE,0x7D,0x01 it still works even though 0x01 is not at address 01 in uint8_t.
How many posts long do you want to waste time with quick but too short postings?
I second that.
post the complete sktech. From the very first line to the very last line of code
My experience is in 95% of all cases the bug is somehwere else than the user posting the question assumes where the bug is.
This can be done by a few mouse-clicks like described here
Additionally it will really help to make visible in the serial monitor each and every byte-value that the arduino received
@StefanL38. The code does print each and every received byte.
@anon66076557:
Your problem lies in the while loop. The received characters arrive at a much slower rate than the Arduino can handle them.
What happens is that the while loop receives the FF, but it hasn't yet received the 01.
So your code just prints that it has received FF and exits the RecvCommand function.
Next time that you call the RecvCommand() function it may not have received anything and so it does nothing.
Eventually, when you call the function it does receive the next character (01). But it is only one character so your code prints that it received the 01 and exits the function again.
Your code is looking for the two character sequence FF 01, but they arrive so slowly (as far as the Arduino is concerned) that it sees one character and then at a later time one more character.
There are various ways to handle this but what we need in order to help you properly is a clear explanation of what you are trying to do.
It would be nice to know what is sending the data to the Arduino and what sort of messages it can send.
Without this information all you can get is guesswork.
Which means you can modify this code to whatever you want.
I recommend that you modify it to send a start-character and an end-character
If you use an end-character as the very last byte you can use this endmarker to set a flag to "command-receiving completed" and then react on the command.
If you use and end-character sending your command can be as slow as you want it.
Even with seconds inbetween each sended character.
Unless you fully cooperate with us people will stop responding to you. In fact I think it would be a good idea if no one responded until we get a full code listing.
I have found the problem, I just move this variable out of the function and give a condition equal or greater than 5 like this, Serial.available() >= 5
uint8_t readBuffer[10];
int indexBuffer = 0;
It's like it's initializing uint8 multiple times and making the stored data problematic.
That code isn't guaranteed to work. You still run the risk of returning before the entire packet is received.
Do you have a way of knowing how many bytes are supposed to be in the packet? If you do, don't return until that many bytes are received (or a timeout elapses). Or if there's some way of telling the end of a packet, don't return until you get it.
If you don't, you have to set a timeout and wait until either the buffer is full or the timeout elapses. Neither is going to be terribly reliable.