Home arrow static arrow Java Programming [Archive] - How to use Javax.Comm API ?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to use Javax.Comm API ?
This topic has 2 replies on 1 page.

Posts:2
Registered: 7/12/04
How to use Javax.Comm API ?   
Jul 12, 2004 6:10 PM



 
i have downloaded a test program , SimpleRead.java, from the java doc. web page.
When i run the program, the show "port /dev/term/a not found"....
( I already put the comm.jar to the lib, and it compile successfully) , but seem that it can detect the com ports in my PC. conld anyone tell me what can i do??? did i miss something i haven't done ?

import java.io.*;
import java.util.*;
import javax.comm.*;

/**
* Class declaration
*
*
* @author
* @version 1.8, 08/03/00
*/
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;

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

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

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;
SimpleRead reader = new SimpleRead();
}
}
}
if (!portFound) {
System.out.println("port " + defaultPort + " not found.");
}

}

/**
* Constructor declaration
*
*
* @see
*/
public SimpleRead() {
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 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.print(new String(readBuffer));
} catch (IOException e) {}

break;
}
}

}
 

Posts:30
Registered: 2/9/01
Re: How to use Javax.Comm API ?   
Jul 12, 2004 7:10 PM (reply 1 of 2)



 
What's your O.S. ?
 

Posts:1,657
Registered: 1/8/98
Re: How to use Javax.Comm API ?   
Jul 12, 2004 8:44 PM (reply 2 of 2)



 
I don't know of any devices named "/dev/term/a" - and that's both Linux and Solaris.
 
This topic has 2 replies on 1 page.