在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

java 串口通信详细及简单实例

2017-3-7 12:52| 发布者: zhangjf| 查看: 1055| 评论: 0

摘要: java 实现串口通信 最近做了一个与硬件相关的项目,刚开始听说用java和硬件打交道,着实下了一大跳。java也可以操作硬件? 后来接触到是用java通过串口通信控制硬件感觉使用起来还不错,也很方便。 特拿出来和大家一 ...

java 实现串口通信

最近做了一个与硬件相关的项目,刚开始听说用java和硬件打交道,着实下了一大跳。java也可以操作硬件?

后来接触到是用java通过串口通信控制硬件感觉使用起来还不错,也很方便。

特拿出来和大家一起分享一下。

准备工作:

首先到SUN官网下载一个zip包:javacomm20-win32.zip

其中重要的有这几个文件:

win32com.dll

comm.jar

javax.comm.properties

按照说明配置好环境,如下:

将win32com.dll复制到bin目录下;将comm.jar复制到lib;把 javax.comm.properties也同样拷贝到lib目录下。然而在真正运行使用串口包的时候,仅作这些是不够的。因 为通常当运行“java MyApp”的时候,是由JRE下的虚拟机启动MyApp的。而我们只复制上述文件到JDK相应目录下,所以应用程序将会提示找不到串口。解决这个问题的 方法很简单,我们只须将上面提到的文件放到JRE相应的目录下就可以了

到这一个可以java 串口开发环境就搭建完成了

确认本机可以使用的串口:

  1. package test;
  2. import java.util.Enumeration;
  3. import java.util.HashMap;
  4. import javax.comm.CommPortIdentifier;
  5. import javax.comm.SerialPort;
  6. public class GetSerialPorts {
  7. public void listPortChoices() {
  8. CommPortIdentifier portId;
  9. Enumeration en = CommPortIdentifier.getPortIdentifiers();
  10. // iterate through the ports.
  11. while (en.hasMoreElements()) {
  12. portId = (CommPortIdentifier) en.nextElement();
  13. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  14. System.out.println(portId.getName());
  15. }
  16. }
  17. }
  18. public static void main(String[] args) {
  19. GetSerialPorts GSP = new GetSerialPorts();
  20. GSP.listPortChoices();
  21. }
  22. }
复制代码

打开串口,关闭串口:

  1. package test;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.util.Enumeration;
  6. import java.util.HashMap;
  7. import javax.comm.CommPortIdentifier;
  8. import javax.comm.PortInUseException;
  9. import javax.comm.SerialPort;
  10. import javax.comm.UnsupportedCommOperationException;
  11. public class GetSerialPorts {
  12. private CommPortIdentifier portId;
  13. private SerialPort testPort;
  14. private CommPortIdentifier myPort;
  15. private InputStream is;
  16. private OutputStream os;
  17. public void listPortChoices() {
  18. Enumeration en = CommPortIdentifier.getPortIdentifiers();
  19. // iterate through the ports.
  20. while (en.hasMoreElements()) {
  21. portId = (CommPortIdentifier) en.nextElement();
  22. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  23. System.out.println(portId.getName());
  24. }
  25. myPort = portId;// 任意取一个串口,比如com1
  26. }
  27. }
  28. public boolean openPort() {
  29. try {
  30. testPort = (SerialPort) myPort.open("COM1", 500);// 注意这里必须换成一个真实的串口
  31. try {
  32. this.testPort.setSerialPortParams(38400, SerialPort.DATABITS_8,
  33. SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);
  34. } catch (UnsupportedCommOperationException e) {
  35. // TODO Auto-generated catch block
  36. e.printStackTrace();
  37. }
  38. try {
  39. this.testPort.enableReceiveTimeout(30);
  40. } catch (UnsupportedCommOperationException e) {
  41. // TODO Auto-generated catch block
  42. e.printStackTrace();
  43. }
  44. this.testPort.setOutputBufferSize(1024);
  45. this.testPort.setInputBufferSize(1024);
  46. try {
  47. this.is = this.testPort.getInputStream();
  48. } catch (IOException e) {
  49. // TODO Auto-generated catch block
  50. e.printStackTrace();
  51. }
  52. try {
  53. this.os = this.testPort.getOutputStream();
  54. } catch (IOException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. this.testPort.notifyOnDataAvailable(true);
  59. this.testPort.notifyOnOutputEmpty(true);
  60. this.testPort.notifyOnBreakInterrupt(true);
  61. // this.printerPort.addEventListener(new PrintPortListener(is));
  62. System.out.println("打开com1机串口成功");
  63. return true;
  64. } catch (PortInUseException e) {
  65. // TODO Auto-generated catch block
  66. e.printStackTrace();
  67. return false;
  68. }
  69. }
  70. /**
  71. * TODO 关闭端口
  72. *
  73. * @param
  74. * @return Map
  75. * @throws
  76. */
  77. public boolean closePort() {
  78. // TODO Auto-generated method stub
  79. try {
  80. if (null != this.testPort) {
  81. is.close();
  82. os.close();
  83. this.testPort.close();
  84. }
  85. System.out.println("关闭COM1串口成功");
  86. return true;
  87. } catch (Exception e) {
  88. // TODO Auto-generated catch block
  89. // e.printStackTrace();
  90. System.out.println("关闭COM1串口失败");
  91. return false;
  92. }
  93. }
  94. public static void main(String[] args) {
  95. GetSerialPorts GSP = new GetSerialPorts();
  96. GSP.listPortChoices();
  97. GSP.openPort();
  98. }
  99. }
复制代码

读数据:

  1. /**
  2. * TODO 接收端口數據
  3. *
  4. * @param InputStream
  5. * @return String
  6. * @throws
  7. */
  8. public String readData(InputStream is) {
  9. // 读取缓冲区域
  10. byte[] readBuffer = new byte[4096];
  11. int readDataLength = 0;
  12. try {
  13. readDataLength = is.read(readBuffer);
  14. // for (byte b : readBuffer) {
  15. // System.out.print(b);
  16. // }
  17. // System.out.println();
  18. } catch (IOException e) {
  19. // TODO Auto-generated catch block
  20. e.printStackTrace();
  21. return null;
  22. }
  23. // 将真实数据保存到零时数组中
  24. byte[] readTemp = new byte[readDataLength];
  25. for (int i = 0; i < readDataLength; i++) {
  26. readTemp[i] = readBuffer[i];
  27. }
  28. // 将byte数组转换为16进制字符串
  29. String stringTemp = FeelTheBase.bytesToHexString(readTemp);
  30. // System.out.println("指令返回值" + stringTemp);
  31. return stringTemp;
  32. }
复制代码

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-3 13:40

Copyright 2015-2025 djqfx

返回顶部