如何从串行端口读取文本?

发布于 2024-07-13 08:12:38 字数 855 浏览 3 评论 0原文

我正在尝试通过 Java 从 Windows 串行端口读取数据。 我有 javax.comm 库,并且能够获取一些数据,但不是正确的数据。 当我将端口读入字节数组并将其转换为文本时,我得到一系列字符,但没有真正的文本字符串。 我尝试将字节数组指定为“UTF-8”和“US-ASCII”。 有谁知道如何从中获取真实文本?

这是我的代码:

while (inputStream.available() > 0) {
 int numBytes = inputStream.read(readBuffer);
 System.out.println("Reading from " + portId.getName() + ": ");
 System.out.println("Read " + numBytes + " bytes");
}

System.out.println(new String(readBuffer));
System.out.println(new String(readBuffer, "UTF-8"));
System.out.println(new String(readBuffer, "US-ASCII"));

前三行的输出不允许我复制和粘贴(我假设是因为它们不是普通字符)。 这是十六进制的输出: 78786000e67e9e60061e8606781e66e0869e98e086f89898861878809e1e9880

我正在从 Hollux GPS 设备读取数据,该设备以字符串格式输出。 我确信这一点,因为我是通过 C# 做到的。

我用于通信的设置(我知道这些设置是从 C# 应用程序中的工作中得到的)是: 波特率:9600 数据位:8 停止位:1 奇偶校验:无

I am trying to read data off of a Windows serial port through Java. I have the javax.comm libraries and am able to get some data but not correct data. When I read the port into a byte array and convert it to text I get a series of characters but no real text string. I have tried to specify the byte array as being both "UTF-8" and "US-ASCII". Does anyone know how to get real text out of this?

Here is my code:

while (inputStream.available() > 0) {
 int numBytes = inputStream.read(readBuffer);
 System.out.println("Reading from " + portId.getName() + ": ");
 System.out.println("Read " + numBytes + " bytes");
}

System.out.println(new String(readBuffer));
System.out.println(new String(readBuffer, "UTF-8"));
System.out.println(new String(readBuffer, "US-ASCII"));

the output of the first three lines will not let me copy and paste (I assume because they are not normal characters). Here is the output of the Hex:
78786000e67e9e60061e8606781e66e0869e98e086f89898861878809e1e9880

I am reading from a Hollux GPS device which does output in string format. I know this for sure because I did it through C#.

The settings that I am using for communication which I know are right from the work in the C# app are:
Baud Rate: 9600
Databits: 8
Stop bit: 1
parity: none

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

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

发布评论

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

评论(3

旧时模样 2024-07-20 08:12:39

我无法从您的代码中看出,但看起来您正在使用 java.io.InputStream 来读取该对象。 您应该使用 java.io.BufferedReader 或 java.io.InputSreamReader。

有很多方法可以做到这一点,但我们使用的是:

javax.comm.SerialPort port = ...{initialize your serial port}
BufferedReader portReader = 
    new BufferedReader(new InputStreamReader(port.getInputStream()));

try {
    String line = portReader.readLine();
} catch(IOException e) { ... }

I can't tell from your code, but it looks like you're using java.io.InputStream to read the object. You should use either java.io.BufferedReader or java.io.InputSreamReader.

There are many ways to do this, but this is what we use:

javax.comm.SerialPort port = ...{initialize your serial port}
BufferedReader portReader = 
    new BufferedReader(new InputStreamReader(port.getInputStream()));

try {
    String line = portReader.readLine();
} catch(IOException e) { ... }
吃→可爱长大的 2024-07-20 08:12:39

连接的设备可能不使用文本协议。 许多设备使用二进制消息格式,其中可能嵌入文本,但用更紧凑的代码表示其他信息。

请编辑您的问题并提供更多信息,例如您通过串行端口与之通信的设备,以及从运行的代码中获得的输出。 要添加的另一个有用的输出是打印您已读取的数据的十六进制表示形式。 一个快速而肮脏的方法如下:

import java.math.BigInteger;
...
System.out.println(new BigInteger(1, readBuffer).toString(16));

我没有使用过Holux GPS,但我已经用javax.comm 编写了一个Garmin 界面。 在这种情况下,设备默认使用专有(二进制)Garmin 协议,但可以在设备上启用(基于文本的)NMEA 0183 协议。 您是否有可能正在接收专有的 Holux 消息格式,并且需要在 GPS 单元上使用某些命令来切换协议?

It's likely that the connected device doesn't use a text protocol. Many devices use a binary message format that may have text embedded in it, but represents other information with more compact codes.

Please edit your question and provide more information, such as the device that you're communicating with through the serial port, and the output that you get from the code you have running. Another helpful output to add would be something that prints a hexadecimal representation of the data you've read. A quick-and-dirty method for that is the following:

import java.math.BigInteger;
...
System.out.println(new BigInteger(1, readBuffer).toString(16));

I haven't worked with Holux GPS, but I have written a Garmin interface with javax.comm. In that case, the unit uses a proprietary (binary) Garmin protocol by default, but the (text-based) NMEA 0183 protocol can be enabled on the device. Is it possible that you are receiving a proprietary Holux message format, and need to use some command on the GPS unit to switch protocols?

全部不再 2024-07-20 08:12:39
 byte[] readBuffer = null;
                try {
                    while (inputStream.available() > 0) {
                        int n = inputStream.available();
                        readBuffer = new byte[n];
                        inputStream.read(readBuffer, 0, n);
                    }
                    String y = new String(readBuffer, StandardCharsets.ISO_8859_1);
                    String str = new String(readBuffer, "UTF-8");
                    jTextField3.setText("    " + str+ "    ");

请我从二进制字符串

 byte[] readBuffer = null;
                try {
                    while (inputStream.available() > 0) {
                        int n = inputStream.available();
                        readBuffer = new byte[n];
                        inputStream.read(readBuffer, 0, n);
                    }
                    String y = new String(readBuffer, StandardCharsets.ISO_8859_1);
                    String str = new String(readBuffer, "UTF-8");
                    jTextField3.setText("    " + str+ "    ");

please me to string from binary

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