Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 14
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2010
    Posts
    72

    Java AT command writing errors.


    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.j ava:66)
    at javax.comm.CommPortIdentifier.open(CommPortIdentif ier.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());
        }
    }

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    Well, the USB device will not be on /dev/ttyS0 - that is a standard serial port. You need to use /dev/ttyUSB0 instead. What do you mean when you say that /dev/ttyUSB0 is "not part of the port lists"?
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jul 2010
    Posts
    72
    Dear Rubberman,
    What I meant is that when I run this statement "while(ports.hasMoreElements())
    {
    pID = (CommPortIdentifier)ports.nextElement();
    System.out.println("Port " + pID.getName());
    " it is not part of it. So what must I change for it to appear as part of the list? Thank you.

  4. #4
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    I'm going to guess that you should be using the API call that returns the list of USB devices instead of serial port devices

  5. #5
    Just Joined!
    Join Date
    Jul 2010
    Posts
    72
    Dear All,
    Ok I have managed to get the ttyUSB0 by modifying the javax.comm.properties and add the ttyUSB0 into it. So below is my problem. It works the first time but not message it sent out. Then the following times it gives me this error "PortInUseException : java.lang.RuntimeException:
    Error opening "/dev/ttyUSB0"
    lockf(): Resource temporarily unavailable". I am suspecting the way I sent the first time the AT command locked the device or the port is it? So what is the wrong in the manner I am sending the data. What I am suppose to send it first AT+CMGS="+phoneNo" then the message and lastly ctrl+Z.
    Below is my coding
    Code:
    import javax.comm.*;
    import java.util.*;
    
    public class PortWriter
    {
        static Enumeration ports;
        static CommPortIdentifier pID;
        static OutputStream outStream;
        SerialPort serPort;
       
        
        
        public PortWriter() throws Exception{
            try
            {
            
            serPort = (SerialPort)pID.open("/dev/ttyUSB0",2000);
            System.out.println();
            outStream = serPort.getOutputStream();
            System.out.println();
            serPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
            SerialPort.STOPBITS_1,
            SerialPort.PARITY_NONE);
            }
            catch (PortInUseException e)
            {
            System.out.println("PortInUseException : "+e);
            }
            catch (Exception e)
            {
            System.out.println("PortInUseException : "+e);
            }
    
        }    
        
        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/ttyUSB0"))
                    {
                        PortWriter pWriter = new PortWriter();
                        //System.out.println("COM1 found");
                    }
                }
            }
            String phoneNumber = "+60162210456";
            String atToSend = "AT+CMGS=\""+phoneNumber+"\"";
            outStream.write(atToSend.getBytes());
            Thread.sleep(5000);
            String messageToSend = "hi test one n see\0x1A";
            outStream.write(messageToSend.getBytes());
        }
    }
    }

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    The port configuration (speed, parity, etc) are probably incorrect. You need to read the manufacturer's documentation for how to configure the device for speed, parity, etc. In most cases with USB devices, you won't need to do anything other than open it. It should auto-detect the configuration I think.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    Jul 2010
    Posts
    72
    Dear Rubberman,
    Ok I have actually used the minicom tool. In that I have set the bps=9600, par=5 and bits =1 and from there I can send the sms succesffuly. So what changes must I do to reflect this in my codes I am not too sure. Can you help me on this?

  8. #8
    Just Joined!
    Join Date
    Jul 2010
    Posts
    72
    Dear Rubberman,
    In the minicom is states as Bps/Par/Bits and the value is 9600 5N1. I dont know whether I made any mistake in the interpretation. Thank you.

  9. #9
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,397
    Hmmm. 9600baud seems awfully slow for a network modem. Mine runs at much higher speed, and can get over 1mbps network thruput with Sprint. I don't remember off-hand what the line setting was - I'll have to look, though I dropped the account a couple of months ago once I got my Android phone to tether with 3g.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  10. #10
    Just Joined!
    Join Date
    Jul 2010
    Posts
    72
    Dear Rubberman,
    I wrote this code as below and the results is stated beside it

    System.out.println("\ngetDataBits"+serPort.getData Bits());==8
    System.out.println("\ngetStopBits"+serPort.getStop Bits());==1
    System.out.println("\ngetParity"+serPort.getParity ());==0
    System.out.println("\ngetBaudRate"+serPort.getBaud Rate());==9600

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •