使用java awt从arduino读取串行数据
在这里,我想从aruino串行端口读取数据,并将其显示在使用awt创建的GUI文本框中,但我遇到了一个空指针异常,有人能帮我解决吗使用java awt从arduino读取串行数据,java,awt,Java,Awt,在这里,我想从aruino串行端口读取数据,并将其显示在使用awt创建的GUI文本框中,但我遇到了一个空指针异常,有人能帮我解决吗 initially i uploaded this code into the arduino and disabled the port int val = 0; int led = 8; void setup() { Serial.begin(9600); pinMode(led, OUTPUT); } void loop() { delay(100
initially i uploaded this code into the arduino and disabled the port
int val = 0;
int led = 8;
void setup()
{
Serial.begin(9600);
pinMode(led, OUTPUT);
}
void loop()
{
delay(100);
}
void serialEvent() // To check if there is any data on the Serial line
{
while (Serial.available())
{
val = Serial.parseInt();
if(val == 1) //Switch on the LED, if the received value is 1.
{
digitalWrite(led, HIGH);
}
else if(val == 0) //Switch off the LED, if the received value is 1.
{
digitalWrite(led, LOW);
}
}
Serial.println("Succesfully received.");
}
这是考试班
package TestClass;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
import Arduino.SerialClass;
import Arduino.SerialClass;
import java.awt.Button;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TestClass extends Frame implements ActionListener
{
public static BufferedReader input;
public static OutputStream output;
TextField t1;
Button b1,b2;
InputStreamReader Ir = new InputStreamReader(System.in);
BufferedReader Br = new BufferedReader(Ir);
public static synchronized void writeData(String data) {
System.out.println("Sent: " + data);
try {
output.write(data.getBytes());
}
catch (Exception e) {
System.out.println("could not write to port");
}
}
TestClass()
{
t1=new TextField(20);
b1=new Button("Probe");
b2=new Button("Clear");
add(t1);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setFont(new Font("Arial",Font.BOLD,24));
setBackground(Color.CYAN);
setForeground(Color.BLUE);
setLayout(new FlowLayout());
setTitle("Mr.Mft");
setSize(500,500);
setVisible(true);
}
public static void main(String[] args) {
TestClass t=new TestClass();
SerialClass obj = new SerialClass();
int c=0;
obj.initialize();
input = SerialClass.input;
output = SerialClass.output;
obj.close();
}
public void actionPerformed(ActionEvent ae) {
String buttonName=ae.getActionCommand();
if(buttonName.equals("Probe"))
{
try {
String inputLine=input.readLine();
t1.setText(""+inputLine);
} catch (IOException ex) {
Logger.getLogger(TestClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
if(buttonName.equals("Clear"))
{
t1.setText("");
}
}
}
这是串行类:
package Arduino;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
public class SerialClass implements SerialPortEventListener {
public SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = {
"COM4", // Windows
};
public static BufferedReader input;
public static OutputStream output;
/** Milliseconds to block while waiting for port open */
public static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
public static final int DATA_RATE = 9600;
public void initialize() {
CommPortIdentifier portId = null;
Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();
//First, Find an instance of serial port as set in PORT_NAMES.
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 {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
char ch = 1;
output.write(ch);
// add event listeners
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 {
String inputLine=input.readLine();
System.out.println(inputLine);
}
catch (Exception e) {
System.err.println(e.toString());
}
}
}
public static synchronized void writeData(String data) {
System.out.println("Sent: " + data);
try {
output.write(data.getBytes());
}
catch (Exception e) {
System.out.println("could not write to port");
}
}
public static void main(String[] args) throws Exception {
SerialClass main = new SerialClass();
main.initialize();
Thread t=new Thread() {
public void run() {
//the following line will keep this app alive for 1000 seconds,
//waiting for events to occur and responding to them
try {
Thread.sleep(1500);
writeData("2");} catch (InterruptedException ie) {}
}
};
t.start();
System.out.println("Started");
}
public void setVisible(boolean b) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
I got these error messages
Dec 24, 2013 1:42:20 PM TestClass.TestClass actionPerformed
SEVERE: null
java.io.IOException
at gnu.io.RXTXPort$SerialInputStream.read(RXTXPort.java:1331)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at TestClass.TestClass.actionPerformed(TestClass.java:78)
at java.awt.Button.processActionEvent(Button.java:409)
at java.awt.Button.processEvent(Button.java:377)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
ajava.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
ajava.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:708)
at java.awt.EventQueue$4.run(EventQueue.java:706)
at java.security.AccessController.doPrivileged(Native Method)
ajava.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
atjava.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
atjava.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)
any help appreciated thanks
您正试图将空对象添加到
TestClass
构造函数中的框架中:
add(t1);
add(b1);
add(b2);
首先需要初始化它们,例如
t1=newtextfield()代码>发布堆栈跟踪。我在java.awt.Container.addImpl(Container.java:1086)的java.awt.Container.add(Container.java:410)的TestClass.TestClass.(TestClass.java:46)的TestClass.TestClass.main(TestClass.java:61)的线程“main”java.lang.NullPointerException中得到了这些错误消息Java结果:1构建成功(总时间:1秒)感谢您的帮助我已经修改了代码我得到了一个窗口,但我需要在我的文本字段t1上显示串行端口数据,当我按下探测按钮任何帮助请感谢