使用 Java 将数据发送到 Arduino Uno

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11306859/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:37:10  来源:igfitidea点击:

Sending data to Arduino Uno with Java

javaarduino

提问by cheese5505

I have just bought an Arduino Uno and I am currently trying to make a flashing LED. I was wondering how I could do this, and I have looked at the Arduino Playground and have found a program to get input. I need to output to the Arduino. It is not possible for me to use anything but Java because I already have another program that requires an Arduino. Please leave any ideas.

我刚刚买了一个 Arduino Uno,我目前正在尝试制作一个闪烁的 LED。我想知道如何才能做到这一点,我查看了 Arduino Playground 并找到了一个获取输入的程序。我需要输出到Arduino。我不可能使用 Java 以外的任何东西,因为我已经有另一个需要 Arduino 的程序。请留下任何想法。

采纳答案by ZnArK

EDIT:Kind of sounds like you want to do this in java.

编辑:听起来你想在java中做到这一点。

And excerpt from the sitementioned by jayeff:

摘自jayeff提到的网站

The OutputStream comes with 3 different write methods to send data from the computer to the Arduino. In the above example, you could use output.write(String)to send data, as in output.write("Hello Arduino!").

OutputStream 带有 3 种不同的写入方法,可将数据从计算机发送到 Arduino。在上面的示例中,您可以使用 output.write(String)发送数据,如output.write("Hello Arduino!").

If you're trying to use Java to write to the Arduino, then this is your answer.

如果您尝试使用 Java 写入 Arduino,那么这就是您的答案。

http://arduino.cc/playground/Interfacing/Java

http://arduino.cc/playground/Interface/Java



EDIT: If you want to use something other than Java, here you go:

编辑:如果您想使用 Java 以外的其他东西,请执行以下操作:

Ask and you shall receive. You can do this in any programming language that has serial support.

问你应接受。您可以使用任何具有串行支持的编程语言执行此操作。

There are certainly other methods for each language, but here are some I found in 5 minutes at the Google Machine

每种语言肯定还有其他方法,但这里有一些我在 5 分钟内在 Google Machine 上找到的

Note:Watch out for the nasty Auto Reset on Serialissue. See my previous answerfor more details on that.

注意:注意串行问题令人讨厌的自动重置。有关更多详细信息,请参阅我之前的回答

Here's my C++ Code (it's ugly but it works)

这是我的 C++ 代码(虽然丑陋但有效)

#include <SerialStream.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;
class SerialComm {
    LibSerial::SerialStream myss;
public:

    SerialComm(int argc, char** argv) {
        myss = new LibSerial::SerialStream("/dev/ttyS0", ios_base::out);
        myss.SetBaudRate(LibSerial::SerialStreamBuf::BAUD_57600);
        myss.SetCharSize(LibSerial::SerialStreamBuf::CHAR_SIZE_8);
        myss.SetFlowControl(LibSerial::SerialStreamBuf::FLOW_CONTROL_NONE);
        myss.SetParity(LibSerial::SerialStreamBuf::PARITY_NONE);
        myss.SetNumOfStopBits(1);

        const int Dsize = 2;
        char buffer[1];
        buffer[0] = 125; //0b00000001;
        buffer[1] = '##代码##';
        bitset(buffer[0]);
        //myss << buffer;
        myss.write(buffer,1);
        //myss.Close();

    }
}