喜读书,我有财产以后这样RXTX如何从COM端口
package compot;
import java.util.Enumeration;
import gnu.io.*;
public class core {
private static SerialPort p;
/**
* @param args
*/
public static void main(String[] args)
{
Enumeration ports = CommPortIdentifier.getPortIdentifiers();
System.out.println("start");
while(ports.hasMoreElements())
{
CommPortIdentifier port = (CommPortIdentifier) ports.nextElement();
System.out.print(port.getName() + " -> " + port.getCurrentOwner() + " -> ");
switch(port.getPortType())
{
case CommPortIdentifier.PORT_PARALLEL:
System.out.println("parell");
break;
case CommPortIdentifier.PORT_SERIAL:
//System.out.println("serial");
try {
p = (SerialPort) port.open("core", 1000);
int baudRate = 57600; // 57600bps
p.setSerialPortParams(
baudRate,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_NONE);
} catch (PortInUseException e) {
System.out.println(e.getMessage());
} catch (UnsupportedCommOperationException e) {
System.out.println(e.getMessage());
}
break;
}
}
System.out.println("stop");
}
}
但我不知道如何从端口读取?我已阅读this tutorial,但我不知道他们是什么“演示应用程序”?
编辑
OutputStream outStream = p.getOutputStream();
InputStream inStream = p.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(inStream));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
我添加此代码,但我recive
稳定图书馆 =================== ====================== Native lib版本= RXTX-2.1-7 Java lib版本= RXTX-2.1-7开始/ dev/ttyUSB3 - > null - >底层输入流返回零字节停止
任何后续行动?根本没有反应? :p – chzbrgla
我编辑我的问题 –
这可能是流量控制的问题。你可以用port.setFlowControl()方法在端口上设置它。但是,我不可能知道应该使用哪种流量控制。也许开始阅读有关串行端口的一般信息:http://tldp.org/HOWTO/Serial-HOWTO-4.html我建议阅读输入流字节明智不行。但这取决于您实际尝试与之通信的设备。 – chzbrgla