My problem is that I seem to be unable to send correct data over the Serial interface.
I use an Arduino Mega 2560. On the Serial2 a NFC reader sends data to the board. This Part works fine. Then I want to send the received data over the Serial1 to an connected SBC-AVL Box which forwards it to a Server. And here is where the problems start.
- I never get the same result twice on the Server, even when I don't forward the data but send some dummy data
- The results differe completelly in length if I use the three following Syntaxes:
2.2) char* test = "HelloTestCode!"); Serial1.write(test);
2.3) uint8_t test = "HelloTestCode!"); Serial1.write(test, sizeof(test));
As you can see I always want to send the same "string". The results for the different tries look as follows (in Hex). The length stays the same but the content changes every time.
- FE 01 FF
- FE 0E 4C 65 6E 6C 6F 54 65 73 74 E3 6F 64 65 B1
- FE 0E E8 75 6F EF 6F 77 F5 7B 7F 7B EF 77 75 FD
Yes I have read the documentation. And yes the SBC-AVL is configured on the same Baude-Rate (115200).
Can anybody tell my what I'm doing wrong? More specific:
- Why the length difference between uint8_t and char*/"HelloTestCode!"?
- Why is the result different every time?
The write(byte) function sends one byte. The write(char, number of bytes) function will send a string. So 2.1 and 2.2 are not using the write function properly. 2.3 should work [except the stray ) in "HelloTestCode!")]. Are you sure that the problem is not how the box interprets the data and sends the data to the server? What do you get if you write to the serial monitor instead of Serial1?
Thank you for your answer, you helped my a lot.
To answer your question -> The Serial Monitor shows in all three cases the string I wanted to send (which was also a reason why I coulden't understand the different results)
And no I'm still checking the box if it is a problem on this side (which seems likely after your explanation)
If 2.1 is not a correct use of the function are you able to explaine the example from the documentary? (see: Serial.write() - Arduino Reference) There they write function is used the way I was trying with 2.1
Thanks for your help
I am embarrassed. Now it is time for me to thank you. My understanding of the write function was flawed
The Serial Monitor shows in all three cases the string I wanted to send (which was also a reason why I coulden't understand the different results)
That works for me to, now that I actually tested. I am sorry if my bogus answer caused confusion.
I understand the function better now. If the array is a null terminated array (as 2.1 and 2.2 are) write can know the length of the array so no problem. 2.3 is not null terminated (not a char array) so must have the length included.
Thank you for the explanation now it is completely clear to me how the function works.
Now I know for sure that the problem is somewhere with the configuration of my sbc-avl box.
We both learned something. A good day at the forum.