Hello,
Here is my code, see if you can help me with it.
Code:
Public class TESTCOM implements srptEventListener {
private String ptcm;
private CommPortIdentifier ptid = null; / / Port ID
private srpt srpt; / / serial port
private BufferedReader fllx; / / sequential read port
/ *
* Method which initializes the serial port evenementiel
* /
Public void ModeEvenement(String ptcm) {
/ / fetch the ID of the port
try {
ptid = CommPortIdentifier.getPortIdentifier(ptcm);
} catch (NoSuchPortException e) {
}
/ / open port
try {
srpt = (srpt) ptid.open("ModeEvenement", 2000);
} catch (PortInUseException e) {
}
/ / get stream
try {
fllx =
new BufferedReader(
new InputStreamReader(srpt.getInputStream()));
} catch (IOException e) {}
/ / add the listener
try {
srpt.addEventListener(this);
} catch (TooManyListenersException e) {
}
/ / port settings
srpt.notifyOnDataAvailable(true);
try {
srpt.setsrptParams(
4800,
srpt.DATABITS_8,
srpt.STOPBITS_1,
srpt.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
System.out.System.out.println("open port, waiting to be read");
}
/ *
* Method which performs the reading of 7 frames on the serial port
* Once the reading is closed the flow of reading and the COM port
* /
Public void Readsrpt(){
int i =7;
String response = new String();
try {
System.out.System.out.println("i ="+ i);
while(i! =0){
System.out.System.out.println("We read the COM port\ n");
response = (String) fllx.readLine();
System.out.System.out.println(Answer);
i -;
System.out.System.out.println("i ="+ i);
}
} catch (IOException e) {}
/ / close the flow of reading
try {
fllx.close();
} catch (IOException e) {}
/ / Close COM port
srpt.close();
}
Public void serialEvent(srptEvent event) {
/ / event management on port:
/ / we do nothing except when the data are available
switch (event.getEventType()) {
box srptEvent.DATA_AVAILABLE :
this.Readsrpt();/ / if data available on launch reading
break;
default:
break;/ / nothing is done for other events
}
}
/ *
* Method which will scan all COM ports and test if the data come from the COM port scan
* /
Public void listPort(){
Enumeration listeports = CommPortIdentifier.getPortIdentifiers();
int typePort;
String GPSPortCOM;
while (listeports.hasMoreElements()){
ptid = (CommPortIdentifier) (CommPortIdentifier) listeports.nextElement();
if(ptid.getPortType()== CommPortIdentifier.PORT_SERIAL){
System.out.System.out.println("Port Name:"+ ptid.getName());
System.out.System.out.println("User"+ ptid.getCurrentOwner());
System.out.System.out.println("Use?"+ ptid.isCurrentlyOwned());
System.out.System.out.println("Type of port:"+ ptid.getPortType());
/ / We launch event management on ptid
this.ModeEvenement(ptid.getName());
}
}
}
Public static void hand(String[] args) {
/ / initialize the driver
Win32Driver w32Driver = new Win32Driver();
w32Driver.initialize();
TESTCOM test = new TESTCOM();
test.listPort();
}
}
Bookmarks