简体   繁体   English

阅读Complete Line Java串行端口

[英]Read Complete Line Java Serial Port

I have implmented JSSC API so I can communicate with the Com Port. 我已经实现了JSSC API,因此我可以与Com Port进行通信。 I send a command like "N\\r\\n" 我发送一个像“N \\ r \\ n”这样的命令

and what i receive in a normal hyperterminal should look like this: 我在普通超级终端中收到的内容应如下所示:

0100071CA79215021803164442180000 0100071CA79215021803164442180000

0100071C9F5415021803164514520000 0100071C9F5415021803164514520000

0100071CDF5115022106142956600000 0100071CDF5115022106142956600000

NOK NOK

But when i do the same with the JSSC API i receive this (only the first code) 但是,当我使用JSSC API执行相同操作时,我会收到此信息(仅限第一个代码)

010 010

0071CA79 0071CA79

2150218 2150218

0316444 0316444

218 218

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. 但这并不重要我只需要每个代码的前12位数字。

The Question is now how do i get the function to only receive the full line and not bitparts? 现在问题是如何让函数只接收完整行而不是bitparts?

This is the receiving part of the class class PortReader2 implements SerialPortEventListener { 这是类类的接收部分PortReader2实现了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);
            }
        }

    }
}

This is the sending part 这是发送部分

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

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. 要修复所述问题,您需要将收到的字符串连接到另一个字符串,该字符串累积从readString()函数接收的字符串片段,该函数位于serialEvent处理程序中。 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. 因为它是它的owm线程,它获得一定量的cpu时间来获取串行数据,因此它有效地获得串行端口输入数据的部分顺序读取。 So this example puts the partial inputs together to create the "whole" input. 因此,此示例将部分输入放在一起以创建“整体”输入。

String serialString;

So within the serialEvent handler: 所以在serialEvent处理程序中:

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

Either the string you get from readString() or the accumulation string can be scanned for tokens like eol condition. 您从readString()获得的字符串或累积字符串可以扫描像eol条件的标记。 For Example: 例如:

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

will break each line out to an element in the dlLines string array. 将每一行分解为dlLines字符串数组中的元素。

However I have found that if I have fixed length output from my target device this works better: 但是我发现如果我从目标设备获得固定长度输出,那么效果会更好:

serialPort.readString(int bytecount);

inline with the write serial string, eliminating the serialEvent handler. 内联写入串行字符串,消除了serialEvent处理程序。

This is a bit contrived but In other words: 这有点做作,但换句话说:

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

So the serialstrlen should obviously become constants for each expected line. 因此,serialstrlen显然应成为每个预期行的常量。

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

should get you the first line. 应该让你第一线。

Put the readString() in a loop, change serialstrelen argument value according to the expected strings. 将readString()放在循环中,根据预期的字符串更改serialstrelen参数值。 Check the strings for alternate content for error handling. 检查字符串是否有替代内容以进行错误处理。 This has worked for me. 这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM