wilmow
asked on
Establishing a Connection using javax.comm (commapi)
Hi all. I know how to claim com ports and then send data into them using the comm api. But I want to be able to establish a connection between 2 com ports (running the same program at both ends). I only want to send data into the port if i know the other end is listening. Also if the connection was broken I want to be able to warn the user. Does anyone know how i can implement this. Do i need to implement some sort of handshaking? Any code examples much appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.javaworld.com/jw-09-1998/indepth/SimpleWrite.java
import java.io.*;
import java.util.*;
import javax.comm.*;
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;
public static void main(String[] args) {
portList = CommPortIdentifier.getPort Identifier s();
while (portList.hasMoreElements( )) {
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("Port type: " + portId.getPortType());
if (portId.getPortType() == CommPortIdentifier.PORT_SE RIAL) {
System.out.println("Port name: " + portId.getName());
if (portId.getName().equals(" /dev/term/ a")) {
try {
serialPort = (SerialPort)
portId.open("SimpleWriteAp p", 2000);
System.out.println("COM port " + portId.getPortType() + "opened");
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream ();
} catch (IOException e) {}
try {
serialPort.setSerialPortPa rams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationE xception e) {}
System.out.println("Data bits set to 8");
System.out.println("Stop bits set to 1");
System.out.println("Parity set to NONE");
try {
System.out.println("Writin g " + messageString);
outputStream.write(message String.get Bytes());
} catch (IOException e) {}
}
}
}
}
}
Here in this example you open the output stream and then write the string to it
so simply calling Input stream and do the same procedure for read.
Regards,
Freedom
import java.util.*;
import javax.comm.*;
public class SimpleWrite {
static Enumeration portList;
static CommPortIdentifier portId;
static String messageString = "Hello, world!\n";
static SerialPort serialPort;
static OutputStream outputStream;
public static void main(String[] args) {
portList = CommPortIdentifier.getPort
while (portList.hasMoreElements(
portId = (CommPortIdentifier) portList.nextElement();
System.out.println("Port type: " + portId.getPortType());
if (portId.getPortType() == CommPortIdentifier.PORT_SE
System.out.println("Port name: " + portId.getName());
if (portId.getName().equals("
try {
serialPort = (SerialPort)
portId.open("SimpleWriteAp
System.out.println("COM port " + portId.getPortType() + "opened");
} catch (PortInUseException e) {}
try {
outputStream = serialPort.getOutputStream
} catch (IOException e) {}
try {
serialPort.setSerialPortPa
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationE
System.out.println("Data bits set to 8");
System.out.println("Stop bits set to 1");
System.out.println("Parity
try {
System.out.println("Writin
outputStream.write(message
} catch (IOException e) {}
}
}
}
}
}
Here in this example you open the output stream and then write the string to it
so simply calling Input stream and do the same procedure for read.
Regards,
Freedom