将java中的AT命令发送到电信设计调制解调器

时间:2015-02-18 11:18:14

标签: java at-command carriage-return serial-communication

我正在尝试在java中执行at命令,我已经在matlab中完成了它,但我发现它在java中有点困难。 java的串行通信或命令是否有api? 我需要一个帮助来声明串口,然后发送命令给它。 我发现这个java代码打开串口(com12),但它没有打开串口。

 static Enumeration portList;
 static CommPortIdentifier portId;
static String messageString = "at \n";
static SerialPort serialPort;
static OutputStream outputStream;
public static void main(String[] args) throws IOException {
    // TODO code application logic here
portList = CommPortIdentifier.getPortIdentifiers();
System.out.println("trying");

while (portList.hasMoreElements()) {
System.out.println("trying");
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
System.out.println("trying");

         if (portId.getName().equals("COM12")) {
System.out.println("found");
            try {
                serialPort = (SerialPort)
                    portId.open("SimpleWriteApp", 2000);
            } catch (PortInUseException e) {System.out.println("err");}
            try {
                outputStream = serialPort.getOutputStream();
            } catch (IOException e) {System.out.println("err1");}
            try {
                serialPort.setSerialPortParams(9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            } catch (UnsupportedCommOperationException e)                      
            {        
            System.out.println("err2");}
            outputStream.write(messageString.getBytes());
            System.out.println(messageString);
            outputStream.close();
            serialPort.close();
            }
            }
           }
              }

我正在使用电信设计调制解调器,我已经在tera term终端中执行了命令,因此我确信调制解调器或我发送命令的方式没有任何问题。我想我正在努力打开串口,并发送托架字符。

提前致谢

1 个答案:

答案 0 :(得分:0)

以下代码可以帮助您

    import java.sql.*;
import java.io.*;
import java.util.*;
import java.text.*;
import gnu.io.*;


public class SerialComm implements SerialPortEventListener 

{
 int result=0;
  static CommPortIdentifier portId=null;
  Enumeration portList=null;
  static InputStream inputStream=null;
  static OutputStream out=null;
  static SerialPort serialPort=null;
  static int srate=9600;//B-Rate
  String data=null;
  int i=0;
  String number="";
  int status=0;
  String f_id="";
  String dateNow="";
  String dateNow1="";
public  String recvdData="aa";  


 public static void main(String args[]) 
 { 
   SerialComm obj=new SerialComm();

 }   


 public SerialComm() 
 {
    try
    {
        if(this.detectPort())
        {
            if(this.initPort()) 
            {

// //               Thread.sleep(2000);
                //  sendMsg();
                //readSMS(1);
                //getRFID();
                }    
            }
    }
   catch(Exception e)
    {
        System.out.println("Error in SerialComm()-->"+e);
    }
 }



 public boolean detectPort() 
 {

    boolean portFound = false;
    //String defaultPort = "/dev/ttyUSB0";
    String defaultPort = "COM1";
    try 
    {
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements())
         {

            CommPortIdentifier portID = (CommPortIdentifier) portList.nextElement();

            if (portID.getPortType() == CommPortIdentifier.PORT_SERIAL) 
            {

                if (portID.getName().equals(defaultPort))
                {
                this.portId=portID;
                System.out.println("Found port: "+portId.getName());
                portFound = true;
                break;
                } 
            } 

         } 
            if (!portFound) 
            {
            System.out.println("port " + defaultPort + " not found.");
            }
    }
    catch(Exception e)
    {
            portFound = false;
    }   

    return portFound;   

  }
  public boolean initPort() 
  {

    try 
    { 
            serialPort = (SerialPort) portId.open("SerialCommApp", 2000);     
    }
    catch (PortInUseException e) 
    {
        System.out.println("Port in use-->"+e);
    }

    try 
    {
        inputStream = serialPort.getInputStream();
        out=serialPort.getOutputStream();
    } 
    catch (IOException e) 
    {
        System.out.println("IO Error-->"+e);
    }

    try 
    { 
            serialPort.addEventListener(this);
    } 
    catch (TooManyListenersException e) 
    {
        System.out.println("Too many LIstener-->"+e);
    }

    serialPort.notifyOnDataAvailable(true);

    try 
    {
       serialPort.setSerialPortParams(srate, SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);
       serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
    }
    catch (UnsupportedCommOperationException e) 
    {
        System.out.println("Error while setting parameters-->"+e);
    }

      System.out.println("Port Initialized....");
      return true;

  }
public synchronized void serialEvent(SerialPortEvent event) 
  {

    switch (event.getEventType())   
    {
       case SerialPortEvent.DATA_AVAILABLE:
        System.out.println("DATA_AVAILABLE");

        byte[] readBuffer = new byte[1024];
        int numBytes=1024;
        data="";

        try 
        {
            Thread.sleep(100);          
            while (inputStream.available() > 0)
            {
                numBytes = inputStream.read(readBuffer);//count of reading data
                data=data+new String(readBuffer,0,numBytes);
                data=data.trim();
                //this.recvdData+=data;
                this.recvdData=data;
            }
            System.out.println("data=========="+this.recvdData);
            getRFID(this.recvdData);
        } 
        catch (Exception e) 
        {
            System.out.println("Exception in serial event-->"+e);
        }

        break;//break from switch case 1:
        }//end of switch 
  }
 public void sendMsg() 
    {


        try
        {

            System.out.println("Sending");
            sendAT();
            System.out.println("AT OK");
            sendAT_CMGF();
            System.out.println("Enabled text mode");
            sendMessage("9xxxxxxxx","testing");
            System.out.println("Message Sent");
            //readSMS(1);


            System.exit(0);
        }
        catch(Exception e)
        {
        System.out.println(e);
        }

    }
    public void sendAT() {
        try{
            System.out.println("Sending AT");
            this.recvdData="";
            String mysms="AT";
            out.write(mysms.getBytes());
            out.write(13);  
            Thread.sleep(500);
            if(this.recvdData.contains("OK"))
            {
                return;
            }else{
                sendAT();
            }
            return;
        }catch(Exception e){
            System.out.println(e);
        }

    }
    }

可能不是一个好的答案,只是参考AT命令的使用方式