Arduino 无法发回串行数据 [英] Arduino not able to send serial data back

查看:38
本文介绍了Arduino 无法发回串行数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我发现了如何将 Arduino 连接到我的 Java 程序.但是使用串行连接不会返回任何有用的数据,它要么格式错误,要么只是作为一个盒子发送.我已经查看了此处早期发布的相关问题,但似乎没有任何提示有帮助.那么有谁知道如何使用串口在Arduino和计算机之间发送数据?

So I found out how to connect the Arduino to my java program. But using the serial connections doesn't give any useful data back, its either in the wrong format, or just sends it as a box. I've looked at the related questions posted early in here, but none of the tips seems to help. So does anyone know how to send the data between an Arduino and a computer using the serial port?

这是我正在使用的代码,由此人提供:http://silveiraneto.net/2009/03/01/arduino-and-java/

This is the code I'm using, provided by this person: http://silveiraneto.net/2009/03/01/arduino-and-java/

package serialtalk;

import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.InputStream;
import java.io.OutputStream;
import processing.app.Preferences;

public class Main {
    static InputStream input;
    static OutputStream output;

    public static void main(String[] args) throws Exception{
        Preferences.init();
        System.out.println("Using port: " + Preferences.get("serial.port"));
        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(
                Preferences.get("serial.port"));

        SerialPort port = (SerialPort)portId.open("serial talk", 4000);
        input = port.getInputStream();
        output = port.getOutputStream();
        port.setSerialPortParams(Preferences.getInteger("serial.debug_rate"),
                SerialPort.DATABITS_8,
                SerialPort.STOPBITS_1,
                SerialPort.PARITY_NONE);
        while(true){
            while(input.available()>0) {
                System.out.print((char)(input.read()));
            }
        }
    }
}

Arduino 是这样的:http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove

The Arduino is this: http://www.arduino.cc/en/Main/ArduinoBoardDuemilanove

代码只是接收一个数字,并确定它应该从我的 Arduino 发回哪个模拟读数.

The code simply receives a number, and determines which analog reading it should be sending back, from my Arduino.

推荐答案

在处理串行连接时,请确保以下关键点:

When dealing with serial connections make sure of the following key points:

  • 波特率匹配
  • DATABITS 应该匹配
  • STOPBITS 应该匹配
  • PARITY 应该匹配
  • 确保您使用的是正确的电缆(如果需要,请使用空调制解调器)
  • 确保电缆长度不要太长

上述所有情况都会导致 Java 端的 com 端口出现奇怪的东西.一旦你掌握了这一点,事情就会变得容易得多.

All of the above can cause odd stuff to come out of the com port on the Java side. Once you get the hang of this it gets much easier.

我个人最喜欢的图书馆这里.

My personal favorite library here.

这篇关于Arduino 无法发回串行数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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