嗨,我有这样的东西
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");
}
}
但我不知道如何从端口读取??我已经阅读了本教程,但我不知道它们是什么意思的“演示应用程序”??
编辑
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();
我已经添加了这段代码,但我收到了
稳定库 =========================================== 原生库版本 = RXTX-2.1 -7 Java lib Version = RXTX-2.1-7 start /dev/ttyUSB3 -> null -> 底层输入流返回零字节停止