您的位置:首页 > 编程语言 > Java开发

java通过串口读取数据

2015-04-14 18:11 1511 查看
最近要做一个项目,接口数据可能通过RS232串口给我们传过来,所以就事先学了一下java如何通过串口接收数据,怕自己忘了,所以记录下来。

java操作串口是通过RXTX实现的,可以从http://fizzed.com/oss/rxtx-for-java下载。

安装说明:

Windows

RXTXcomm.jar goes in \jre\lib\ext (under java)

rxtxSerial.dll goes in \jre\bin

Mac OS X (x86 and ppc) (there is an Installer with the source)

RXTXcomm.jar goes in  /Library/Java/Extensions

librxtxSerial.jnilib goes in /Library/Java/Extensions

Run fixperm.sh thats in the directory.  Fix perms is in the Mac_OS_X

subdirectory.

Linux (only x86, x86_64, ia64 here but more in the ToyBox)

RXTXcomm.jar goes in /jre/lib/ext (under java)

librxtxSerial.so goes in /jre/lib/[machine type] (i386 for instance)

Make sure the user is in group lock or uucp so lockfiles work.

Solaris (sparc only so far)

RXTXcomm.jar goes in /jre/lib/ext (under java)

librxtxSerial.so goes in /jre/lib/[machine type]

Make sure the user is in group uucp so lockfiles work.

代码如下

package comm;

import gnu.io.CommPort;

import gnu.io.CommPortIdentifier;

import gnu.io.SerialPort;

import gnu.io.SerialPortEvent;

import gnu.io.SerialPortEventListener;

import java.io.IOException;

import java.io.InputStream;

/**

 * Java 操作串口读取数据

 * @author suyunlong

 *

 */

public class SerialComm

{

 /**

  * 打开串口

  * @param portName 串口名

  * @param waverate 波特率

  */

 public void connect(String portName,int waverate)

 {

  CommPortIdentifier portIdentifier=null;

  try

  {

   portIdentifier=CommPortIdentifier.getPortIdentifier(portName);

   if(portIdentifier.isCurrentlyOwned())

   {

    System.out.println("Error: Port is currently in use");

   }

   else

   {

    CommPort commPort=null;

    commPort=portIdentifier.open(this.getClass().getName(), 2000);

    if(commPort instanceof SerialPort)

    {

     SerialPort serialPort=(SerialPort)commPort;

     serialPort.setSerialPortParams(waverate,SerialPort.DATABITS_8, SerialPort.STOPBITS_1,SerialPort.PARITY_NONE);

     InputStream in=serialPort.getInputStream();

     serialPort.addEventListener(new SerialReader(in));

     serialPort.notifyOnDataAvailable(true);

    }

   }

  }

  catch(Exception e)

  {

   System.out.println(e.getMessage());

  }

 }

 /**

  * 处理输入来自串行端口,启动一个监听器

  * @author suyunlong

  *

  */

 public static class SerialReader implements SerialPortEventListener

 {

  private InputStream in;

  private byte[] buffer=new byte[1024];

  public SerialReader(InputStream in)

  {

   this.in=in;

  }

  public void serialEvent(SerialPortEvent arg0)

  {

   int data;

   try

   {

    int len=0;

    while((data=in.read()) > -1)

    {

     if(data=='\n')

     {

      break;

     }

     buffer[len++]=(byte)data;

    }

    System.out.println(new String(buffer, 0, len));

   }

   catch(IOException e)

   {

    System.out.println(e.getMessage());

    System.exit(-1);

   }

  }

 }

 public static void main(String[] args)

 {

  (new SerialComm()).connect("COM1",57600);

 }

}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java