在路上

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

java的com口通讯

2016-12-20 13:16| 发布者: zhangjf| 查看: 609| 评论: 0

摘要: import gnu.io.CommPortIdentifier; import gnu.io.PortInUseException; import gnu.io.SerialPort; import gnu.io.SerialPortEvent; import gnu.io.SerialPortEventListener; import gnu.io.UnsupportedCommOp ...
  1. import gnu.io.CommPortIdentifier;
  2. import gnu.io.PortInUseException;
  3. import gnu.io.SerialPort;
  4. import gnu.io.SerialPortEvent;
  5. import gnu.io.SerialPortEventListener;
  6. import gnu.io.UnsupportedCommOperationException;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.util.Enumeration;
  10. import java.util.TooManyListenersException;
  11. public class SimpleRead implements Runnable, SerialPortEventListener {
  12. static CommPortIdentifier portId;
  13. static Enumeration portList;
  14. InputStream inputStream;
  15. SerialPort serialPort;
  16. Thread readThread;
  17. public static void main(String[] args) {
  18. String com="4";
  19. if(args!=null&&args.length>=1)
  20. com=args[0];
  21. portList = CommPortIdentifier.getPortIdentifiers();
  22. // 检索系统串口
  23. while (portList.hasMoreElements()) {
  24. portId = (CommPortIdentifier) portList.nextElement();
  25. /*如果端口类型是串口,则打印出其端口信息*/
  26. if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
  27. System.out.println("------------------------");
  28. System.out.println("系统可用串口: "+portId.getName());
  29. System.out.println("------------------------");
  30. // 指定COM口
  31. if (portId.getName().equals("COM"+com)) {
  32. System.out.println("找到COM"+com+"口,初始化...");
  33. SimpleRead reader = new SimpleRead();
  34. }else{
  35. System.out.println("无法找到COM"+com+"口,请重新指定...");
  36. }
  37. }
  38. }
  39. }
  40. public SimpleRead() {
  41. try {
  42. // 打开COM串口 2000 设置毫秒数 超时等待时间
  43. serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
  44. System.out.println("COM口打开成功!");
  45. } catch (PortInUseException e) {
  46. System.out.println("端口被占用");
  47. }
  48. try {
  49. inputStream = serialPort.getInputStream();
  50. System.out.println("获得输入流...");
  51. } catch (IOException e) {
  52. }
  53. //进行端口监听 , 当事件发生自动调用 serialEvent方法
  54. try {
  55. serialPort.addEventListener(this);
  56. } catch (TooManyListenersException e) {
  57. }
  58. serialPort.notifyOnDataAvailable(true);
  59. //设置通讯位
  60. try {
  61. System.out.println("设置通讯位...");
  62. serialPort.setSerialPortParams(115200,// 设置波特率
  63. SerialPort.DATABITS_8,// 数据位数
  64. SerialPort.STOPBITS_1,// 停止位
  65. SerialPort.PARITY_NONE);// 奇偶位
  66. } catch (UnsupportedCommOperationException e) {
  67. }
  68. // 启动线程,监听
  69. readThread = new Thread(this);//线程负责每接收一次数据休眠20秒钟
  70. readThread.start();
  71. }
  72. public void run() {
  73. try {
  74. System.out.println("监听...");
  75. Thread.sleep(20000);//休息20秒
  76. } catch (Exception e) {
  77. }
  78. }
  79. // 处理侦听到的串口事件
  80. public synchronized void serialEvent(SerialPortEvent event) {
  81. // System.out.println("接收数据...rn");
  82. switch (event.getEventType()) {
  83. case SerialPortEvent.BI://BI - 通讯中断.
  84. case SerialPortEvent.OE://OE - 溢位错误.
  85. case SerialPortEvent.FE://FE - 帧错误.
  86. case SerialPortEvent.PE://PE - 奇偶校验错.
  87. case SerialPortEvent.CD://CD - 载波检测.
  88. case SerialPortEvent.CTS://CTS - 清除发送.
  89. case SerialPortEvent.DSR://DSR - 数据设备准备好.
  90. case SerialPortEvent.RI://RI -  振铃指示.
  91. case SerialPortEvent.OUTPUT_BUFFER_EMPTY://OUTPUT_BUFFER_EMPTY - 输出缓冲区已清空
  92. break;
  93. case SerialPortEvent.DATA_AVAILABLE://DATA_AVAILABLE - 有数据到达
  94. byte[] readBuffer = new byte[10000];
  95. try {
  96. //读数据
  97. while (inputStream.available() > 0) {
  98. int numBytes = inputStream.read(readBuffer);
  99. }
  100. String str=new String(readBuffer);
  101. if(str.equals("exit")){
  102. inputStream.close();serialPort.close();
  103. }
  104. //输出内容
  105. System.out.println("<------开始------->");
  106. System.out.println(str+"==============");
  107. System.out.println("<------结束------->");
  108. System.out.println(" ");
  109. } catch (IOException e) {
  110. }
  111. break;
  112. }
  113. }
  114. }
复制代码

最新评论

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

;

GMT+8, 2025-8-23 04:26

Copyright 2015-2025 djqfx

返回顶部