java serial communication implementation process example

  • 2020-11-20 06:05:16
  • OfStack

1, download 64 rxtx for java links: http: / / fizzed com oss/rxtx for -- java

2. Unzip the downloaded package and follow the instructions to put it under the installation path of JAVA_HOME or JAVA

3. Add under ES19en. xml of maven


  <dependency>
    <groupId>org.rxtx</groupId>
    <artifactId>rxtx</artifactId>
    <version>2.1.7</version>
  </dependency>

4. Serial port API

CommPort: Abstract class for ports
CommPortIdentifier: Core class for serial port access and control
SerialPort: It can read, write and set up the serial port directly

5. List the ports available on the machine


Enumeration<CommPortIdentifier> em = CommPortIdentifier.getPortIdentifiers();
  while (em.hasMoreElements()) {
    String name = em.nextElement().getName();
    System.out.println(name);
  }

6. General steps: Open the serial port to get the serial port object == "Setting parameters ==" To read and write to the serial port == "Closing the serial port, among which reading to the serial port is more commonly used


// Open the serial port 
  CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("COM4");//COM4 Serial port name 
  CommPort commPort = portIdentifier.open("COM4", 2000);  //2000 Is the open timeout 
  serialPort = (SerialPort) commPort;
  // Set parameters (including baud rate, input / Output flow control, data bits, stop bits and parity) 
  serialPort.setSerialPortParams(9600,
  SerialPort.DATABITS_8, SerialPort.STOPBITS_1,
  SerialPort.PARITY_NONE);
  // Listen for serial port events 
  serialPort.addEventListener(new Abc()); //Abc Is to realize the SerialPortEventListener Interface class in which the specific read operation is carried out 
  //  Sets to wake up the listener receiving thread when data arrives 
  serialPort.notifyOnDataAvailable(true);
  //  Sets to wake up the interrupt thread when communication breaks 
  serialPort.notifyOnBreakInterrupt(true);
  //  in.close(); // Close a serial port 

Abc class content, namely the specific operation of reading serial port:


public class Abc implements SerialPortEventListener {
  public void serialEvent(SerialPortEvent arg0) {
    // TODO Auto-generated method stub
    // Judge and act on the following 
    /*
    BI - Communication interruption 
    CD - Carrier detect 
    CTS - Clear to send 
    DATA_AVAILABLE - Data arrives 
    DSR - Data set ready 
    FE - The frame error 
    OE - Overflow error 
    OUTPUT_BUFFER_EMPTY - The output buffer has been cleared 
    PE - Parity error 
    RI - Ringing indicating 
    */
    //switch More than one, if A single 
    if (arg0.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
      try {
        InputStream in = null;
        byte[] bytes = null;
        in = App.serialPort.getInputStream();

        int bufflenth = in.available();
        while (bufflenth != 0) {
          //  Initialize the byte The array is buffer The length of the data in 
          bytes = new byte[bufflenth];
          in.read(bytes);
          System.out.println(new String(bytes));
          bufflenth = in.available();
        }
        
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }  
}

Write operations:


OutputStream out = serialPort.getOutputStream();
out.write(data); //byte[] data;
out.flush();

conclusion

That is the end of this article on java serial communication implementation process examples, I hope to help you. If you have any questions, please feel free to leave a message and look forward to your valuable comments.


Related articles: