Update of /cvsroot/blob/blob/src/lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17799/src/lib
Modified Files:
serial.c
Log Message:
SerialInputString with millisecond timeout option.
Index: serial.c
===================================================================
RCS file: /cvsroot/blob/blob/src/lib/serial.c,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -d -r1.12 -r1.13
--- serial.c 4 Sep 2003 17:42:56 -0000 1.12
+++ serial.c 5 Feb 2004 18:16:03 -0000 1.13
@@ -250,6 +250,67 @@
* len is the length of array s _including_ the trailing zero,
* the function returns the number of bytes read _excluding_
* the trailing zero
+ *
+ * MILLISECOND TIMEOUT
+ */
+int SerialInputStringMs(char *s, const int len, const int timeout)
+{
+ u32 startTime, currentTime;
+ int c;
+ int i;
+ int numRead;
+ int skipNewline = 1;
+ int maxRead = len - 1;
+
+ startTime = TimerGetTime();
+
+ for(numRead = 0, i = 0; numRead < maxRead;) {
+ /* try to get a byte from the serial port */
+ while(serial_poll() == 0) {
+ currentTime = TimerGetTime();
+
+ /* check timeout value */
+ if((currentTime - startTime) >
+ (timeout * (TICKS_PER_MILLI))) {
+ /* timeout */
+ s[i++] = '\0';
+ return(numRead);
+ }
+ }
+
+ c = serial_read();
+
+ /* check for errors */
+ if(c < 0) {
+ s[i++] = '\0';
+ return c;
+ }
+
+ /* eat newline characters at start of string */
+ if((skipNewline == 1) && (c != '\r') && (c != '\n'))
+ skipNewline = 0;
+
+ if(skipNewline == 0) {
+ if((c == '\r') || (c == '\n')) {
+ s[i++] = '\0';
+ return(numRead);
+ } else {
+ s[i++] = (char)c;
+ numRead++;
+ }
+ }
+ }
+
+ return(numRead);
+}
+
+/*
+ * read a string with maximum length len from the serial port
+ * using a timeout of timeout seconds
+ *
+ * len is the length of array s _including_ the trailing zero,
+ * the function returns the number of bytes read _excluding_
+ * the trailing zero
*/
int SerialInputString(char *s, const int len, int timeout)
{
@@ -304,7 +365,6 @@
-
/*
* SerialInputBlock(): almost the same as SerialInputString(), but
* this one just reads a block of characters without looking at
|