好吧,即时通讯迷路了,甚至不知道谷歌在这一点上。

我正在使用JSSC从串行端口读取数据。

当我将其打印为校验图时,数据被顶起。
它可以打印几行,然后按单个字符打印。
如果我使用“ printf”,它将正常打印

$GPRMC,212729.000,A,3805.8438,N,08459.5498,W,0.00,280.71,031117,,,D*73
$GPVTG,280.71,T,,M,0.00,N,0.0,K0
F



$
G
P
G


这是for循环即时通讯使用:

  for (String s: data2) {

                System.out.println(s);

                 }


其他代码:

String getData =  serialPort.readString(event.getEventValue());

List<String> data2 = new ArrayList<String>(Arrays.asList(getData.split("$")));


如果我只是打印它,我会选择printf。

我需要用$符号将每个句子分开。

任何帮助,将不胜感激。

    import jssc.SerialPort;
    import jssc.SerialPortEvent;
    import jssc.SerialPortEventListener; import jssc.SerialPortException;
    import java.util.Arrays;
    import java.util.ArrayList;
    import java.util.List;


    public class test {

    static SerialPort serialPort;

    public static void main(String[] args) {
        serialPort = new SerialPort("COM!");
        try {
            serialPort.openPort();//Open ports
            serialPort.setParams(4800, 8, 1, 0);//Set params
          int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
           serialPort.setEventsMask(mask);//Set mask
           serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener


        }
        catch (SerialPortException ex) {
            System.out.println(ex);
        }

}public void serialEvent(SerialPortEvent event) {
          //  if(event.isRXCHAR()){//If data is available
           //    if(event.getInputBufferBytesCount()  > 1){//Check bytes count in the input buffer
                    //Read data, if 10 bytes available
                    try {


                          String getdata =  serialPort.readString(event.getEventValue());

                          String delimiter = "$";

                      List<String> data2 = new ArrayList<String>(Arrays.asList(getdata.split("$")));


                   for (String s: data2) {

                    System.out.printf("NEW" + s);

                     }
                    }
                    catch (SerialPortException ex) {

                    }


每次请求更新的代码

    import jssc.SerialPort;
import jssc.SerialPortEvent;
import jssc.SerialPortEventListener; import jssc.SerialPortException;
//import java.util.Arrays;
//import java.awt.List;
//import java.util.Base64;
//import java.io.BufferedReader;
//import java.io.ByteArrayInputStream;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.io.IOException;
import java.lang.*;
//import static java.util.Arrays.asList;
//import java.util.List;
//import java.util.stream.Collectors;
//import org.apache.commons.lang3.StringUtils;


public class test {
static ArrayList<String> data = new ArrayList<String>();
static SerialPort serialPort;

public static void main(String[] args) {
    serialPort = new SerialPort("COM1");
    try {
        serialPort.openPort();//Open ports
        serialPort.setParams(4800, 8, 1, 0);//Set params
      int mask = SerialPort.MASK_RXCHAR + SerialPort.MASK_CTS + SerialPort.MASK_DSR;//Prepare mask
       serialPort.setEventsMask(mask);//Set mask
       serialPort.addEventListener(new SerialPortReader());//Add SerialPortEventListener


    }
    catch (SerialPortException ex) {
        System.out.println(ex);
    }
}

/*
 * In this class must implement the method serialEvent, through it we learn about
 * events that happened to our port. But we will not report on all events but only
 * those that we put in the mask. In this case the arrival of the data and change the
 * status lines CTS and DSR
 */

static class SerialPortReader implements SerialPortEventListener {

    public void serialEvent(SerialPortEvent event) {
    //   if(event.isRXCHAR()){//If data is available
      //     if(event.getEventValue() < 577){//Check bytes count in the input buffer
                //Read data, if 10 bytes available

                try {
                    String getdata =  serialPort.readString(event.getEventValue());
                String[] parts= getdata.split("$");

                data.set(data.size() - 1, data.get(data.size() - 1) + parts[0]);
                for (int i=1; i<parts.length; i++) {
                    data.add(parts[i]);
                }

                }
                catch (SerialPortException ex) {
             }

       }
}
}

最佳答案

您的serialEvent方法只是为每个EventListener触发的SerialPortEvent,但是不能保证数据将“完整”到达,因此您应在类级别声明List,获取到达的数据,然后以适当的方式存储它。就像是:

public class test {
    // ...
    static List<String> data = new ArrayList<String>();
    // ...
    public void serialEvent(SerialPortEvent event) {
        try {
            String getdata =  serialPort.readString(event.getEventValue());
            // String.split takes a regular expression. In regular expressions,
            // the dollar sign ($) by it's own means the end of the line, so you
            // have to escape it using a backslash (\), but since it's a string
            // you have to escape that one with another backslash so it get's
            // passed correctly, thus searching for the dollar sign and not the
            // end of the line
            String[] parts = getdata.split("\\$");
            // Append whatever is before the first dollar sign to the last item
            // in your data
            if(!data.isEmpty()){
                data.set(data.size() - 1, data.get(data.size() - 1) + parts[0]);
            }
            // Append the rest of the parts in your data
            for (int i=1; i<parts.length; i++) {
                data.add(parts[i]);
            }
        }
        catch (SerialPortException ex) {}
        // ...
    }
    // ...
}

关于java - 使用串行端口阵列列表中的数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47104724/

10-11 03:47
查看更多