我正在使用NetBeans和Arduino IDE进行Arduino项目。那段时间我感觉到了这个问题。我正在使用我的代码从Arduino板上获取信号,使用可变电阻为我的程序获取各种信号。
我正在附加我的NetBeans源代码。
package hirikattaproject;
import com.fazecast.jSerialComm.SerialPort;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Scanner;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class HirikattaProject {
static SerialPort chosenPort;
public static void main(String[] args) {
//Create confugaration window
JFrame window=new JFrame();
window.setTitle("Sensor value");
window.setSize(600,400);
window.setLayout(new BorderLayout());
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//create a dropdown list and button
JComboBox<String> portList=new JComboBox<String>();
JButton connectionButton=new JButton("connect");
JPanel topPanel=new JPanel();
topPanel.add(portList);
topPanel.add(connectionButton);
window.add(topPanel,BorderLayout.NORTH);
//populate the dropdown list
SerialPort[] portNames=SerialPort.getCommPorts();
for(int i=0;i<portNames.length;i++){
portList.addItem(portNames[i].getSystemPortName());
}
//create line Graph
XYSeries series=new XYSeries("Sensor Reading");
XYSeriesCollection dataset=new XYSeriesCollection();
JFreeChart chart=ChartFactory.createXYLineChart("SensorReading", "time(Second)", "Reading", dataset, PlotOrientation.HORIZONTAL, true, true, true);
window.add(new ChartPanel(chart),BorderLayout.CENTER);
//configure the connection button and use another thread to listen for data
connectionButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent arg0){
if(connectionButton.getText().equals("connect")){
//attempt to coonect serial port
chosenPort =SerialPort.getCommPort(portList.getSelectedItem().toString());
chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0);
if(chosenPort.openPort()){
connectionButton.setText("Disconnect");
portList.setEnabled(false);
}
//create new threed that listens for incoming text and populates the graph
Thread thread=new Thread(){
@Override
public void run(){
Scanner scanner = new Scanner(chosenPort.getInputStream());
int x=0;
while(scanner.hasNextLine()){
String line=scanner.nextLine();
int number =Integer.parseInt(line);
series.add(x++,number);
}
}
};
thread.start();
}else{
//disconnect serial port
chosenPort.closePort();
portList.setEnabled(true);
connectionButton.setText("connect");
}
}
});
//show window
window.setVisible(true);
}
}
和Arduino源代码:
void setup() {
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
int s= analogRead(A0);
Serial.println(s);
}`.
我正在添加Arduino电路图像和错误图像
我也在添加此图片,我认为这可能是错误。
答案 0 :(得分:0)
我认为问题出在图书馆。
复制库 rxtxSerial.dll rxtxParallel.dll
在文件夹
C:\ Program Files \ Java \ jdkxxx \ bin \
C:\ Program Files \ Java \ jrexxx \ bin \
并将RXTXcomm.jar附加到项目中