读取完整的行 Java 串行端口 [英] Read Complete Line Java Serial Port

查看:56
本文介绍了读取完整的行 Java 串行端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了 JSSC API,因此我可以与 Com 端口进行通信.我发送一个类似N\r\n"的命令

I have implmented JSSC API so I can communicate with the Com Port. I send a command like "N\r\n"

我在普通超级终端中收到的应该是这样的:

and what i receive in a normal hyperterminal should look like this:

0100071CA79215021803164442180000

0100071CA79215021803164442180000

0100071C9F5415021803164514520000

0100071C9F5415021803164514520000

0100071CDF5115022106142956600000

0100071CDF5115022106142956600000

没问题

但是当我对 JSSC API 做同样的事情时,我收到了这个(只有第一个代码)

But when i do the same with the JSSC API i receive this (only the first code)

010

0071CA79

2150218

0316444

218

问题是我随机收到位部分,在代码的末尾我丢失了一些部分.但这并不重要,我只需要每个代码的前 12 位数字.

The Problem is that i randomly receive bit parts and at the end of the code i lose some parts. But thats not important i only need the first 12 digits of every code.

现在的问题是如何让函数只接收整行而不是位部分?

The Question is now how do i get the function to only receive the full line and not bitparts?

这是课程的接收部分类 PortReader2 实现了 SerialPortEventListener {

This is the receiving part of the class class PortReader2 implements SerialPortEventListener {

    @Override
    public void serialEvent(SerialPortEvent event) {
        if(event.isRXCHAR()&& event.getEventValue() > 2) {
            try {
                // получение ответа от порта
                String receivedData = serialPort.readString();
                System.out.println(receivedData.length() + ":" + receivedData);
            }
            catch (SerialPortException ex) {
                System.out.println("Error in receiving response from port: " + ex);
            }
        }

    }
}

这是发送部分

    public void sendCodeCommand(SerialPort serialPort) {
    // writing string to port
    try {
        Thread.sleep(3000);
        serialPort.writeBytes("N\r\n".getBytes());
    } catch (SerialPortException | InterruptedException ex) {
        Logger.getLogger(ComPortSendReceive.class.getName()).log(Level.SEVERE, null, ex);
    }
        System.out.println("String wrote to port, waiting for response..");
    }

推荐答案

要解决上述问题,您需要将收到的字符串连接到另一个字符串中,该字符串累积从 readString() 函数收到的字符串片段,该函数位于serialEvent 处理程序.因为它是 owm 线程,所以它获得一定数量的 CPU 时间来获取串行数据,因此它有效地获取串行端口输入数据的部分顺序读取.因此,此示例将部分输入放在一起以创建整体"输入.

To fix the stated problem you need to concatenate the strings you receive into another string that accumulates the string fragments you receive from the readString() function, which is in the serialEvent handler. Because it is it's owm thread it gets a certain amount of cpu time to get serial data so it effectively gets partial sequential reads of the serial port input data. So this example puts the partial inputs together to create the "whole" input.

String serialString;

因此在 serialEvent 处理程序中:

So within the serialEvent handler:

try {
serialStringFragment = serialPort.readString();
serialString.concat(serialStringFragment);
}

无论是从 readString() 获得的字符串还是累积字符串,都可以扫描像 eol 条件这样的标记.例如:

Either the string you get from readString() or the accumulation string can be scanned for tokens like eol condition. For Example:

String [] dlLines = serialString.split("\r\n");

会将每一行拆分为 dlLines 字符串数组中的一个元素.

will break each line out to an element in the dlLines string array.

但是我发现如果我的目标设备有固定长度的输出,效果会更好:

However I have found that if I have fixed length output from my target device this works better:

serialPort.readString(int bytecount);

内联写入串行字符串,消除serialEvent 处理程序.

inline with the write serial string, eliminating the serialEvent handler.

这有点做作,但换句话说:

This is a bit contrived but In other words:

String expectedStringLength "0100071CA79215021803164442180000";
int serialstrlen = expectedStringLength.length();

因此,serialstrlen 显然应该成为每个预期行的常量.

So the serialstrlen should obviously become constants for each expected line.

serialPort.writeString("N\r\n");
serialPort.readString(serialstrlen+2); // assuming cr lf 

应该给你第一行.

将 readString() 放入循环中,根据预期的字符串更改 serialstrelen 参数值.检查字符串的替代内容以进行错误处理.这对我有用.

Put the readString() in a loop, change serialstrelen argument value according to the expected strings. Check the strings for alternate content for error handling. This has worked for me.

这篇关于读取完整的行 Java 串行端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆