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操作串口是通过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通过串口读取数据例子
- 通过JAVA读取串口信息
- Java串口编程:串口数据的发送与监听读取
- 通过java读取HDFS的数据 (转)
- java通过URL读取json数据
- java通过URL读取json数据
- 通过java访问232串口数据并将数据存入数据库
- java读取串口数据
- java代码--通过某路径读取数据
- java读取硬件串口——数据断行问题
- java监听、读取串口数据
- 【java基础:JDBC】通过读取配置文件进行数据库连接并读取数据的Demostration
- java接口之通过FileSystem API 读取数据。
- Java读取usb串口号数据
- Qt 通过串口读取地磅数据
- java通过url读取远程数据并保持到本地的实例代码
- app连接wifi模块,通过wifi模块串口读取数据失败
- java通过相对路径读取properties数据
- Java串口编程:串口数据的发送与监听读取
- 通过纯Java代码从excle中读取数据(为.xlsx文件)