使用 Java 将数据发送到 Arduino Uno [英] Sending data to Arduino Uno with Java

查看:37
本文介绍了使用 Java 将数据发送到 Arduino Uno的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

听起来像是您想在 Java 中执行此操作.

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

摘自jayeff:

OutputStream 自带 3 种不同的写方法来发送数据从计算机到 Arduino.在上面的例子中,你可以使用output.write(String) 发送数据,如 output.write("HelloArduino!").

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!").

如果您正在尝试使用 Java 向 Arduino 写入数据,那么这就是您的答案.

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

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

编辑:如果你想使用 Java 以外的东西,你可以这样做:

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

问,你会收到.您可以使用任何具有串行支持的编程语言执行此操作.

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

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

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

  • Perl - Device::SerialPort
  • Python - pySerial via Android Playground
  • C++ - LibSerial (used below)

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

Note: Watch out for the nasty Auto Reset on Serial issue. See my previous answer for more details on that.

这是我的 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] = '\0';
        bitset(buffer[0]);
        //myss << buffer;
        myss.write(buffer,1);
        //myss.Close();

    }
}

这篇关于使用 Java 将数据发送到 Arduino Uno的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆