//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);
ItuV250Modem atModem = new ModemFactory().getAtV250Modem(false, false, false, true); atModem.setModemInputStream(sPort.getInputStream()); atModem.setModemOutputStream(sPort.getOutputStream());
//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();
atModem.setRxtxTimeoutEnabled(false); sPort.close();