- void maybe_stop_monitor() {
- if (!need_monitor() && monitor_thread != null) {
- monitor_thread.interrupt();
- try {
- monitor_thread.join();
- } catch (InterruptedException e) {
- } finally {
- monitor_thread = null;
- }
- }
- }
-
- void maybe_start_monitor() {
- if (need_monitor() && monitor_thread == null) {
- monitor_thread = new Thread(this);
- monitor_thread.start();
- }
- }
-
- public void monitor(AltosSerialMonitor monitor) {
- synchronized(callbacks) {
- callbacks.add(monitor);
- maybe_start_monitor();
- }
- }
-
-
- public void unmonitor(AltosSerialMonitor monitor) {
- synchronized(callbacks) {
- callbacks.remove(monitor);
- maybe_stop_monitor();
- }
- }
-
- public void close() {
- synchronized(callbacks) {
- reader.close();
- maybe_stop_monitor();
- }
- }
-
- public void open(File serial_name) throws FileNotFoundException {
- reader.open(serial_name);
- serial_out = new FileOutputStream(serial_name);
- }
-
- public void open(CommPort comm_port) throws IOException {
- reader.open(comm_port);
- serial_out = comm_port.getOutputStream();
- }
-
- public void connect(String port_name) throws IOException, NoSuchPortException, PortInUseException {
- System.out.printf("Opening serial port %s\n", port_name);
- CommPort comm_port = new RXTXPort(port_name);
-// CommPortIdentifier port_identifier = CommPortIdentifier.getPortIdentifier(port_name);
-// CommPort comm_port = port_identifier.open("Altos", 1000);
- open(comm_port);
- }
-
- void init() {
- reader = new AltosSerialReader();
- callbacks = new LinkedList<AltosSerialMonitor>();