Java AT command writing errors.

Discussion in 'Programming/Scripts' started by newbie14, Nov 11, 2010.

  1. newbie14

    newbie14 New Member

    Dear All,
    I am running a java application on centos. For now I have a gsm modem connected via the the usb cable. Below is the message I get when I type the command dmesg | grep tty

    serial8250: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
    00:0c: ttyS0 at I/O 0x3f8 (irq = 4) is a 16550A
    usb 2-2: pl2303 converter now attached to ttyUSB0
    pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
    usb 2-1: pl2303 converter now attached to ttyUSB0
    pl2303 ttyUSB0: pl2303 converter now disconnected from ttyUSB0
    usb 2-1: pl2303 converter now attached to ttyUSB0

    The based on that I am running a java application that trying to send sms via the gsm modem. Why I am using /dev/ttyS0 because /dev/ttyUSB0 is not part of the port lists. Below is my codes. When I run then I get an error as below. Can some one help me please? Thank you.

    Exception in thread "main" java.lang.RuntimeException:
    Error opening "/dev/ttyS0"
    lockf(): Resource temporarily unavailable
    at com.sun.comm.LinuxDriver.getCommPort(LinuxDriver.java:66)
    at javax.comm.CommPortIdentifier.open(CommPortIdentifier.java:369)
    at PortWriter.<init>(PortWriter.java:15)
    at PortWriter.main(PortWriter.java:34)

    Code:
    import java.io.*;
    import javax.comm.*;
    import java.util.*;
    
    public class PortWriter
    {
        static Enumeration ports;
        static CommPortIdentifier pID;
        static OutputStream outStream;
        SerialPort serPort;
        static String messageToSend = "AT+CMGS='+60162210456'testing\0x1A";
        
        public PortWriter() throws Exception{
            serPort = (SerialPort)pID.open("PortWriter",2000);
            outStream = serPort.getOutputStream();
            serPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);
        }    
        
        public static void main(String[] args) throws Exception{
            ports = CommPortIdentifier.getPortIdentifiers();
            
            while(ports.hasMoreElements())
            {
                pID = (CommPortIdentifier)ports.nextElement();
                System.out.println("Port " + pID.getName());
                
                if (pID.getPortType() == CommPortIdentifier.PORT_SERIAL)
                {
                    if (pID.getName().equals("/dev/ttyS0"))
                    {
                        PortWriter pWriter = new PortWriter();
                        System.out.println("COM1 found");
                    }
                }
            }
            outStream.write(messageToSend.getBytes());
        }
    }
     

Share This Page