最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java串口通信详细及简单实例介绍
时间:2017-01-11 编辑:简简单单 来源:一聚教程网
java 实现串口通信
最近做了一个与硬件相关的项目,刚开始听说用java和硬件打交道,着实下了一大跳。java也可以操作硬件?
后来接触到是用java通过串口通信控制硬件感觉使用起来还不错,也很方便。
特拿出来和大家一起分享一下。
准备工作:
首先到SUN官网下载一个zip包:javacomm20-win32.zip
其中重要的有这几个文件:
win32com.dll
comm.jar
javax.comm.properties
按照说明配置好环境,如下:
将win32com.dll复制到
到这一个可以java 串口开发环境就搭建完成了
确认本机可以使用的串口:
代码如下 | 复制代码 |
packagetest; importjava.util.Enumeration; importjava.util.HashMap; importjavax.comm.CommPortIdentifier; importjavax.comm.SerialPort; publicclassGetSerialPorts { publicvoidlistPortChoices() { CommPortIdentifier portId; Enumeration en = CommPortIdentifier.getPortIdentifiers(); // iterate through the ports. while(en.hasMoreElements()) { portId = (CommPortIdentifier) en.nextElement(); if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println(portId.getName()); } } } publicstaticvoidmain(String[] args) { GetSerialPorts GSP =newGetSerialPorts(); GSP.listPortChoices(); } } |
打开串口,关闭串口:
代码如下 | 复制代码 |
packagetest; importjava.io.IOException; importjava.io.InputStream; importjava.io.OutputStream; importjava.util.Enumeration; importjava.util.HashMap; importjavax.comm.CommPortIdentifier; importjavax.comm.PortInUseException; importjavax.comm.SerialPort; importjavax.comm.UnsupportedCommOperationException; publicclassGetSerialPorts { privateCommPortIdentifier portId; privateSerialPort testPort; privateCommPortIdentifier myPort; privateInputStream is; privateOutputStream os; publicvoidlistPortChoices() { Enumeration en = CommPortIdentifier.getPortIdentifiers(); // iterate through the ports. while(en.hasMoreElements()) { portId = (CommPortIdentifier) en.nextElement(); if(portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { System.out.println(portId.getName()); } myPort = portId;// 任意取一个串口,比如com1 } } publicbooleanopenPort() { try{ testPort = (SerialPort) myPort.open("COM1",500);// 注意这里必须换成一个真实的串口 try{ this.testPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN); }catch(UnsupportedCommOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ this.testPort.enableReceiveTimeout(30); }catch(UnsupportedCommOperationException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.testPort.setOutputBufferSize(1024); this.testPort.setInputBufferSize(1024);
try{ this.is =this.testPort.getInputStream(); }catch(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ this.os =this.testPort.getOutputStream(); }catch(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.testPort.notifyOnDataAvailable(true); this.testPort.notifyOnOutputEmpty(true); this.testPort.notifyOnBreakInterrupt(true); // this.printerPort.addEventListener(new PrintPortListener(is)); System.out.println("打开com1机串口成功"); returntrue; }catch(PortInUseException e) { // TODO Auto-generated catch block e.printStackTrace(); returnfalse; } } /** * TODO 关闭端口 * * @param * @return Map * @throws */ publicbooleanclosePort() { // TODO Auto-generated method stub try{ if(null!=this.testPort) { is.close(); os.close(); this.testPort.close(); } System.out.println("关闭COM1串口成功"); returntrue; }catch(Exception e) { // TODO Auto-generated catch block // e.printStackTrace(); System.out.println("关闭COM1串口失败"); returnfalse; } } publicstaticvoidmain(String[] args) { GetSerialPorts GSP =newGetSerialPorts(); GSP.listPortChoices(); GSP.openPort(); } } |
读数据:
代码如下 | 复制代码 |
/** * TODO 接收端口???/div> * * @param InputStream * @return String * @throws */ publicString readData(InputStream is) { // 读取缓冲区域 byte[] readBuffer =newbyte[4096]; intreadDataLength =0; try{ readDataLength = is.read(readBuffer); // for (byte b : readBuffer) { // System.out.print(b); // } // System.out.println(); }catch(IOException e) { // TODO Auto-generated catch block e.printStackTrace(); returnnull; } // 将真实数据保存到零时数组中 byte[] readTemp =newbyte[readDataLength]; for(inti =0; i < readDataLength; i++) { readTemp[i] = readBuffer[i]; } // 将byte数组转换为16进制字符串 String stringTemp = FeelTheBase.bytesToHexString(readTemp); // System.out.println("指令返回值" + stringTemp); returnstringTemp; } |
相关文章
- 鼠标经过出现气泡框的简单实例 07-04
- Spring Annotaion Support详细介绍及简单实例 06-22
- Java clone方法详解及简单实例 06-19
- vue实现ToDoList简单实例 03-16
- python 编程之twisted详解及简单实例 03-08
- java BASE64Encoder详细介绍及简单实例 02-28