2

I'm having issues reading the actual double values from my Arduino Serial.println() in my Java app. I've tried following usage examples found here but get what I guess is a hex value. The serial monitor within the Arduino program is displaying the correct values.

This is the code: I'm fairly new to this and would appreciate any comments regarding style/problems/inefficiencies within my code!

import com.fazecast.jSerialComm.*;
import java.util.*;
import java.io.*;

public class PortRead {
  public static SerialPort userPort;
  static InputStream in;

  public static void main(String args[]) {
    Scanner input = new Scanner(System.in);
    /*
     This returns an array of commport addresses, not useful for the client
     but useful for iterating through to get an actual list of com parts available
    */
    SerialPort ports[] = SerialPort.getCommPorts();
    int i = 1;
    //User port selection
    System.out.println("COM Ports available on machine");
    for (SerialPort port : ports) {
      //iterator to pass through port array
      System.out.println(i++ + ": " + port.getSystemPortName()); //print windows com ports
    }
    System.out.println("Please select COM PORT: 'COM#'");
    SerialPort userPort = SerialPort.getCommPort(input.nextLine());

    //Initializing port
    userPort.openPort();
    if (userPort.isOpen()) {
    System.out.println("Port initialized!");
    //timeout not needed for event based reading
    //userPort.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0);
    } else {
    System.out.println("Port not available");
    return;
    }

    userPort.addDataListener(new SerialPortDataListener() {
      @Override
      public int getListeningEvents() {
        return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
      }
      public void serialEvent(SerialPortEvent event) {
        if (event.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE)
        return;
        byte[] newData = new byte[userPort.bytesAvailable()];
        int numRead = userPort.readBytes(newData, newData.length);
        System.out.println("Read " + numRead + " bytes.");
      }
    });
  }
}
  • 2
    Your code is reading characters and displaying the numeric value of the character codes. You need instead to collect full strings (perhaps until a newline, not merely the amount of data vailable when your call returns!) and then parse numeric values out of that. This is really a Stackoverflow question, not an Arduino-specific one. – Chris Stratton Oct 12 '15 at 5:13
  • Above code reading data sent by arduino and storing into byte [ ], instead of showing how much contents you have read, you need to show the total string. – Datta Jan 2 '16 at 4:12
0

Since you are reading received data into array newData, you should replace System.out.println("Read " + numRead + " bytes."); with System.out.print(newData); (not sure about the syntax, since I dont know Java) if you want to print the actual data received and not just the number of bytes received.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.