首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Java与Arduino问题的通信

Java与Arduino问题的通信
EN

Stack Overflow用户
提问于 2022-05-28 13:34:48
回答 1查看 79关注 0票数 2

我正在尝试与Java通信Arduino,为此我使用了JSerialComm。我试图使用GUI使用java打开和关闭led。我遇到的问题是,当我试图打开领头时,它会立即闪现,但我试图让它开着或关着按钮。当我在Arduino中使用并编写1或0时,它可以正常工作,但是当使用带有按钮的GUI时,由于某种原因它不能工作。

Ardiuno法典:

代码语言:javascript
运行
复制
#define LED 13

int input;

void setup()
{
  Serial.begin(9600);
  pinMode(LED, OUTPUT);
}


void loop()
{
      

 if (Serial.available() > 0) {

    input = Serial.read();
    Serial.println(input);
    

    if (input == '1') {


       Serial.println("Start");
       digitalWrite(LED, HIGH);
      
    } else if (input == '0') {

        Serial.println("Stop);
        digitalWrite(LED, LOW);

      
    }
  
 }
}

Java代码Ardiuno连接,在这里,我们创建用于打开led和off的连接和类:

代码语言:javascript
运行
复制
import com.fazecast.jSerialComm.SerialPort;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class ArdiunoConnection {

    SerialPort sp;


    public boolean getConnection() {

        sp = SerialPort.getCommPorts()[0];
        sp.setComPortParameters(9600, 8, 1, 0);
        sp.setComPortTimeouts(SerialPort.TIMEOUT_WRITE_BLOCKING, 0, 0);
        return sp.openPort();

    }

    public void turnOn() throws IOException {


        if (getConnection()) {

            sp.getOutputStream().write('1');
            sp.getOutputStream().flush();
            System.out.println("Sent number: " + 0);
            sp.closePort();

        } else {

            System.out.println("Error");


        }

    }

    public void turnOff() throws IOException {

        if (getConnection()) {


            sp.getOutputStream().write('0');
            sp.getOutputStream().flush();
            System.out.println("Sent number:" + 0);
            sp.closePort();


        } else {


            System.out.println("Error");


        }
    }





}

用于图形用户界面的Java代码,其中我们创建了一个ActionListener,用于关闭或在

代码语言:javascript
运行
复制
import com.fazecast.jSerialComm.SerialPort;

import javax.swing.*;
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 Systeem extends javax.swing.JFrame {

    boolean light;
    ArdiunoConnection obj;
    private JButton btnLight;
    private JPanel MainPaneltje;


    public Systeem() {

        setContentPane(MainPaneltje);

        btnLight.setText("Starten");
        obj = new ArdiunoConnection();


        setTitle("GUI");
        setSize(600, 400);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setVisible(true);


        btnLight.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                try {


                    if (light) {

                        btnLight.setText("Starten");
                        obj.turnOn();
                        light = false;

                    } else {

                        btnLight.setText("Stoppen");
                        obj.turnOff();
                        light = true;


                    }



                } catch (IOException ex) {


                    Logger.getLogger(System.class.getName()).log(Level.SEVERE, null, ex);

                }
            }
        });
    }


    public static void main(String[] args) {


        Systeem gui = new Systeem();



    }



}
EN

回答 1

Stack Overflow用户

发布于 2022-06-03 15:29:38

Arduino的正常配置是创建串行连接时重新启动。在java类方法中,可以使用:

代码语言:javascript
运行
复制
if (getConnection())

这将导致调用getConnection()方法,该方法打开串口,因此Arduino重新启动,因此LED就会关闭。

有两项建议:

  1. 如果需要测试串口的状态,请使用if (sp.openPort())而不是if (getConnection())
  2. 但更好的做法是,在使用该程序时不要关闭串口连接,这样就不必再次打开它。不需要在turnOn/ need方法中使用sp.closePort()语句。相反,创建一个ArduinoConnection.close()方法,并在GUI退出时调用它。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72416117

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档