gpt4 book ai didi

Java RXTX 不发送数据

转载 作者:太空宇宙 更新时间:2023-11-04 15:15:15 27 4
gpt4 key购买 nike

我正在尝试制作一个 Java 接口(interface)来向 GPRS 模块发送一些 AT 命令。

我已经有了一个关于Processing的工作界面,但我迁移到纯Java,因为我发现制作图形界面更容易。

处理程序通过串行将数据发送到 COM#。 COM# 是带有 GPRS 扩展板的 Arduino。 Arduino 代码所做的就是将接收到的数据传递到 GPRS 模块,反之亦然:

void loop(){
if (GPRS.available())
{
while(GPRS.available())
{
buffer[count++]=GPRS.read();
if(count == 64)break;
}
Serial.write(buffer,count);
clearBufferArray();
count = 0;
}
if (Serial.available()){
delay(100);
while(Serial.available()){
GPRS.write(Serial.read());
}
}
}

所以我知道它工作得很好,因为我已经使用处理接口(interface)和一个名为 SSCOM 的外部工具对其进行了测试,并且所有命令都被正确解释。

现在的问题是,当我尝试使用 RXTX 在 java 上制作界面时,它根本不起作用。我在控制台上没有收到任何错误,每次我运行 java 时,我在 Arduino 上收到的唯一数据是 ÿ (char 255),并且在打开端口时发送它,写入串口时不会。

这是我正在使用的SerialHandler类。我在网上找到了它。

import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialPortHandler {
private SerialPort serialPort;
private OutputStream outStream;
private InputStream inStream;

public void connect(String portName) throws IOException, PortInUseException, NoSuchPortException {
try {
// Obtain a CommPortIdentifier object for the port you want to open
CommPortIdentifier portId =
CommPortIdentifier.getPortIdentifier(portName);

// Get the port's ownership
serialPort =
(SerialPort) portId.open("Demo application", 5000);

// Set the parameters of the connection.
setSerialPortParameters();

// Open the input and output streams for the connection. If they won't
// open, close the port before throwing an exception.
outStream = serialPort.getOutputStream();
inStream = serialPort.getInputStream();
} catch (NoSuchPortException e) {
throw new IOException(e.getMessage());
} catch (PortInUseException e) {
throw new IOException(e.getMessage());
} catch (IOException e) {
serialPort.close();
throw e;
}
}

/**
* Get the serial port input stream
* @return The serial port input stream
*/
public InputStream getSerialInputStream() {
return inStream;
}

/**
* Get the serial port output stream
* @return The serial port output stream
*/
public OutputStream getSerialOutputStream() {
return outStream;
}

/**
* Sets the serial port parameters
*/
private void setSerialPortParameters() throws IOException {
int baudRate = 19200;

try {
serialPort.setSerialPortParams(
baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);

serialPort.setFlowControlMode(
SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException ex) {
throw new IOException("Unsupported serial port parameter");
}
}
}

这是我的主要类(class):

public class Main {
SerialPortHandler serial = new SerialPortHandler();
serial.connect("COM3");
OutputStream serialOut = serial.getSerialOutputStream();
String s = "AT+CPOWD=0\r\n";
serialOut.write(s.getBytes());
serialOut.flush();
}

}

rxtxSerial.dll和RXTXcomm.jar分别位于jre\bin和jre\lib\ext中。

我找不到问题,就像我说的,我在任何地方都没有收到任何错误或警告。

我使用的是 NetBeans 7.3.1、JDK 1.7 和 Windows 8.1 x64。

我还尝试使用 jSSC但我得到了相同的结果。

最佳答案

我解决了这个问题。它不是 jSSC 或 RXTX 库。

PeterMmm 是对的,我需要延迟,但我的 java 程序需要它。当我测试通信时,我正在打开端口并立即发送数据。问题是 Arduino 默认情况下会在建立连接时重置,因此 Arduino 尚未准备好处理它。

几天前,我决定使用 jSSC 再试一次,但这一次,java 程序要求通过串行端口发送输入,为 Arduino 启动留出了时间。 Arduino 成功接收了我输入的任何内容。

现在我在正在开发的用户界面上实现了串行通信,一切都运行顺利。

关于Java RXTX 不发送数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20916793/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com