在Java中阅读Arduino A0,A1和A2,独立

问题描述 投票:0回答:1

我从控制台上得到它:

0

====数据完成====

3

====数据完成====

5

====数据完成====

我想达到彼此分开的A0,A1,A2的值,但它们都在一起。 我的arduino代码是:

int firstSensor = 0; // first analog sensor int secondSensor = 0; // second analog sensor int thirdSensor = 0; // third analog sensor int inByte = 0; // incoming serial byte void setup() { Serial.begin(9600); } void loop() { // read first analog input firstSensor = analogRead(A0); // delay 10ms to let the ADC recover: delay(10); // read second analog input secondSensor = analogRead(A1); delay(10), // read third analog input thirdSensor = analogRead(A2); // send sensor values: Serial.println(firstSensor); Serial.println(secondSensor); Serial.println(thirdSensor); }

我是Java,Arduino和这里的新手

提前感谢

查看这一点 简而言之,您需要添加一些额外的数据,例如

<sensor_name>=<sensor_value>
然后在您的Java代码中,您可以在=上拆分并获取传感器名称和值

公共类Test5 { 静态串行portsport;
java serial-port arduino
1个回答
1
投票
public static void main(String[] args) { // create and configure the window JFrame window = new JFrame(); window.setVisible(true); window.setTitle("Sensor Graph GUI"); window.setSize(800, 400); window.setLayout(new BorderLayout()); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.revalidate(); // create a drop-down box and connect button, then place them at the top of the window JComboBox<String> portList = new JComboBox<String>(); portList.setPreferredSize(new Dimension(100,25)); JButton connectButton = new JButton("Connect"); JPanel topPanel = new JPanel(); topPanel.add(portList); topPanel.add(connectButton); window.add(topPanel, BorderLayout.NORTH); GridBagLayout gridbag = new GridBagLayout(); topPanel.setLayout(gridbag); GridBagConstraints C1 = new GridBagConstraints(); C1.fill = GridBagConstraints.HORIZONTAL; JLabel label1=new JLabel("sensor-1"); C1.gridx = 1; C1.gridy = 3; topPanel.add(label1,C1); JLabel label2=new JLabel(" s-1 value "); label2.setPreferredSize(new Dimension(100,25)); label2.setBorder(BorderFactory.createLineBorder(Color.black)); label2.setSize(300,50); C1.gridx = 2; C1.gridy = 3; topPanel.add(label2,C1); JLabel label3=new JLabel("sensor-2"); C1.gridx = 1; C1.gridy = 4; topPanel.add(label3,C1); JLabel label4=new JLabel(" s-2 value "); label4.setPreferredSize(new Dimension(100,25)); label4.setBorder(BorderFactory.createLineBorder(Color.black)); C1.gridx = 2; C1.gridy = 4; topPanel.add(label4,C1); JLabel label5=new JLabel(" Data "); label5.setPreferredSize(new Dimension(100,25)); label5.setBorder(BorderFactory.createLineBorder(Color.black)); C1.gridx = 2; C1.gridy = 5; topPanel.add(label5,C1); JButton button1=new JButton("Get Data"); topPanel.add(button1); button1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String S1=label4.getText(); label5.setText(S1); } }); // populate the drop-down box SerialPort[] portNames = SerialPort.getCommPorts(); for(int i = 0; i < portNames.length; i++) portList.addItem(portNames[i].getSystemPortName()); // configure the connect button and use another thread to listen for data connectButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if(connectButton.getText().equals("Connect")) { // attempt to connect to the serial port chosenPort = SerialPort.getCommPort(portList.getSelectedItem().toString()); chosenPort.setComPortTimeouts(SerialPort.TIMEOUT_SCANNER, 0, 0); if(chosenPort.openPort()) { connectButton.setText("Disconnect"); portList.setEnabled(false); } // create a new thread that listens for incoming text and populates the graph Thread thread=new Thread(){ @Override public void run(){ Scanner scanner=new Scanner(chosenPort.getInputStream()); String line=scanner.nextLine(); label1.setText(line); label2.setText(scanner.nextLine()); label3.setText(scanner.nextLine()); label4.setText(scanner.nextLine()); scanner.close(); }}; thread.start(); } else { //disconnect from the serial port chosenPort.closePort(); portList.setEnabled(true); connectButton.setEnabled(true); }}});

} }


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.