Kaynağa Gözat

Fixed typographical mistakes

Fixed some typographical mistakes, as well as communication loop algorithm - possibly making UART working properly.
Mateusz Bugdalski 13 yıl önce
ebeveyn
işleme
c8aff9eb49
2 değiştirilmiş dosya ile 32 ekleme ve 21 silme
  1. 26 20
      serial.c
  2. 6 1
      sprog.c

+ 26 - 20
serial.c

@@ -197,7 +197,7 @@ void serial_communicate(struct serial_device *port, int infd, int outfd) {
   FD_ZERO(&write_set);
   FD_ZERO(&except_set);
   FD_SET(port->fd, &read_set);
-  FD_SET(infd, &read_set);;
+  FD_SET(infd, &read_set);
   FD_SET(port->fd, &except_set);
   FD_SET(infd, &except_set);
   FD_SET(outfd, &except_set);
@@ -221,36 +221,41 @@ void serial_communicate(struct serial_device *port, int infd, int outfd) {
       n = write(port->fd, buf, n);
       ringbuf_move(&port->out_buf, n);
     }
-    
-    FD_ZERO(&read_set);
-    FD_ZERO(&write_set);
-    FD_ZERO(&except_set);
-    FD_SET(port->fd, &read_set);
-    FD_SET(infd, &read_set);
-    FD_SET(port->fd, &except_set);
-    FD_SET(infd, &except_set);
-    FD_SET(outfd, &except_set);
+
+
     
     if(FD_ISSET(infd, &read_set)) {
       bytes = sizeof(port->out_buf.data) - port->out_buf.size;
       if(bytes>0) {
 	n = read(infd, buf, bytes);
+	if(n==0)
+	  break;
 	ringbuf_append(&port->out_buf, buf, n);
-      } else
-	FD_CLR(infd, &read_set);
-      FD_SET(port->fd, &write_set);
+      }
     }
     
     if(FD_ISSET(port->fd, &read_set)) {
-      bytes = sizeof(port->in_buf.data) - port->out_buf.size;
+      bytes = sizeof(port->in_buf.data) - port->in_buf.size;
       if(bytes>0) {
-	n = read(infd, buf, bytes);
-	ringbuf_append(&port->out_buf, buf, n);
-      } else
-	FD_CLR(port->fd, &read_set);
-      FD_SET(outfd, &write_set);
+	n = read(port->fd, buf, bytes);
+	if(n==0)
+	  break;
+	ringbuf_append(&port->in_buf, buf, n);
+      }
     }
-      
+    
+    FD_ZERO(&write_set);
+    FD_ZERO(&read_set);
+    FD_ZERO(&except_set);
+    FD_SET(port->fd, &read_set);
+    FD_SET(infd, &read_set);
+    FD_SET(port->fd, &except_set);
+    FD_SET(infd, &except_set);
+    FD_SET(outfd, &except_set);
+    if(port->in_buf.size>0)
+      FD_SET(outfd, &write_set);
+    if(port->out_buf.size>0)
+      FD_SET(port->fd, &write_set);
   }
 }
 
@@ -332,6 +337,7 @@ void ringbuf_move(struct ring_buf *buf, unsigned int bytes) {
     if(p>=sizeof(buf->data))
       p -= sizeof(buf->data);
     buf->start = p;
+    buf->size -= bytes;
   }
 }
 

+ 6 - 1
sprog.c

@@ -35,10 +35,15 @@ void sprog_communicate(const struct sprog_family *fam, const char *port, int bau
     sprog_error("Unable to fork: %s\n", strerror(errno));
   else {
     if(pid==0) {
+      close(pstdin[0]);
+      close(pstdout[1]);
       serial_communicate(&dev, pstdout[0], pstdin[1]);
       fam->close(arg);
-    } else
+    } else {
+      close(pstdin[1]);
+      close(pstdout[0]);
       sprog_process(fam, arg, pstdin[0], pstdout[1]);
+    }
   }
 }