skychan
asked on
socket com3 port reader
i want to read com3 port
i install javax.com version 2.0
but i try to this code (SimpleRead.java) I use jdeveloper
output is :Process exited with exit code 0.
Could you help me to can read port
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPort Identifier s();
while (portList.hasMoreElements( )) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SE RIAL) {
// if (portId.getName().equals(" COM1")) {
if (portId.getName().equals(" /dev/term/ a")) {
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp ", 2000);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream( );
} catch (IOException e) {}
try {
serialPort.addEventListene r(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAva ilable(tru e);
try {
serialPort.setSerialPortPa rams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationE xception e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEven t 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_BUF FER_EMPTY:
break;
case SerialPortEvent.DATA_AVAIL ABLE:
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffe r);
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
i install javax.com version 2.0
but i try to this code (SimpleRead.java) I use jdeveloper
output is :Process exited with exit code 0.
Could you help me to can read port
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleRead implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
public static void main(String[] args) {
portList = CommPortIdentifier.getPort
while (portList.hasMoreElements(
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SE
// if (portId.getName().equals("
if (portId.getName().equals("
SimpleRead reader = new SimpleRead();
}
}
}
}
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream(
} catch (IOException e) {}
try {
serialPort.addEventListene
} catch (TooManyListenersException
serialPort.notifyOnDataAva
try {
serialPort.setSerialPortPa
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationE
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
public void serialEvent(SerialPortEven
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_BUF
break;
case SerialPortEvent.DATA_AVAIL
byte[] readBuffer = new byte[20];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffe
}
System.out.print(new String(readBuffer));
} catch (IOException e) {}
break;
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It seems that for some reason, getPortIdentifiers () is not returning what it should - what is the output of this:
CommPortIdentifier obj = CommPortIdenfier.getPortId entifier ( "COM3" ) ;
CommPort port = obj.open ( "MyApplication", 100 ) ;
System.out.println ( port.getName () ) ;
CommPortIdentifier obj = CommPortIdenfier.getPortId
CommPort port = obj.open ( "MyApplication", 100 ) ;
System.out.println ( port.getName () ) ;
Girionis, isn't this happening a little too often? 'That' recommendation was not followed ;-) I wonder how somebody would work this code out if their code does not look like the one I posted.
ASKER
import java.util.*;
public class gokdk {
public static void main(String[] args) {
Enumeration e = CommPortIdentifier.getPort
System.out.print(e.hasMore
while(e.hasMoreElements())
System.out.println(e.nextE
}
}
print out : false