Using atmodem4j with rxtx on the same machine

  1. Setup rxtx port (19200 bit/s, 8 bit data 1 stop bit, no parity, DTR/RST on
    //replace string with actual com port on Windows "COM1" or similar.
    CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier("/dev/ttyUSB0");
    
    // Open the port represented by the CommPortIdentifier object. Give
    // the open call a relatively long timeout of 30 seconds to allow
    // a different application to reliquish the port if the user
    // wants to.
    SerialPort sPort = (SerialPort) portId.open(Modem.class.getName(), 30000);
    sPort.setSerialPortParams(19200, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
    sPort.enableReceiveTimeout(1000);
    sPort.setDTR(true);
    sPort.setRTS(true);
    sPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
  2. Create Modem instance and connect rxtx with that instance
    ItuV250Modem atModem = new ModemFactory().getAtV250Modem(false, false, false, true);
    atModem.setModemInputStream(sPort.getInputStream());
    atModem.setModemOutputStream(sPort.getOutputStream());
  3. Work with te modem
    //make a call and get a connection
    Connection conn = atModem.dial("9876543210");
    // setup receiver tread which puts out received data
    Thread t = new Thread(new Runnable() {
        final InputStream is = conn.getInputStream();
        public void run() {
            while (true) {
                try {
                    int i = is.read();
                    if (i == -1) {
                        System.err.println("Console Printer Cancelled");
                        break;
                    } else {
                        System.out.print((char) i);
                    }
                } catch (IOException ex) {
                    System.err.println("Console Printer ERROR");
                }
            }
        }
    });
    t.setDaemon(true);
    t.start();
    
    //send some data
    conn.getOutputStream().write("Hello world\r\n".getBytes());
    //close the connection, disconnect the call
    conn.close();
  4. Close the modem and the serial port
        atModem.setRxtxTimeoutEnabled(false);
        sPort.close();