简体   繁体   English

从Arduino Uno读取字节

[英]Reading bytes from Arduino Uno

I'm using the RxTx library for communication between Arduino and Java app. 我正在使用RxTx库在Arduino和Java应用程序之间进行通信。 My Arduino cod is: 我的Arduino鳕鱼是:

void setup(){
  Serial.begin(9600);
}

void loop(){
  Serial.println("Hello world");
  delay(1000);
} 

If you want to see all my java cods All . 如果您想查看我所有的java cods All I have a method for read bytes from arduino 我有一种从arduino读取字节的方法

public byte[] readBlocked(int num) throws IOException {
        byte[] buff;
        buff = new byte[num];

       this.inputStream.readFully(buff, 0, num);

        return buff;
    }

In TestComunication class i print receive data 在TestComunication类中,我打印接收数据

public class TestComunication {

    private Connection connection;
    //private CommPortIdentifier port;
    private static final String PORT_NAMES[] = {
        "/dev/tty.usbserial-A9007UX1", // Mac OS X
        "/dev/ttyACM0", // Raspberry Pi
        "/dev/ttyUSB0", // Linux
        "COM3", // Windows
        "/dev/tty.usbmodem621",
        "/dev/tty.usbmodem411"
    };

    public TestComunication() throws IOException {

        SerialClassConnection serialClassConnection = null;
        CommPortIdentifier port = null;

        serialClassConnection = SerialClassConnection.getInstance();
        Enumeration portEnum = CommPortIdentifier.getPortIdentifiers();

        //First, Find an instance of serial port as set in PORT_NAMES.
        while (portEnum.hasMoreElements()) {
            CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
            for (String portName : PORT_NAMES) {
                if (currPortId.getName().equals(portName)) {
                    port = currPortId;
                    break;
                }
            }
        }

        if (port == null) {
            System.out.println("Could not find COM port.");
            return;
        }
        System.out.println(port.getName());

        serialClassConnection.openPort(port);

        this.connection = serialClassConnection;

        this.manageData(this.connection);
    }

    private void manageData(Connection conn) throws IOException {

        Connection connection;
        int availableBytes;
        byte[] inBytes;

        connection = conn;
        // listen forever for incoming data

        while (true) {
            if (connection.isDataAvailable()) {

                inBytes = connection.readBlocked(11);
                String text = new String(inBytes, "UTF-8");
                System.out.println(text);
            }

        }
    }

    public static void main(String[] args) throws IOException {

        new TestComunication();
        Thread t = new Thread() {
            public void run() {
                //the following line will keep this app alive for 1000 seconds,
                //waiting for events to occur and responding to them (printing incoming messages to console).
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ie) {
                }
            }
        };
        t.start();
        System.out.println("Started");

    }
}

I am sending string 我正在发送字符串

Hello world 你好,世界

from Arduino Uno. 来自Arduino Uno In my Java app i' m reading this string as bytes. 在我的Java应用程序中,我正在以字符串形式读取此字符串。 But i'm receiving choppy data. 但是我收到的数据不连贯。 Example: 例:

Native lib Version = RXTX-2.1-7
Java lib Version   = RXTX-2.1-7
/dev/tty.usbmodem621
Held
Hello
world
Hel
lo world
H
ello world

The main problem: The string Hello World convert to ASCII is 主要问题:字符串Hello World转换为ASCII是

072 101 108 108 111 032 119 111 114 108 100 072 101 108 108 111 032 119 111 114 108 100

but i don't receive that sequence every time. 但我不会每次都收到该序列。 Example i receive: 我收到的示例:

072 108 101 108 111 032 119 111 114 108 100 072 108 101 108 111 032 119 111 114 108 100

101 072 108 108 111 032 111 114 108 119 100 101072108108111032111114108119100

101 072 108 111 032 114 108 111 119 100 108 101 072 108 111 032 114 108 111119 100 108

But length of receive data is correct 11 bytes 但是接收数据的长度是正确的11个字节

Hmmm what return this.inputStream.readFully(buff, 0, num); 嗯什么返回this.inputStream.readFully(buff, 0, num); check this method, maybe it's wrong 检查此方法,也许是错误的

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

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