Java Outputstream write()使用RXTX与HC-05 BT模块通信时被阻止
在过去的三天里,我试图解决以下问题。我花了三天时间在许多论坛上浏览各种帖子 这是我的问题: 我尝试向通过虚拟Com端口连接的蓝牙HC-05设备发送命令。在我的例子中是“COM4” 我使用RXTX建立到串行端口的连接。这个很好用Java Outputstream write()使用RXTX与HC-05 BT模块通信时被阻止,java,bluetooth,serial-port,outputstream,rxtx,Java,Bluetooth,Serial Port,Outputstream,Rxtx,在过去的三天里,我试图解决以下问题。我花了三天时间在许多论坛上浏览各种帖子 这是我的问题: 我尝试向通过虚拟Com端口连接的蓝牙HC-05设备发送命令。在我的例子中是“COM4” 我使用RXTX建立到串行端口的连接。这个很好用 public static void main(String[] args) { portList = CommPortIdentifier.getPortIdentifiers(); boolean portFound = false; whi
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
boolean portFound = false;
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM4")) {
portFound = true;
SimpleRead reader = new SimpleRead();
}
}
}
}
@SuppressWarnings("restriction")
public SimpleRead() {
try {
serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
} catch (PortInUseException e) {System.out.println(e);}
try {
serialPort.setOutputBufferSize(15);
serialPort.setEndOfInputChar((byte)'\n');
inputStream = serialPort.getInputStream();
outputStream = serialPort.getOutputStream();
} catch (IOException e) {System.out.println(e);} catch (UnsupportedCommOperationException e)
e.printStackTrace();
}
try {
serialPort.addEventListener(this);
} catch (TooManyListenersException e) {System.out.println(e);}
serialPort.notifyOnDataAvailable(true);
try {
serialPort.setSerialPortParams(300,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
} catch (UnsupportedCommOperationException e) {System.out.println(e);}
serialPort.setRTS(true);
serialPort.setDTR(true);
serialPort.notifyOnDataAvailable(true);
serialPort.setInputBufferSize(8192);
serialPort.setOutputBufferSize(8192);
writeThread = new Thread(new SerialWriter(outputStream));
writeThread.start();
readThread = new Thread(this);
readThread.start();
}
问题发生在写入线程中。当调用ByteArrayOutputStream的writeTo()
方法时,程序将挂起而不会引发异常
public class SerialWriter implements Runnable {
OutputStream out;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
try {
Thread.sleep(500);
while (true) {
out.flush();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write("AT\n".getBytes(Charset.forName("ASCII")));
baos.writeTo(out); // <-- Here is my Problem. The program just
// hangs up
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
公共类SerialWriter实现可运行{
输出流输出;
公共SerialWriter(输出流输出){
this.out=out;
}
公开募捐{
试一试{
睡眠(500);
while(true){
out.flush();
ByteArrayOutputStream bas=新的ByteArrayOutputStream();
write(“AT\n”.getBytes(Charset.forName(“ASCII”));
资产负债表(表外);//如果ByteArrayOutputStream
包含的数据超过设备当时愿意读取的数据量,则可能会发生这种情况。谢谢。但我如何知道设备愿意读取多少数据?这取决于设备规格。您尚未指定要发送的数据或命令结构,以及“HC-05”设备可能不够具体,任何人都无法提供帮助。首先,a希望向该设备发送AT命令以获得连接。所有HC-05模块的情况是否相同?AT命令是否以CRLF结尾?