Java RXTX如何从com端口读取数据
嗨,我有这样的事Java RXTX如何从com端口读取数据,java,rxtx,Java,Rxtx,嗨,我有这样的事 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 = CommPor
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");
}
}
但我不知道如何从端口读取??我读过,但我不知道他们是什么意思
编辑
我已经添加了此代码,但我收到了
稳定库
==================================================================================本机库版本=
RXTX-2.1-7 Java lib Version=RXTX-2.1-7 start/dev/ttyUSB3->null
->基础输入流返回零字节停止
在您的try块中类似于以下内容:
OutputStream outStream = p.getOutputStream();
InputStream inStream = p.getInputStream();
在您的try块中类似于以下内容:
OutputStream outStream = p.getOutputStream();
InputStream inStream = p.getInputStream();
这是你的密码吗?你到底想在那里做什么p 要从串行端口读取,您需要声明此端口:
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/tty/USB0"); //on unix based system
SerialPort serialPort = (SerialPort) portIdentifier.open("NameOfConnection-whatever", 0);
然后在此端口上打开连接:
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/tty/USB0"); //on unix based system
SerialPort serialPort = (SerialPort) portIdentifier.open("NameOfConnection-whatever", 0);
下一步是在需要时设置此端口的参数:
serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
这是我的配置-您的配置可能会相应不同:
现在,您可以读取此端口上的一些数据了!
要获取数据,您需要获取serialPorts inputstream并从中读取:
InputStream inputStream = serialPort.getInputStream();
while (active) {
try {
byte[] buffer = new byte[22];
while ((buffer[0] = (byte) inputStream.read()) != 'R') {
}
int i = 1;
while (i < 22) {
if (!active) {
break;
}
buffer[i++] = (byte) inputStream.read();
}
//do with the buffer whatever you want!
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
}
}
我在这里实际做的是使用inputstream的read方法读取。这将阻塞直到数据可用,或者如果到达流的末尾,则返回-1。在本例中,我等待得到一个“R”字符,然后将接下来的22个字节读入缓冲区。这就是你读取数据的方式
获取serialPorts inputstream
使用.read方法
在我的例子中,当取消时,可以通过另一种方法将active设置为false,从而结束读取过程。
希望这有帮助这是你的代码吗?你到底想在那里做什么p 要从串行端口读取,您需要声明此端口:
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/tty/USB0"); //on unix based system
SerialPort serialPort = (SerialPort) portIdentifier.open("NameOfConnection-whatever", 0);
然后在此端口上打开连接:
CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/tty/USB0"); //on unix based system
SerialPort serialPort = (SerialPort) portIdentifier.open("NameOfConnection-whatever", 0);
下一步是在需要时设置此端口的参数:
serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
这是我的配置-您的配置可能会相应不同:
现在,您可以读取此端口上的一些数据了!
要获取数据,您需要获取serialPorts inputstream并从中读取:
InputStream inputStream = serialPort.getInputStream();
while (active) {
try {
byte[] buffer = new byte[22];
while ((buffer[0] = (byte) inputStream.read()) != 'R') {
}
int i = 1;
while (i < 22) {
if (!active) {
break;
}
buffer[i++] = (byte) inputStream.read();
}
//do with the buffer whatever you want!
} catch (IOException ex) {
logger.error(ex.getMessage(), ex);
}
}
我在这里实际做的是使用inputstream的read方法读取。这将阻塞直到数据可用,或者如果到达流的末尾,则返回-1。在本例中,我等待得到一个“R”字符,然后将接下来的22个字节读入缓冲区。这就是你读取数据的方式
获取serialPorts inputstream
使用.read方法
在我的例子中,当取消时,可以通过另一种方法将active设置为false,从而结束读取过程。
希望这有帮助尝试使用
if (socketReader.ready()) {
}
因此,套接字仅在缓冲流中有要读取的内容时才会响应
因此,该异常永远不会发生。尝试使用
if (socketReader.ready()) {
}
因此,套接字仅在缓冲流中有要读取的内容时才会响应
所以这个异常永远不会发生。那么什么是p=SerialPort.opencore,1000;对于,这个第一部分核心需要??当当前所有者在1000ms后没有释放连接时,serialport连接被命名为core和Timeout。。您可能不应该有任何进程以任何方式使用此端口。此名称将标识端口的所有者。只需使用您的应用程序名。更多信息:那么什么是p=SerialPort.opencore,1000;对于,这个第一部分核心需要??当当前所有者在1000ms后没有释放连接时,serialport连接被命名为core和Timeout。。您可能不应该有任何进程以任何方式使用此端口。此名称将标识端口的所有者。只需使用您的应用程序名。更多信息:有后续跟进吗?完全没有反应p这可能是流量控制的问题。您可以使用port.setFlowControl方法在端口上进行设置。但是,我无法告诉您应该使用哪种流量控制。也许可以开始阅读有关串行端口的一般内容:我建议阅读inputstream字节信息,而不是行信息。但这取决于您实际尝试与之通信的设备。我使用TTL到RS232转换器,但为什么我得到的是ttyUSB0而不是ttyS0??但当我插入pendrive时,它没有看到任何后续操作?完全没有反应p这可能是流量控制的问题。您可以使用port.setFlowControl方法在端口上进行设置。但是,我无法告诉您应该使用哪种流量控制。也许可以开始阅读有关串行端口的一般内容:我建议阅读inputstream字节信息,而不是行信息。但这取决于您实际尝试与之通信的设备。我使用TTL到RS232转换器,但为什么我得到的是ttyUSB0而不是ttyS0??但当我插入pendrive时,它看不到它