Electronic – Microcontroller hanging during serial communication

embeddedmicrocontrollerserial

I was working on a project in which microcontroller is communicating serially with computer.

Initially it works properly but after some duration (i.e 2-3 hours) microcontroller get hanged.

I think it hangs due to serial communication with computer. To verify it I have written a program to for microcontroller as well as for computer to transmit and receive data serially.

In my program I am first transmitting string of five characters serially from computer. Microcontroller will receive the string and re-transmit the same string to computer.
Computer will receive the string transmitted by microcontroller.

Now I have transmitted string and a received string at computer, I am comparing both the strings and if they are same I am writing '1' in a file and '0' if they are different.

I want to know that my process to verify serial port communication is correct or not?

Best Answer

Long'ish serial wiring may incidentally suffer from bit errors. How about adding a checksum to every message (data frame) you send, so the receiving end can check that checksum and if it is wrong discard the whole data frame, probably asking for a retransmit.

A simple checksum mechanism can be achieved by EOR'ing all bytes in the frame and sending the resulting byte as checksum. The receiving end should be able to calculate the same checksum.