Giu*_*_ai 6 java serial-port arduino-uno
我正在尝试建立java-arduino串行通信。到目前为止,我已经从Arduino页面(http://playground.arduino.cc/interface/java)下载了代码,但出现错误。码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import gnu.io.SerialPortEvent;
import gnu.io.SerialPortEventListener;
import java.util.Enumeration;
public class SerialTest implements SerialPortEventListener {
SerialPort serialPort;
/** The port we're normally going to use. */
private static final String PORT_NAMES[] = {
"/dev/tty.usbserial-A9007UX1", // Mac OS X
"/dev/ttyACM0", // Raspberry Pi
"/dev/ttyUSB0", // Linux
"COM3", // Windows
};
/**
* A BufferedReader which will be fed by a InputStreamReader
* converting the bytes into characters
* making the displayed results codepage independent
*/
private BufferedReader input;
/** The output stream to the port */
private OutputStream output;
/** Milliseconds to block while waiting for port open */
private static final int TIME_OUT = 2000;
/** Default bits per second for COM port. */
private static final int DATA_RATE = 9600;
public void initialize() {
// the next line is for Raspberry Pi and
// gets us into the while loop and was suggested here was suggested http://www.raspberrypi.org/phpBB3/viewtopic.php?f=81&t=32186
System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");
CommPortIdentifier portId = null;
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)) {
portId = currPortId;
break;
}
}
}
if (portId == null) {
System.out.println("Could not find COM port.");
return;
}
try {
// open serial port, and use class name for the appName.
serialPort = (SerialPort) portId.open(this.getClass().getName(),
TIME_OUT);
// set port parameters
serialPort.setSerialPortParams(DATA_RATE,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
// open the streams
input = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
output = serialPort.getOutputStream();
// add event listeners
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
} catch (Exception e) {
System.err.println(e.toString());
}
}
/**
* This should be called when you stop using the port.
* This will prevent port locking on platforms like Linux.
*/
public synchronized void close() {
if (serialPort != null) {
serialPort.removeEventListener();
serialPort.close();
}
}
/**
* Handle an event on the serial port. Read the data and print it.
*/
public synchronized void serialEvent(SerialPortEvent oEvent) {
if (oEvent.getEventType() == SerialPortEvent.DATA_AVAILABLE) {
try {
String inputLine=input.readLine();
System.out.println(inputLine);
} catch (Exception e) {
System.err.println(e.toString());
}
}
// Ignore all the other eventTypes, but you should consider the other ones.
}
public static void main(String[] args) throws Exception {
SerialTest main = new SerialTest();
main.initialize();
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(1000000);} catch (InterruptedException ie) {}
}
};
t.start();
System.out.println("Started");
}
}
Run Code Online (Sandbox Code Playgroud)
那就是我得到的输出:
稳定的库=========================================本机库版本= RXTX-2.2 pre2 Java库版本= RXTX-2.1-7警告:RXTX版本不匹配Jar版本= RXTX-2.1-7本机库版本= RXTX-2.2pre2
找不到COM端口。已开始
我正在使用Linux。而且我认为端口是正确的。有人可以帮忙吗?
在看到问题并在 RXTX 和 JSSC 上挣扎了一段时间后,我开发了JAVA Arduino 通信库。
我的图书馆(希望如此)有很好的文档(在图书馆的 SourceForge Wiki 上找到),并告诉你如何安装和使用它。在库中下载两个 JAR 并将它们包含在类路径中后,只需在类定义之前包含以下语句:
import arduino.*;
Run Code Online (Sandbox Code Playgroud)
现在,您只需要方法中的以下代码片段,就可以开始了!
String ArduinoPort = ""; //Your port name here
int BAUD_RATE = 9600;
Arduino arduino = new Arduino(ArduinoPort, BAUD_RATE);
arduino.openConnection();
arduino.serialWrite('1'); //serialWrite is an overridden method, allowing both characters and strings.
arduino.serialWrite('1', 20); //its second parameter even allows delays. more details can be found in the documentation.
Run Code Online (Sandbox Code Playgroud)
当您的 Arduino 无法与 Arduino IDE 连接时,默认错误是“无法找到 Com 端口”。
但在这种情况下,错误并不是说 Arduino IDE 无法看到您的 Arduino,而是说 RXTX 库无法看到 Arduino。
您的电脑驱动程序可能是旧版本,尚不支持 RXTX 所需的 com...
这是我的建议:
插入您的 Arduino 板
转到控制面板 ---> 系统和安全 ---> 系统 ---> 左侧窗格中的设备管理器
展开其他设备。
在“其他设备”下,您会注意到一个带有黄色小错误图形的图标。(拔掉连接到任何串行端口的所有其他设备)
右键单击该设备 ---> 更新驱动程序软件
选择浏览我的计算机以获取驱动程序软件
单击浏览 --->浏览您从 Arduino 网站下载的 Arduino 环境文件夹。如果没有下载则http://arduino.cc/en/Main/Software
浏览后标记包含子文件夹。
单击下一步 ---> 将安装您的驱动程序。
折叠其他设备 ---> 展开端口(仅在设备管理器中的其他设备下)
你会看到Arduino Written --->寻找它的COM PORT(关闭设备管理器)
转到 Arduino 环境 ---> 工具 ---> 串行端口 ---> 选择设备管理器中的端口中提到的 COM 端口。(如果您使用任何其他 Arduino 板而不是 UNO,则在板中选择相同的板)
一些参考资料:http://www.devfactor.net/2014/10/26/arduino-basics-serial-communication/
| 归档时间: |
|
| 查看次数: |
10238 次 |
| 最近记录: |