java中的串口读取

发布于 2025-01-06 12:30:40 字数 1159 浏览 1 评论 0原文

这是我的 Java 串行通信中的问题...我的 jennic 硬件设备是使用 UART 连接的。我想从我的设备检索值..

我在 SerialPortEvent.DATA_AVAILABLE 中接收字符串的字节数组,

        case SerialPortEvent.DATA_AVAILABLE:
            try {
                     int size;
                     while(inputStream.available()!=0) {
                         byte buff[]=new byte[100];
                         size=inputStream.read(buff);
                         inputStream.close();
                         String result = new String(buff,0,size);
                         ZPS_tsAplZdpIeeeAddrRsp IeeRsp = new ZPS_tsAplZdpIeeeAddrRsp(result);
               }

首先我读取字节并将其存储在 buff[] 中。然后将其转换为字符串,然后将其转换为字符串数组..但我的问题是我得到的输出就像但很少有中断。

示例输出:

  80011634002078445541560000341201004189

  80011635002078445541560000341201004189

  80011636002078445541560000341201004189
  /*Here is Break my seq */
  800116370020784455

  41560000341201004189/*this two breaking seq generated two separate array and here is the problem*/

  80011638002078445541560000341201004189

刷新输入缓冲区是否有问题我已经尝试过 inputStream.reset() 但它不起作用..任何人都可以给我一个合适的建议来克服这个问题..

谢谢...

Here is problem in my Java Serial Communication ... my jennic hardware device is connected using UART. I want to retrieve values form my device ..

i am receiving byte array of string in SerialPortEvent.DATA_AVAILABLE

        case SerialPortEvent.DATA_AVAILABLE:
            try {
                     int size;
                     while(inputStream.available()!=0) {
                         byte buff[]=new byte[100];
                         size=inputStream.read(buff);
                         inputStream.close();
                         String result = new String(buff,0,size);
                         ZPS_tsAplZdpIeeeAddrRsp IeeRsp = new ZPS_tsAplZdpIeeeAddrRsp(result);
               }

first I read the bytes and store it in buff[]. then convert it into string and convert it to string array there after .. but my problem is i get the out put like but few time its breaks.

Sample output:

  80011634002078445541560000341201004189

  80011635002078445541560000341201004189

  80011636002078445541560000341201004189
  /*Here is Break my seq */
  800116370020784455

  41560000341201004189/*this two breaking seq generated two separate array and here is the problem*/

  80011638002078445541560000341201004189

is there problem for flushing the input buffer I have tried inputStream.reset() but it doesn't work.. can anyone give me a suitable suggestion to overcome the problem..

thanks...

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

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

发布评论

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

评论(3

三生殊途 2025-01-13 12:30:41

“问题”在于你的期望。没有任何地方说 read() 将填充缓冲区,或者串行数据传输将保留您的消息边界。这取决于你。你得到的只是一个字节流。

The 'problem' is in your expectations. Nowhere does it say that read() will fill the buffer, or that serial data transfer will preserve your message boundaries. That's up to you. All you get is a byte stream.

ゞ花落谁相伴 2025-01-13 12:30:41

您需要从端口读入缓冲区,当该缓冲区具有完整消息时,将缓冲区的该部分刷新到消息处理例程中。这意味着您需要以每条消息都可以独立识别和隔离的方式定义消息。

当数据可用或不可用时,读取流将起作用或阻塞;但是,从流中读取数据并不能保证您获得单个消息大小的数据。您只会注意到有数据要读取。您注意到当串行端口缓冲区中的数据可供读取时存在一个常见问题,并且您在所有消息可供读取之前就开始读取它。请记住,可能会出现另一个问题,也许在另一次运行期间,在程序准备好读取“下一条”消息之前,可能会在串行端口缓冲区中缓冲两条或更多消息。

重新设计通信协议,将字节读入缓冲区(一个类),该缓冲区保存字节,直到消息可供读取为止。然后在该缓冲区上放置一个接口 readMessage(),其行为类似于 read(),但在消息级别除外(缓冲直到获得完整消息)。

You need to read from the port into a buffer, and when that buffer has a whole message, flush that portion of the buffer into your message handling routines. This means you need to define your messages in a manner where each message can independently be identified and isolated.

Reading a stream will work or block when data is available or unavailable; however, reading from a stream won't guarantee that you get your data in one message-sized pieces. You only get notice that there is data to be read. You noticed a common issue when data is available to be read in the serial port buffer, and you started reading it before all of the message was available to be read. Remember that there is another issue which can occur, perhaps during another run two or more messages might be buffered in the serial port buffers before your program is ready to read the "next" message.

Rework you communication protocol to read bytes into a buffer (a class), which holds bytes until messages are available to be read. Then put an interface on that buffer readMessage() which acts like read() except at the message level (buffering until it gets a full message).

还不是爱你 2025-01-13 12:30:41

一般来说,您不能指望从串行连接一端发送的“消息”将被作为一组全部接收。您可以一次获得全部内容,也可以分成不同长度的几块。由您的接收程序使用它对传入数据的了解来从串行端口读取字节并将它们放在一起并意识到何时收到完整的消息。

通常,设备通过以下三种方式之一处理此问题:

  • 固定长度数据包 - 读取直到获得 X 字节,然后处理这些 X 字节。
  • 数据包长度是数据包标头的一部分,指示在将接收到的数据视为完整数据包之前要读取多少附加字节。
  • 数据包开始/结束指示器(通常为 STX 或 SOH 开始,ETX 结束)。您可以将开始指示器和结束指示器之间收到的所有数据视为一个消息包。

In general you cannot expect that a "message" sent from one end of a serial connection is going to be received all as one group. You may get it all at once or in several chunks of varying lengths. It is up to your receiving program to use what it knows about the incoming data to read bytes from the serial port and put them together and realize when a complete message has been received.

Normally devices handle this in one of three ways:

  • Fix length packets - you read until you get X bytes and then process those X bytes.
  • Packet length is part of the packet header indicating how many additional bytes to read before considering the data received so far as a complete packet.
  • Packet start/end indicators (STX or SOH to start and ETX to end usually). You treat all data received between a start and end indicator as one message packet.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文