What I'm trying to do:
I'm trying to write win console which will communicate with my ATMega2560 board via UART. Now it should send string saved in stringToSend
to the MCU which should be send back to the PC. String sent from MCU should be saved in receivedString
and then written to the win console window.
What the code does:
What it does now is that after I send the string to MCU it will come back, but on the console I see an unexpected sequence of '╠' characters preceding the echoed string, just like this:
You sent:
╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠Sended test string!
EDIT: Number of characters is consistent if BUFFER_SIZE
is constant, but it scales with BUFFER_SIZE
:
- If
BUFFER_SIZE = 124
then there's 132 of these characters - If
BUFFER_SIZE = 1024
then there's 1032 of these characters - If
BUFFER_SIZE = 1
then there's 9 of these characters
Weird things (probably just for me):
- The weirdest thing is that when I use breakpoints and look into
receivedString
the string fromstringToSend
isn't there BUT it will appear in the console. - When I send data via Termite (console for sending data via serial communication) the problem disappear and after that I'm receiving ONLY the right string even with my console.
The questions:
- Am I supposed to setup some bit/flag in the MCU which cause this behavior?
- May it be problem with format of the string?
- Am I just completely missing something what I should do before/after I send the data from PC/MCU?
What did I try:
- I tried to find some solution on internet but I can't find nothing
- As experimentation I tried nothing because honestly I'm completely clueless what the problem could be
I would really appreciate any help even with refactoring/other coding tips.
Answers to comments:
- Could you print values as hexadecimal and add number of these elements?
The output:
ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc >ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc >ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc >ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc >ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc ffffffcc 53 65 6e 64 65 64 20 74 65 73 74 20 73 74 72 69 6e 67 21
Number of these characters is always
BUFFER_SIZE + 8
and the character in decimal is represented as-52
Solution:
- I already added 4th parameter into functions
WriteFile()
andReadFile()
and the problem wasn't there (but it definitely can cause some problems because documentation says the last two parameters can't be both NULL). - I found somewhere in documentation that
ReadFile()
should always come after event occurs and functionWaitCommEvent()
catch it. The type of event is set bySetCommMask(mainCom, EV_RXCHAR)
so it occur when there's character in input buffer (there's documentation to function SetCommMask()). This solution shows the error is caused by the board which doesn't send the data becauseWaitCommEvent()
will end up in infinite loop waiting for receiving data. However without usingWaitCommEvent()
it seems like the sent data are held by some kind of memory and read from there so even when the board doesn't send them, they're printed (I really don't understand why but see @thebusybee answer). What actually repair the problem is just simple reconnecting the board every time I boot windows (after reconnecting is everything functional even with usingWaitCommEvent()
) which leads me into completely different question if windows is sending some weird stuff into USB ports when booting so it mess with the board but that's for another topic. It could by also caused by windows using different communication parameters (as baud rate etc.) while booting. But that's for another/different question.