+char ReadChar()
+{
+ fd_set readfs;
+ char ch = 0;
+ int ret;
+ struct timeval timeout;
+
+ timeout.tv_sec = READ_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: select return %i\n", ret);
+ return 0;
+ }
+
+ ret = read(giCoke_SerialFD, &ch, 1);
+ if( ret != 1 ) {
+ printf("ReadChar: ret != 1 (%i)\n", ret);
+ return 0;
+ }
+
+ return ch;
+}
+
+int Writef(const char *Format, ...)
+{
+ va_list args;
+ int len;
+
+ va_start(args, Format);
+ len = vsnprintf(NULL, 0, Format, args);
+ va_end(args);
+
+ {
+ char buf[len+1];
+ va_start(args, Format);
+ vsnprintf(buf, len+1, Format, args);
+ va_end(args);
+
+ #if DEBUG
+ printf("Writef: %s", buf);
+ #endif
+
+ return write(giCoke_SerialFD, buf, len);
+ }
+
+}
+
+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;
+
+ 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;
+ }
+ }
+
+ if( !ch ) return -1;
+ return i;
+}
+