java通过串口读取数据
来源:互联网 发布:手机pdf免费编辑软件 编辑:程序博客网 时间:2024/05/17 05:00
最近要做一个项目,接口数据可能通过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通过串口读取数据
- Java通过串口读取数据例子
- 通过串口读取mavlink数据
- java读取串口数据
- 通过JAVA读取串口值
- 通过JAVA读取串口值
- 通过JAVA读取串口信息
- java监听、读取串口数据
- 通过JAVA读取串口值(原创)
- Python3通过数据缓存区读取串口数据
- 通过读取串口数据,把数据写入MySQL
- 通过Java读取HDFS数据
- java串口读取数据(转载)
- Java+jSSC 串口从读取数据
- java 串口编程----设置缓冲屈来读取串口数据
- Java串口编程:串口数据的发送与监听读取
- Java串口编程:串口数据的发送与监听读取
- MFC读取串口数据
- LINUX任意精度计算器BC用法
- 36辆车,6条跑道,无计时器,最少几次比赛可以选出前三
- 各种格式的时间日期正则表达式
- 基于DSS的先侦听后推送式流媒体转发
- Codeforces round #298
- java通过串口读取数据
- linux下的ssh配置
- Go常用包系列---strings包
- shell 简单oo编程
- Netty系列之Netty线程模型
- 开源性能测试工具Jmeter
- Xamarin.Android Adapter初探
- 神器 Brython —— 将 python 当做客户端脚本使用
- bss,data,text,rodata,堆,栈,常量段