如何从串行端口读取文本?
我正在尝试通过 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法从您的代码中看出,但看起来您正在使用
java.io.InputStream
来读取该对象。 您应该使用 java.io.BufferedReader 或 java.io.InputSreamReader。有很多方法可以做到这一点,但我们使用的是:
I can't tell from your code, but it looks like you're using
java.io.InputStream
to read the object. You should use eitherjava.io.BufferedReader
orjava.io.InputSreamReader
.There are many ways to do this, but this is what we use:
连接的设备可能不使用文本协议。 许多设备使用二进制消息格式,其中可能嵌入文本,但用更紧凑的代码表示其他信息。
请编辑您的问题并提供更多信息,例如您通过串行端口与之通信的设备,以及从运行的代码中获得的输出。 要添加的另一个有用的输出是打印您已读取的数据的十六进制表示形式。 一个快速而肮脏的方法如下:
我没有使用过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:
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?
请我从二进制字符串
please me to string from binary