gpt4 book ai didi

Java - 扫描器类 : correct usage

转载 作者:太空宇宙 更新时间:2023-11-04 12:58:49 25 4
gpt4 key购买 nike

我正在使用 jSerialComm 库和 Scanner 类。当我尝试运行我的程序时,出现以下错误:

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)

指向这一行:Scanner Scanner = new Scanner(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");
}
}
}

}

最佳答案

我已经测试了你的代码,它可以在我的 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);
}

}

关于Java - 扫描器类 : correct usage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35120178/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com