java读取串口设备信息

一个最简单的从串口设备读取信息实例:

关于读取串口信息的环境配置过程网上很多,大致就是:

下载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)