Home arrow static arrow Java Programming [Archive] - Java Communication Port Read
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Java Communication Port Read
This topic has 1 reply on 1 page.

Posts:2
Registered: 6/17/04
Java Communication Port Read  
Jun 28, 2004 10:40 AM



 
public SimpleRead(String portName) {	//Check for selected port or by default				if(portName!=null&&portName.length()>0) defaultPort = portName.toUpperCase();	System.out.println("\nPort Selected="+portName);	System.out.println("Delimiter="+DELIM);		readBuffer = new StringBuffer();	System.out.print("Scanning Comm Ports");	if(!Scan4Ports()) System.out.print(": No Ports Found on Machine");		while (portList.hasMoreElements()) {				portId = (CommPortIdentifier) portList.nextElement();		System.out.print(".");		if(FindPort(CommPortIdentifier.PORT_SERIAL, defaultPort)) {			System.out.println("\nPort Name="+portId.getName());			System.out.println("Port Type="+portId.getPortType());			break;		}	}		System.out.println("");	if(portId!=null){			try {					if(portId.isCurrentlyOwned()){System.out.println("Sorry, "+portId.getName()+" occupied by="+portId.getCurrentOwner());}			else if(portId.getName().indexOf("COM")>=0){				serialPort = (SerialPort) portId.open("JAVACOMM", 2000);				inputStream = serialPort.getInputStream();				serialPort.addEventListener(this);				serialPort.notifyOnDataAvailable(true);					try {					serialPort.setSerialPortParams(9600,					SerialPort.DATABITS_7,					SerialPort.STOPBITS_1,					SerialPort.PARITY_EVEN);					serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_XONXOFF_IN);				} catch (UnsupportedCommOperationException e) {e.getMessage(); }			}		} 		catch (PortInUseException e) {System.out.println(e.getMessage());}		catch (IOException e) {System.out.println(e.getMessage());}		catch (TooManyListenersException e) {System.out.println(e.getMessage());}				readThread = new Thread(this);		readThread.start();	}


The program was modified from the SimpleRead sample of the java communication API package. Could anyone help explaining why the last 2 line codes(calling thread) are for? Will the program work without them?
 

Posts:14,142
Registered: 99-04-02
Re: Java Communication Port Read  
Jun 28, 2004 1:33 PM (reply 1 of 1)



 
this:

readThread = new Thread(this);
readThread.start();

? The class implements Runnable and has a public void run() method, so this creates a new thread and starts it, which will call the run method and do whatever that does (presumably reads data as it becomes available).
 
This topic has 1 reply on 1 page.