+char ReadChar()
+{
+ fd_set readfs;
+ char ch = 0;
+ int ret;
+ struct timeval timeout;
+
+ timeout.tv_sec = 5; // 5 second timeout
+ timeout.tv_usec = 0;
+
+ FD_ZERO(&readfs);
+ FD_SET(giCoke_SerialFD, &readfs);
+
+ ret = select(giCoke_SerialFD+1, &readfs, NULL, NULL, &timeout);
+ if( ret == 0 ) return 0; // Timeout
+ if( ret != 1 ) {
+ printf("readchar return %i\n", ret);
+ return 0;
+ }
+
+ ret = read(giCoke_SerialFD, &ch, 1);
+ if( ret != 1 ) {
+ printf("ret = %i\n", ret);
+ return 0;
+ }
+
+ return ch;
+}
+
+int WaitForColon()
+{
+ fd_set readfs;
+ char ch = 0;
+
+ FD_SET(giCoke_SerialFD, &readfs);
+
+ while( (ch = ReadChar()) != ':' && ch != 0);
+
+ if( ch == 0 ) return -1; // Timeout
+
+ return 0;
+}
+
+int ReadLine(int len, char *output)
+{
+ char ch;
+ int i = 0;
+ int ret = 0;
+
+ for(;;)
+ {
+ ch = ReadChar();
+
+ if( i < len )
+ output[i++] = ch;
+
+ if( ch == '\0' ) {
+ break;
+ }
+ if( ch == '\n' || ch == '\r' ) {
+ if( i < len )
+ output[--i] = '\0';
+ break;
+ }
+ }
+
+ //printf("ReadLine: output=%s\n", output);
+
+ if( !ch ) return -1;
+ return i;
+}
+