Sending data to Arduino Uno with Java

2020-06-20 15:50发布

问题:

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.

回答1:

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

And excerpt from the site mentioned by 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!").

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

http://arduino.cc/playground/Interfacing/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.

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.

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

#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