Add timer

Add a timer to detect timeout.
Add some global value to do this.
Also change the type of buffer from char to unsigned char to solve a
warning.