我正在尝试建立一个Java接口,将一些AT命令发送到GPRS模块。
我已经在Processing上有一个工作界面,但是我迁移到了纯Java,因为我发现它更容易制作图形界面。
Processing程序通过串口向COM#发送数据。 COM#是带有GPRS Shield的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());
}
}
}
所以我知道它工作正常,因为我使用Processing接口测试了它,并且一个名为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");
}
}
}
这是我的主要课程:
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,但我得到的结果相同。
答案 0 :(得分:0)
我解决了这个问题。它不是jSSC或RXTX库。
PeterMmm是对的,我需要延迟,但我需要在我的java程序上。
当我测试通信时,我正在打开端口并立即发送数据。问题是Arduino
默认情况下在建立连接时重置,因此Arduino尚未准备好处理它。
几天前,我决定再次尝试使用jSSC,但这一次,java程序要求输入通过串口发送,留出时间让Arduino启动。 Arduino成功收到我输入的任何内容。
现在我在我正在开发的用户界面上实现了串行通信,一切运行顺利。