java读取串口设备信息
2015-05-08 11:39
323 查看
一个最简单的从串口设备读取信息实例:
关于读取串口信息的环境配置过程网上很多,大致就是:
下载javacomm20-win32.zip,解压缩。
截取网上一段配置过程:
1.网上下载javacomm20-win32.zip后,解压之。此时有用的文件有3个:comm.jar、javax.comm.properties和win32com.dll
2.Eclipse下创建JavaSerial工程,将comm.jar放在/lib/目录下,并添加至Build Path;将win32com.dll放在/目录下
comm.jar放在/lib/目录下为习惯用法;如果环境变量CLASSPATH中包含了当前目录.,那么可以讲win32com.dll放在Eclipse工程的根目录,否则需要将win32com.dll放在C:/windows/system32/目录
3.将javax.comm.properties放在jre的lib目录下,譬如C:\Program Files\Java\jre7\lib\
完整安装java之后会有jdk和jre,一般Eclipse运行Java程序的时候都会使用jre,所以需要将.properties文件放在jre的lib目录下。如果没有效果,可以尝试将.properties放到jdk的lib目录下。
但是我在配置完这些环境后,进行测试,并不能读出串口,另外,将comm.jar的路径配置到环境变量path中,将win32com.dll放在src目录下,并且在程序前面用代码手动加载配置文件:
public class comTest implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
public static String str;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
static BigWindowServiceImpl bws;
public static void main(String[] args) {
CommDriver driver = null;
bws=new BigWindowServiceImpl();
String driverName = "com.sun.comm.Win32Driver";
try {
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM4")) {//要读取的串口
comTest reader = new comTest();
}
}
}
}
//构造函数
public comTest() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 9600);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
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[15];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
str=new String(readBuffer);
System.out.print(str);
if(str!=null){
//str是从串口读取出来的数据,这里转换成了字符串,如果不为空进行一些处理
}
}
catch (IOException e) {}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}我用的是手持条码扫描机,MD62XX系列。可以读取一维码,二维码信息。当然,如果读取的信息有中文,需要进行一些处理
关于读取串口信息的环境配置过程网上很多,大致就是:
下载javacomm20-win32.zip,解压缩。
截取网上一段配置过程:
1.网上下载javacomm20-win32.zip后,解压之。此时有用的文件有3个:comm.jar、javax.comm.properties和win32com.dll
2.Eclipse下创建JavaSerial工程,将comm.jar放在/lib/目录下,并添加至Build Path;将win32com.dll放在/目录下
comm.jar放在/lib/目录下为习惯用法;如果环境变量CLASSPATH中包含了当前目录.,那么可以讲win32com.dll放在Eclipse工程的根目录,否则需要将win32com.dll放在C:/windows/system32/目录
3.将javax.comm.properties放在jre的lib目录下,譬如C:\Program Files\Java\jre7\lib\
完整安装java之后会有jdk和jre,一般Eclipse运行Java程序的时候都会使用jre,所以需要将.properties文件放在jre的lib目录下。如果没有效果,可以尝试将.properties放到jdk的lib目录下。
但是我在配置完这些环境后,进行测试,并不能读出串口,另外,将comm.jar的路径配置到环境变量path中,将win32com.dll放在src目录下,并且在程序前面用代码手动加载配置文件:
CommDriver driver = null; String driverName = "com.sun.comm.Win32Driver"; try { driver = (CommDriver) Class.forName(driverName).newInstance(); driver.initialize(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } portList = CommPortIdentifier.getPortIdentifiers(); while (portList.hasMoreElements()) { portId = (CommPortIdentifier) portList.nextElement(); if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { if (portId.getName().equals("COM4")) { comTest reader = new comTest(); } } }也可以用上面程序测试,如果文件加载正确,则可以读取出电脑上所有可用的串口
public class comTest implements Runnable, SerialPortEventListener {
static CommPortIdentifier portId;
static Enumeration portList;
public static String str;
InputStream inputStream;
SerialPort serialPort;
Thread readThread;
static BigWindowServiceImpl bws;
public static void main(String[] args) {
CommDriver driver = null;
bws=new BigWindowServiceImpl();
String driverName = "com.sun.comm.Win32Driver";
try {
driver = (CommDriver) Class.forName(driverName).newInstance();
driver.initialize();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
portList = CommPortIdentifier.getPortIdentifiers();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM4")) {//要读取的串口
comTest reader = new comTest();
}
}
}
}
//构造函数
public comTest() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 9600);
} catch (PortInUseException e) {}
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (UnsupportedCommOperationException e) {}
readThread = new Thread(this);
readThread.start();
}
public void run() {
try {
Thread.sleep(20000);
} catch (InterruptedException e) {}
}
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[15];
try {
while (inputStream.available() > 0) {
int numBytes = inputStream.read(readBuffer);
}
str=new String(readBuffer);
System.out.print(str);
if(str!=null){
//str是从串口读取出来的数据,这里转换成了字符串,如果不为空进行一些处理
}
}
catch (IOException e) {}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
}
}我用的是手持条码扫描机,MD62XX系列。可以读取一维码,二维码信息。当然,如果读取的信息有中文,需要进行一些处理
相关文章推荐
- java--IO流-LineNumberReader,读取键盘录入,字符字节流转换,改变标准输入输出设备,异常的日志信息,系统信息
- java 读取串口 com 外部设备
- 通过JAVA读取串口信息
- Java Properties 类读取配置文件信息
- Java读取ipa中info.plist获取版本信息
- c#使用WPD读取便携式设备信息一(枚举设备、连接设备及读取设备信息)
- Java读取注册表信息到内存中
- 读取Android手机设备相关信息
- uidevice extension工程解读(读取设备信息)
- java读取tiff格式图片信息的方式。
- C#读取设备信息(源代码下载)
- Java Properties 类读取配置文件信息
- java用户metadata-extractor读取照片信息错误Exception in thread “main” java.lang.NoClassDefFoundError: com/adobe
- Java读取股票信息
- java读取xml文件的节点信息
- Java--串口之间的通信及扫描枪的读取
- Java 安卓获得获得手机设备信息
- 解决linux下java读取串口之权限问题 No permission to create lock file. 分类: LINUX 2010-07-13 20:51 2006人阅读 评论(0) 收藏
- Java读取图片和EXIF信息
- C#读取设备信息(源码)