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 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
:
BUFFER_SIZE = 124
then there's 132 of these charactersBUFFER_SIZE = 1024
then there's 1032 of these charactersBUFFER_SIZE = 1
then there's 9 of these charactersreceivedString
the string from stringToSend
isn't there BUT it will appear in the console.I would really appreciate any help even with refactoring/other coding tips.
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
WriteFile()
and ReadFile()
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).ReadFile()
should always come after event occurs and function WaitCommEvent()
catch it. The type of event is set by SetCommMask(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 because WaitCommEvent()
will end up in infinite loop waiting for receiving data. However without using WaitCommEvent()
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 using WaitCommEvent()
) 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.ReadFile()
does not read any bytes from the serial device because of your erroneous call with both last parameters set to NULL
. However, ReadFile()
did not return FALSE
.
The values 0xCC (printed sign-extended as 0xFFFFFFCC) that are filled into receivedString
are used as debug aid.
Your PC's compiler apparently locates the variable receivedString
before the variable stringToSend
on the stack. The additional bytes are most probably canary words.
When you now print receivedString
, the function printf
scans through the memory until it finds a terminating '\0'
. This is the case after the contents of stringToSend
was scanned additionally and copied to the output. So what you see is not received by ReadFile()
, but the existing characters of stringToSend
, which happens to be exactly what you expect to receive.
This visualizes the memory situation:
(lower address)... | receivedString |
padding | stringToSend |
...(higher address) |
---|---|---|---|---|
0xCC, 0xCC, ... 0xCC | 0xCC, ... 0xCC | 'S', 'e', ... '!', '\0' |
What should you do?