Link to home
Start Free TrialLog in
Avatar of wilmow
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
Avatar of ashok3sep
ashok3sep

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ashok3sep
ashok3sep

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 {
                        serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,                            
                            SerialPort.STOPBITS_1,                            
                            SerialPort.PARITY_NONE);                            
                    } 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);
                       outputStream.write(messageString.getBytes());
                    } 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