2016-01-31 78 views
1

我正在使用jSerialComm庫和掃描儀類。當我嘗試運行我的程序我收到以下錯誤:Java - 掃描儀類:正確的用法

Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.(Unknown Source)
at java.io.InputStreamReader.(Unknown Source)
at java.util.Scanner.(Unknown Source)
at SerialComm.main(SerialComm.java:44)

指向這一行:掃描儀掃描=新的掃描儀(port.getInputStream());

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

public class SerialComm { 

public static void main(String[] args) { 
    // TODO Auto-generated method stub 
    SerialPort serPort[] = SerialPort.getCommPorts(); 

    int i = 0; 

    for(SerialPort port : serPort) 
    { 
     System.out.println(i++ + " " + port.getSystemPortName()); 
    } 

    Scanner s = new Scanner(System.in); 

    int selected = s.nextInt(); 

    SerialPort port = serPort[selected]; 


    System.out.println(port.getBaudRate()); 
    port.setBaudRate(115200); 
    System.out.println(port.getBaudRate()); 
    port.setNumDataBits(8); 
    port.setNumStopBits(1); 
    port.setParity(SerialPort.NO_PARITY); 

    try 
    { 
     port.openPort(); 
     System.out.println("Connection is opend"); 
    } 
    catch (Exception ex) 
    { 
     System.out.println("Chack connection ther is a problem"); 
    } 

    port.setComPortTimeouts(SerialPort.TIMEOUT_READ_SEMI_BLOCKING, 100, 0); 

    Scanner scanner = new Scanner(port.getInputStream()); 

    while(scanner.hasNextLine()) 
    { 
     try 
     { 
      String line = scanner.nextLine(); 
      System.out.println(line); 
     } 
     catch(Exception ex) 
     { 
      System.out.println("halo"); 
     } 
    } 
} 

}

回答

0

我已經測試你的代碼,它正常工作與我的Arduino,這Arduino的草圖

void setup(){ 
    Serial.begin(115200); 
} 

void loop(){ 
    for(int i = 0;i<1024;i++){ 
    Serial.println(i); 
    delay(1); 
    } 

    for(int i = 1023;i>0;i--){ 
    Serial.println(i); 
    delay(1); 
    } 

}