javax.comm Help Needed

Hi, I have managed to install the java communications API and am running all the examples provided with the package well, on Windows 2000 Pro SP4 and jdk1.5.0_05. Now, the (allow me to call it) basic application of the javax.comm is BlackBox which I have no idea what it is meant to do. The documentations are just talking about how to set the Baud rate and what-have-you. Are they assuming knowledge of anything before you may use that package? If so, what is it? And where can I get the knowledge (eBook, article or any other means) from? Thanks.

[555 byte] By [Jamwaa] at [2007-10-2 21:19:05]
# 1

maybe u can check out this base class that i've developed

it basically contains methods to allow u to open/read/write to a rs232 ports using java comms api

import javax.comm.*;

import java.io.IOException;

import java.util.*;

import java.io.OutputStream;

import java.io.InputStream;

public class RS232BaseClass {

private SerialPort portName;

private CommPortIdentifier portID;

private OutputStream portOut = null;

private InputStream portIn = null;

public RS232BaseClass() {

portName = null;

portID = null;

portIn = null;

portOut = null;

}

// Method to re-init everything

// ****************************

public void reInit() {

this.portName = null;

this.portID = null;

this.portIn = null;

this.portOut = null;

}

// Method to retrieve list of ports in OS

// **************************************

public void getListofPorts() {

System.out

.println("Getting the List of Ports configured in this server");

Enumeration pList = CommPortIdentifier.getPortIdentifiers();

while (pList.hasMoreElements()) {

portID = (CommPortIdentifier) pList.nextElement();

System.out.println("Port :[" + portID.getName() + "]");

}

}

// Method to open specific port required

// **************************************

public void openPorts(String toOpenPort) {

System.out.println("Process is opening port: [" + toOpenPort + "]");

// Retrieve the port

try {

portID = CommPortIdentifier.getPortIdentifier(toOpenPort);

} catch (NoSuchPortException e) {

System.out

.println("No such port available to retrieve - Program will Exit!");

e.printStackTrace();

System.exit(1);

}

// Open the port

try {

portName = (SerialPort) portID.open(toOpenPort, 30000);

System.out.println("Port opened successfully!");

// Get Input Stream

portIn = portName.getInputStream();

// Get Output Stream

portOut = portName.getOutputStream();

} catch (PortInUseException e) {

System.out.println("Port is in use - Program will Exit!");

System.out.println("Owner is [" + e.currentOwner + "]");

e.printStackTrace();

System.exit(1);

} catch (IOException ioe) {

System.out

.println("IO Exception during stream IO - Program will Exit!");

ioe.printStackTrace();

System.exit(1);

}

}

// Method to set the port parameters - int baudrate, int dataBits, int stopBits, int parity

public void setPortParameters(int baudrate, int dataBits, int stopBits,

int parity) {

try {

portName.setSerialPortParams(baudrate, dataBits, stopBits, parity);

} catch (UnsupportedCommOperationException ucce) {

System.out

.println("Unsupported Comms Operation - Program will Exit!");

ucce.printStackTrace();

System.exit(1);

}

}

// Method to set Port Wait timing - Different for different devices

public void setPortReceiveTimeout(int timeOut) {

try {

this.portName.enableReceiveTimeout(timeOut);

} catch (UnsupportedCommOperationException ucce) {

System.out

.println("Unsupported Comms Operation - Program will Exit!");

ucce.printStackTrace();

}

}

// Method to Read contents from the port

// **************************************

public String readPort() {

System.out.println("Reading From port...");

StringBuffer buffer = new StringBuffer();

int ch = 0;

try {

while ((ch = portIn.read()) != -1)

buffer.append((char) ch);

portIn.close();

} catch (IOException e) {

e.printStackTrace();

}

System.out.println("String read is [" + buffer.toString()

+ "] Length is " + buffer.toString().length());

return buffer.toString();

}

// Method to write contents to the port

// ************************************

public void writePort(String outToPort) {

try {

portOut.write(outToPort.getBytes());

System.out.println("Writing out [" + outToPort + "]");

} catch (IOException e) {

System.out.println("IO Exception during Writing to port");

e.printStackTrace();

}

}

// Method to close the port

public void closePort() {

System.out.println("Closing Port");

this.portName.close();

}

}

stdouta at 2007-7-14 0:28:13 > top of Java-index,Java Essentials,Java Programming...
# 2
Thank you very very much
Jamwaa at 2007-7-14 0:28:13 > top of Java-index,Java Essentials,Java Programming...