Search code examples
javasmsgsmmodemserial-communication

Java Serial Communication PortInUseException


I have an SMS-based Java application using CommV3 drivers for Serial communication to a GSM Modem. I have two separate codes, one for sending messages and one for receiving messages. Each code works perfectly when executed individually.

Now, when I want to integrate both these codes, I get a PortInUseException, which might be obvious. But, I am not sure where to start from.

Could I get pointers/ links/ tutorials where I could start resolving my issue. I do not have a clue where I should start from.

Thanks in advance!


Solution

  • Make sure that your code uses one SerialPort and not two. Then there is no problem reading or writing to this port.

    SerialPort serialPort;
    InputStream inStream;
    OutputStream outStream;
    
    serialPort = (SerialPort) portId.open(...);
    serialPort.setFlowControlMode(...);
    serialPort.setSerialPortParams(...);
    
    inStream = serialPort.getInputStream ();
    outStream = serialPort.getOutputStream ();
    

    PS: SMSLib is an excellent Java library (ported to .NET Framework as well) which allows you to send/receive SMS messages via a compatible GSM modem or GSM phone. SMSLib also supports some bulk SMS operators. It is free and very stable.