简体   繁体   English

在 java 中使用连续触发事件使用 arduino 控制直流电机

[英]Controlling a dc motor with arduino using triggering event continuously in java

I'm trying to control a small DC motor through Arduino Uno with pc java.我正在尝试使用 pc java 通过 Arduino Uno 控制小型直流电机。 This java application is server app and connect with android app using socket.这个java应用程序是服务器应用程序并使用套接字与android应用程序连接。 And it works on single command from java.它适用于来自 java 的单个命令。 But doesn't work continuously pressed.但不能连续按下。 I want rotating motor when I continuously press down the button and stop press up.当我连续按下按钮并停止按下时,我想要旋转电机。 Please help me.请帮我。

Here is android code.这是安卓代码。 /continuously press/ / 连续按 /

   class RepetitiveUpdater implements Runnable {

        @Override
        public void run() {
            if (autoIncrement) {
                increment();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                repeatUpdateHandler.postDelayed(new RepetitiveUpdater(), REPEAT_DELAY);
            } else if (autoDecrement) {
                decrement();
                repeatUpdateHandler.postDelayed(new RepetitiveUpdater(), REPEAT_DELAY);
            }
        }

    }

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            increment();
        }
    });

    btn.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            autoIncrement = true;
            repeatUpdateHandler.post(new RepetitiveUpdater());
            return false;
        }
    });

    btn1.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View v) {
            autoDecrement = true;
            repeatUpdateHandler.post(new RepetitiveUpdater());
            return false;
        }
    });

And here is java server.这是java服务器。

private void sendMsgToPort(int k) {
    try {
        String s = String.valueOf(k);
        char buf2[] = s.toCharArray();
        serialPort.writeByte((byte) buf2[0]);
    } catch (SerialPortException ex) {
        System.out.println(ex);
    }
}

// ........
        while (true) {
            try {

                clientSocket = serverSocket.accept(); // accept the client connection
                inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
                bufferedReader = new BufferedReader(inputStreamReader); // get the client message
                message = bufferedReader.read();
                sendMsgToPort(message);
                System.out.println("m=:" + message);
               // System.out.println("Serial : "+serialPort.readString());
                //inputStreamReader.close();
               // clientSocket.close();
                //System.out.println(serialPort.readBytes());
                //Thread.sleep(100);

            } catch (IOException ex) {
                // status.setText("Error: " + ex);
                System.out.println("Problem in message reading");
            } 
        } 

And here is ardiuno example code:这是 ardiuno 示例代码:

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
{
   if (Serial.available())
   {
       val = Serial.parseInt();
       if(val == 1)   //Switch on the LED, if the received value is 1.
       {
          digitalWrite(led, HIGH);
       }
   }

}

` `

One problem is in your Java server program:一个问题出在您的 Java 服务器程序中:

clientSocket = serverSocket.accept(); clientSocket = serverSocket.accept();

You accept a new connection, read/process one message, and then?您接受一个新连接,读取/处理一条消息,然后呢? That client socket just goes out of scope.该客户端套接字超出了范围。 The connection is just left open and your code waits for a new connection again.连接只是保持打开状态,您的代码再次等待新连接。

Search for "Java multithreaded server" examples, it's really easy.搜索“Java 多线程服务器”示例,真的很简单。 Have each connection fully handled in a dedicated thread after accepting, so that you can do both wait for another connection and handle a longer dialog on an established connection at the same time.接受后在专用线程中完全处理每个连接,以便您可以等待另一个连接并同时在已建立的连接上处理更长的对话。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM