java comm 编程式例

这是我第一次在csdn上发表文章,只是做个测试而已,没什么好看的.

java 的comm.jar通信编程包可用来直接写串口通信程序,下面是我在测试Siemens Tc35i时写的一些代码.

/*
 * Created on 2005-6-13
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.mno5.comm.sms;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.TooManyListenersException;

import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

/**
 * @author wuth
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SerialOperate implements SerialPortEventListener{
 
 private CommPortIdentifier portId;
 private Enumeration portList;
 private SerialPort serialPort;
 private InputStream inputStream;
  
 /**
  * 查找串口 COM1
  *
  * @param args
  */
 public  boolean searchPort(String port)
 {
  portList = CommPortIdentifier.getPortIdentifiers();

  while (portList.hasMoreElements()) {
   portId = (CommPortIdentifier) portList.nextElement();
   if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
    if (portId.getName().equals(port)) {
     System.out.println(portId.getName()+" founded!!!");
     return true;
    }
   }
  }
  
  return false;
 }
 
 /**
  * 打开串口 注册监听器
  * @throws IOException
  * @author wuth
  *
  * TODO To change the template for this generated type comment go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
 
 public  void openPort() throws PortInUseException, TooManyListenersException, UnsupportedCommOperationException, IOException
 {
  serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
  inputStream = serialPort.getInputStream();
  serialPort.addEventListener(this);
  serialPort.notifyOnDataAvailable(true);
  serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8,
     SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
  
 }
 
 /**
  * 向串口写数据
  * @param command
  * @throws IOException
  * @throws InterruptedException
  */
 
 public void writePort(String command) throws IOException, InterruptedException
 {
  OutputStream outputStream = serialPort.getOutputStream();
  outputStream.write(command.getBytes());
        outputStream.flush();
        outputStream.close();
        Thread.sleep(500);
 }
 
 public void closePort()
 {
  serialPort.close();
 }
 
 
 
 
 
 public void serialEvent(SerialPortEvent event) {
   
   switch (event.getEventType()) {
   case SerialPortEvent.BI:
   case SerialPortEvent.OE:
   case SerialPortEvent.FE:
   case SerialPortEvent.PE:
   case SerialPortEvent.CD:
   case SerialPortEvent.CTS:
   case SerialPortEvent.DSR:
   case SerialPortEvent.RI:
   case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
    break;
   case SerialPortEvent.DATA_AVAILABLE:
    //  byte[] readBuffer = new byte[20];
    String str = "";
    try {
     
     while (inputStream.available() > 0) {
      int numBytes = inputStream.read();
      //   System.out.print((char)numBytes);
      str += (char) numBytes;
     }

    } catch (IOException e) {
    }
    System.out.print(str);
  
    break;
   }
  }
 
 
 
 /**
  * @return Returns the serialPort.
  */
 public SerialPort getSerialPort() {
  return serialPort;
 }
 /**
  * @param serialPort The serialPort to set.
  */
 public void setSerialPort(SerialPort serialPort) {
  this.serialPort = serialPort;
 }
}

测试类

/*
 * Created on 2005-6-13
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.mno5.comm.sms;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TooManyListenersException;

import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;

import junit.framework.TestCase;

/**
 * @author wuth
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SerialOperateTest extends TestCase {
 
 public void testSearchPort() {
  SerialOperate serialOperate = new SerialOperate();
  System.out.print(serialOperate.searchPort("COM1"));
  SerialPort port = serialOperate.getSerialPort();
  try {
   serialOperate.openPort();
   String command;
   while(true)
   {
    
    BufferedReader reder = new BufferedReader(new InputStreamReader(System.in));
    command = reder.readLine();
    if(command.equals("OK"))
    { 
     System.err.println("/nBye");
     System.exit(1);
    }
    serialOperate.writePort(command+"/r");
    
   }
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  
  serialOperate.closePort();
 }


}

运行测试类,在命令直接输入AT指令既可

你可能感兴趣的:(java comm 编程式例)