RXTX如何从com口读取

发布于 2024-11-25 09:06:58 字数 2389 浏览 11 评论 0原文

嗨,我有这样的东西

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 版本 = RXTX-2.1-7 start /dev/ttyUSB3 ->无效的 ->底层输入流返回零字节停止

Hi i have somthing like this

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");
    }
}

But I dont know how to read from port ?? I have read this tutorial but i dont know what "Demo application" they mean ??

EDIT

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();

I have add this code but i recive

Stable Library
========================================= Native lib Version =
RXTX-2.1-7 Java lib Version = RXTX-2.1-7 start /dev/ttyUSB3 -> null
-> Underlying input stream returned zero bytes stop

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱,才寂寞 2024-12-02 09:06:58

这是你的代码吗?你到底想在那里做什么? :p

为了从 SerialPort 读取数据,您需要声明此端口:

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);

这是我的配置 - 您的配置可能会有所不同:)

现在您已准备好读取此端口上的一些数据!
要获取数据,您需要获取串行端口输入流并从中读取:

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);
        }
}

我实际上在这里做的是使用它的 read() 方法从输入流中读取数据。这将阻塞直到数据可用,或者如果到达流末尾则返回 -1。在此示例中,我等到收到“R”字符,然后将接下来的 22 个字节读入缓冲区。这就是读取数据的方式。

  1. 获取串行端口输入流,
  2. 使用 .read() 方法
  3. 所有这些都在循环内,并在取消时退出循环(在我的例子中,可以通过另一种方法将 active 设置为 false,从而结束读取过程。

希望这有帮助

Is this your code? What are you actually trying to do there? :p

In order to read from a SerialPort, you need to declare this port:

CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/tty/USB0"); //on unix based system

Then open a connection on this port:

SerialPort serialPort = (SerialPort) portIdentifier.open("NameOfConnection-whatever", 0);

Next step would be to set the params of this port (if needed):

serialPort.setSerialPortParams(38400, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

This is my config - your's might differ accordingly :)

Now you're ready to read some data on this port!
To get the data, you need to get the serialPorts inputstream and read from that:

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);
        }
}

What I'm actually doing here is reading from the inputstream using it's read() method. This will block until data is available or return -1 if end of stream is reached. In this example I wait until I get an 'R' character and then read the next 22 bytes into a buffer. And that's how you read data.

  1. Get the serialPorts inputstream
  2. use .read() method
  3. have that all inside a loop and exit loop when canceled (in my case, the active can be set to false by another method and thus end the reading process.

hope this helps

单身情人 2024-12-02 09:06:58

尝试使用

if (socketReader.ready()) {
}

,以便套接字仅在缓冲流中有内容要读取时才响应
所以异常永远不会发生。

Try using

if (socketReader.ready()) {
}

so that the socket responds only when there is something to read in the Buffer Stream
so the Exception never Occurs.

雪花飘飘的天空 2024-12-02 09:06:58

你的 try 块中是这样的:

OutputStream outStream = p.getOutputStream();
InputStream inStream = p.getInputStream();

Something like this in your try block:

OutputStream outStream = p.getOutputStream();
InputStream inStream = p.getInputStream();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文