zoukankan      html  css  js  c++  java
  • JAVA 串口编程(二)

    三、实例

    (1)打开、关闭串口

    首先使用CommPortIdentifier中的方法,获取可用的端口,并且选择一个端口打开作为通信端口。

    A:枚举可用端口

    void listPortChoices() 
    {
         CommPortIdentifier portId;
         Enumeration en = CommPortIdentifier.getPortIdentifiers();
          while (en.hasMoreElements()) 
           {
             portId = (CommPortIdentifier) en.nextElement();
             if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) 
             System.out.println(portId.getName());
    10         }
    11         portChoice.select(parameters.getPortName());
    12   }
    13   
    14   

     

    B:打开指定的端口

    CommPortIdentifier portId;
    try 
    {
      //生成CommPortIdentifier类的实例
      portId = CommPortIdentifier.getPortIdentifier("COM4");
    } 
    catch (NoSuchPortException e)
    {
       e.printStackTrace();
    10  }
    11  try 
    12  {
    13      //打开串口COM4,设置超时时限为3000毫秒
    14      serialPort = (SerialPort) portId.open("testApp", 3000);
    15  } 
    16  catch (PortInUseException e)
    17  {
    18      e.printStackTrace();
    19  }
    20   
    21   
     

    C:关闭端口

    使用完的端口,必须记得将其关闭,否则其它的程序将无法使用该端口,CommPortIdentifier类只提供了开启端口的方法,而要关闭端口,则要调用CommPort类的close()方法。

    serialPort.close()

    (2)设置串口参数

    try {
        try {
            serialPort.setSerialPortParams(9600, //  波特率
                                           SerialPort.DATABITS_8,//  数据位数
                                           SerialPort.STOPBITS_1, //  停止位
                                           SerialPort.PARITY_NONE);//  奇偶位
        } 
        catch (UnsupportedCommOperationException e)
         {
    10          e.printStackTrace();
    11      }
    12   

    (3)串口的读、写

    A:向串口写数据

        OutputStream outputStream;
        try
        {
            outputStream = serialPort.getOutputStream();
        } 
        catch (IOException e)
         {
            e.printStackTrace();
        }
    10      bt = new byte[] { (byte) 0x55, (byte) 0xAA, (byte) 0xF1 };
    11      try
    12      {
    13          outputStream.write(bt);
    14          outputStream.flush();
    15          outputStream.close();
    16      }
    17      catch (IOException e)
    18       {
    19          e.printStackTrace();
    20      }
    21   

    B:读取串口中的数据

    读操作,需继承SerialPortEventListener接口。为SerialPort添加监听Listener。实现该接口的serialEvent (SerialPortEvent event)方法。

            public class SerialRead implements SerialPortEventListener{
           
                InputStream inputStream;
                byte[] rBuffer = new byte[38];
                 SerialRead(SerialPort serialPort)
                 {
                    try {
                        serialPort.addEventListener((SerialPortEventListener) this);
                        this.serialPort.notifyOnDataAvailable(true);
    10         
    11                  } catch (TooManyListenersException e) {
    12                  }
    13             
    14                  try {
    15                      if (serialPort != null)
    16                          inputStream = serialPort.getInputStream();
    17                  } catch (IOException e) {
    18                  }
    19                  }
    20               
    21              public void serialEvent(SerialPortEvent event) 
    22              {
    23                  if (event.getEventType() == SerialPortEvent.DATA_AVAILABLE)
    24                  { 
    25                      int count = 0;
    26                      try 
    27                      {
    28                          while ((ch = inputStream.read()) != -1) 
    29                          {       
    30                              bt[count++]=(byte)ch;
    31                          }
    32                      }
    33                      catch (IOException e) 
    34                      {
    35                          e.printStackTrace();
    36                      }
    37                  }   
    38              }
    39   
  • 相关阅读:
    Spring整合JMS-基于activeMQ实现(二)
    iOS 2D绘图详解(Quartz 2D)之概述
    iOS开发UI-利用Quartz2D 实现基本绘图(画三角形、矩形、圆、圆弧)
    Quart 2D 绘制图形简单总结
    IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
    用 Swift 制作一个漂亮的汉堡按钮过渡动画
    CAShapeLayer和CAGradientLayer
    Swift计算属性
    Swift常用语法示例代码(二)
    Swift 中的指针使用
  • 原文地址:https://www.cnblogs.com/dyufei/p/2573912.html
Copyright © 2011-2022 走看看