Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #3718 > unrolled thread

Serial Port Communication

Started by"HulkingNightCrawler" <hulkingnightcrawler@THRWHITE.remove-dii-this>
First post2011-04-27 15:46 +0000
Last post2011-04-27 15:46 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.java.gui


Contents

  Serial Port Communication "HulkingNightCrawler" <hulkingnightcrawler@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000
    Re: Serial Port Communica "tar" <tar@THRWHITE.remove-dii-this> - 2011-04-27 15:46 +0000

#3718 — Serial Port Communication

From"HulkingNightCrawler" <hulkingnightcrawler@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
SubjectSerial Port Communication
Message-ID<908eadf1-c4b9-4fd2-b0c0-dedc24dedd04@56g2000hsm.googlegroups.com>
  To: comp.lang.java.gui
I am trying to read data off a serial port and then display the data
in a GUI and I cannot seem to access the GUI in order to do this.  For
some reason whenever I try to append to the text box the program seems
to stall out and I cannot figure out why?  Any help would be
appreciated, thanks!!


import java.io.*;
import java.util.*;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import gnu.io.*;


public class Data implements Runnable, SerialPortEventListener{
	static CommPortIdentifier	portId;
	static Enumeration	    	  portList;
	InputStream		             inputStream;
	SerialPort		     	       serialPort;
	Thread		      			readThread;
	static Shell			  	shell;
	static Display			      dispMain;
	static Text				txtOutput;

	/**
	 * Method declaration
	 *
	 *
	 * @param args
	 *
	 * @see
	 */
	public static void main(String[] args) {
		boolean		      portFound = false;
		String		      defaultPort;

		if (args.length > 0) {
			defaultPort = args[0];
		}

		buildGUI();

		portList = CommPortIdentifier.getPortIdentifiers();

		while (portList.hasMoreElements()) {
			portId = (CommPortIdentifier) portList.nextElement();
			if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
				if (portId.getName().equals(defaultPort)) {
					System.out.println("Found port: "+defaultPort);
					portFound = true;
					Data reader = new Data();
				}
			}
		}
		if (!portFound) {
			System.out.println("port " + defaultPort + " not found.");
		}

		while (!shell.isDisposed())
		{
			if (!dispMain.readAndDispatch()) dispMain.sleep();
		}
	}

	/**
	 * Constructor declaration
	 *
	 *
	 * @see
	 */
	public Data() {
		try {
			serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
		} catch (PortInUseException e) {}

		try {
			inputStream = serialPort.getInputStream();
		} catch (IOException e) {}

		try {
			serialPort.addEventListener(this);
		} catch (TooManyListenersException e) {}

		serialPort.notifyOnDataAvailable(true);

		try {
			serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
					SerialPort.STOPBITS_1,
					SerialPort.PARITY_NONE);
		} catch (UnsupportedCommOperationException e) {}



		readThread = new Thread(this);

		readThread.start();


	}

	/**
	 * Method declaration
	 *
	 *
	 * @see
	 */
	public void run() {
		try {
			Thread.sleep(20000);
		} catch (InterruptedException e) {}
	}

	public static void buildGUI()
	{
		dispMain = new Display();
		shell = new Shell(dispMain);

		shell.setText("Parking Meter Test Bench Data");
		shell.setSize(800,600);
		shell.open();

		txtOutput = new Text(shell, SWT.BORDER | SWT.H_SCROLL |
SWT.V_SCROLL);
		txtOutput.setBounds(10,90,200,200);
		txtOutput.setEditable(false);
		txtOutput.setText("Test Text\n");
		txtOutput.append("Hello World");
		txtOutput.append("Hello Look at Me");
		txtOutput.setLocation(10, 150);



	}

	/**
	 * Method declaration
	 *
	 *
	 * @param event
	 *
	 * @see
	 */
	public void serialEvent(SerialPortEvent event) {
		switch (event.getEventType()) {

		case SerialPortEvent.BI:

		case SerialPortEvent.OE:

		case SerialPortEvent.FE:

		case SerialPortEvent.PE:

		case SerialPortEvent.CD:

		case SerialPortEvent.CTS:

		case SerialPortEvent.DSR:

		case SerialPortEvent.RI:

		case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
			break;

		case SerialPortEvent.DATA_AVAILABLE:
			byte[] readBuffer = new byte[20];

			try {
				while (inputStream.available() > 0) {
					int numBytes = inputStream.read(readBuffer);
					System.out.println("1233433423423");
				}

				System.out.print(new String(readBuffer));
				txtOutput.append(new String(readBuffer));

			} catch (IOException e) {}

			break;
		}
	}

}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [next] | [standalone]


#3731 — Re: Serial Port Communica

From"tar" <tar@THRWHITE.remove-dii-this>
Date2011-04-27 15:46 +0000
SubjectRe: Serial Port Communica
Message-ID<ymiy74q4qn7.fsf@blackcat.isi.edu>
In reply to#3718
  To: comp.lang.java.gui
HulkingNightCrawler <TravisPomeroy@gmail.com> writes:

> I am trying to read data off a serial port and then display the data
> in a GUI and I cannot seem to access the GUI in order to do this.  For
> some reason whenever I try to append to the text box the program seems
> to stall out and I cannot figure out why?  Any help would be
> appreciated, thanks!!

Well, the first thing I would try to do would be to eliminate the empty
exception catch clauses.  At the very least you should put something in
that prints the error message and the stack trace.

Silently catching exceptions is a terrible way to debug anything, and is
just downright stupid programming practice in any case.

-- 
Thomas A. Russ,  USC/Information Sciences Institute

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.gui


csiph-web