0

I am writing a code to get data from a radiometer sensor connected to one of the "COMs" of the computer, to get the measurement i have to communicate with that sensor throw "COM7" and write the command "gi" to get a value like ""9.919e-08" for example from the command user interface.

Now i have a problem with the code and it is giving me that error "No line found"

enter image description here

and here is the code

package reading_data;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

import com.fazecast.jSerialComm.SerialPort;

public class main {

public static void main(String[] args) throws IOException, InterruptedException {
        
    SerialPort sp= SerialPort.getCommPort("COM7");
    sp.setComPortParameters(115200, 8, 1, 0);
    sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);

    sp.openPort();
    
    if(sp.isOpen()) {
    System.out.println("Port is open");
        
    PrintWriter output=new PrintWriter(sp.getOutputStream());
    Scanner data=new Scanner(sp.getInputStream());
    output.println("gi");
    String ssss=data.nextLine();
    System.out.println("--++++---->"+ssss);
    
    }else {
        System.out.println("Port is not open");
    }   
    
    sp.closePort();

}
}

and here is the error i get

Port is open
Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Unknown Source)
at reading_data.main.main(main.java:26)

May you please tell me where is my mistake?

thanks in advance

KamelK
  • 71
  • 9
  • 1
    Personally I would call output.flush() after output.println(...) if I want to be sure data is sent. Usually close() should be enough, but if yoou need data before try to flush them when you need. – sigur Nov 17 '20 at 10:32
  • https://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found already answered here – Umer Farooq Nov 17 '20 at 10:51
  • @sigur hello sir, i added that line but still have a problem – KamelK Nov 17 '20 at 20:30
  • Are you sure the "COMX" has written something? Maybe you're trying to obtain a response before the COM could put the response. There're different ways, the simpliest should be: you put a wait condition in a while !data.hasNextLine and sleep for some milliseconds. Then when data.hasNextLine is true you do the nextLine. – sigur Nov 18 '20 at 15:39

1 Answers1

2

Try like this:

output.flush();    
if(data.hasNextLine()) {
  String ssss=data.nextLine();
}

If no next line, then don't call the nextLine. Edit.: flush needs to be done.

Qbi
  • 21
  • 3
  • hello sir, i did it but the program doesn't enter the if condition, i also tried tp print "data" and the output in the console is like this "java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=true][skipped=false][group separator=\x{2c}][decimal separator=\x{2e}][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q?\E][infinity string=\Q?\E]" – KamelK Nov 17 '20 at 20:28
  • I have modified my comment. As @sigur stated in his comment, you have to flush. Without that, data.hasnext will be always false, because it has no data. – Qbi Nov 18 '20 at 22:25