Establishing a Connection using javax.comm (commapi)

Posted on 2005-03-11
Medium Priority
Last Modified: 2008-03-17
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.
Question by:wilmow
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3

Accepted Solution

ashok3sep earned 1000 total points
ID: 13519958
SUN has come out with a new API for Communications.
It is available for download at the Java Developer Connection.
This package has sample code to read/write Communication ports.
This package should solve your problem.


Expert Comment

ID: 13520163
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.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
             System.out.println("Port type: " + portId.getPortType());
             if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                System.out.println("Port name: " + portId.getName());
                if (portId.getName().equals("/dev/term/a")) {
                    try {
                            serialPort = (SerialPort)
                            portId.open("SimpleWriteApp", 2000);
                            System.out.println("COM port  " + portId.getPortType() + "opened");                          
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try {
                    } catch (UnsupportedCommOperationException 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("Writing " + messageString);
                    } 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.



Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question