+//***********************************************************
+// port_read()
+//
+//
+// This function becomes the long-running thread that snags
+// characters from an interface and passes them off to the
+// decoding routines. One copy of this is run for each read
+// thread for each interface.
+//***********************************************************
+gboolean read_port_data();
+
+void port_read() {
+// unsigned char cin, last;
+// gint i;
+ struct timeval tmv;
+ fd_set rd;
+
+// cin = (unsigned char)0;
+// last = (unsigned char)0;
+ gboolean success = TRUE;
+ GThread *my_thread = g_thread_self();
+
+ fprintf(stderr, "Enter port_read\n");
+
+ // We stay in this read loop until the port is shut down
+ while(port_data.active == DEVICE_IN_USE && _aprs_tty_thread == my_thread
+ && RCVR_UP == _aprs_tty_state && success == TRUE){
+
+ if (port_data.status == DEVICE_UP){
+
+ port_data.read_in_pos = 0;
+ port_data.scan = 1;
+
+
+ while (port_data.scan >= 0
+ && success == TRUE
+ //&& RCVR_UP == _aprs_tty_state
+ && (port_data.read_in_pos < (MAX_DEVICE_BUFFER - 1) )
+ && (port_data.status == DEVICE_UP)
+ && (_aprs_tty_thread == my_thread)
+ )
+ {
+
+ success = read_port_data();
+ }
+
+ }
+ if (port_data.active == DEVICE_IN_USE) {
+
+
+
+ // We need to delay here so that the thread doesn't use
+ // high amounts of CPU doing nothing.
+
+// This select that waits on data and a timeout, so that if data
+// doesn't come in within a certain period of time, we wake up to
+// check whether the socket has gone down. Else, we go back into
+// the select to wait for more data or a timeout. FreeBSD has a
+// problem if this is less than 1ms. Linux works ok down to 100us.
+// We don't need it anywhere near that short though. We just need
+// to check whether the main thread has requested the interface be
+// closed, and so need to have this short enough to have reasonable
+// response time to the user.
+
+//sched_yield(); // Yield to other threads
+
+ // Set up the select to block until data ready or 100ms
+ // timeout, whichever occurs first.
+ FD_ZERO(&rd);
+ FD_SET(port_data.channel, &rd);
+ tmv.tv_sec = 0;
+ tmv.tv_usec = 100000; // 100 ms
+ (void)select(0,&rd,NULL,NULL,&tmv);
+ }
+ }
+
+ fprintf(stderr, "End of port_read\n");
+}
+
+