简体   繁体   中英

Arduino serial port communication

I'm trying to set up a Java-Arduino serial communication. So I downloaded source code for googling. But I can't send correct data from Arduino to the database (MySQL).

public class Serial implements SerialPortEventListener {
  SerialPort serialPort;
  private static final String PORT_NAMES[] = {
    "/dev/tty.usbserial-A9007UX1", // Mac OS X
    "/dev/ttyUSB0", // Linux
    "COM3", // Windows
  };

  private InputStream input;
  private OutputStream output;
  private static final int TIME_OUT = 2000;
  private static final int DATA_RATE = 9600;

  public void initialize() {
    CommPortIdentifier portId = null;
    Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
    while (portEnum.hasMoreElements()) {
      CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
      for (String portName : PORT_NAMES) {
        if (currPortId.getName().equals(portName)) {
          portId = currPortId;
          break;
        }
      }
    }

    if (portId == null) {
      System.out.println("Could not find COM port.");
      return;
    }

    try {
      serialPort = (SerialPort) portId.open(this.getClass().getName(), TIME_OUT);
      serialPort.setSerialPortParams(DATA_RATE, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
      input = serialPort.getInputStream();
      output = serialPort.getOutputStream();
      serialPort.addEventListener(this);
      serialPort.notifyOnDataAvailable(true);
    } catch (Exception e) {
      System.err.println(e.toString());
    }
  }

  public synchronized void close() {
    if (serialPort != null) {
      serialPort.removeEventListener();
      serialPort.close();
    }
  }

  public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
      try {
        int available = input.available();
        byte chunk[] = new byte[available];
        input.read(chunk, 0, available);
        // print to console
        System.out.print(new String(chunk));
        Frequency frequnecy = new Frequency();
        FrequencyDAO frequencyDAO = new FrequencyDAO();
        frequencyDAO.insertFrequency(new String(chunk));
      } catch (Exception e) {
        System.err.println(e.toString());
      }
    }
  }
}

And the output I get:

在此处输入图片说明

在此处输入图片说明

You want to change the type of field input to BufferedReader and initialize it like:

input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));

Using a the buffered reader you're able to read a line at a time, which is, I assume, what you want to store in the database.

Then update the event handler accordingly:

public synchronized void serialEvent(SerialPortEvent oEvent) {
    if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
        try {
            String line = input.readLine();
            System.out.println(line);

            FrequencyDAO frequencyDAO = new FrequencyDAO();
            frequencyDAO.insertFrequency(line);
        } catch (Exception e) {
            System.err.println(e.toString());
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM