簡體   English   中英

如何從串口拆分輸入流?

[英]How to split inputStream from Serial port?

我正在使用 InputStream 從串口讀取數據。

我需要將 stream 的一部分轉換為字符串,另一部分轉換為二進制數據。

我已經有兩種方法,每一種都做了需要的,但是我需要從串口發送兩次數據才能使這兩種方法都起作用

我的問題是我是否可以以某種方式拆分 stream 以便我可以將所有數據一次從串行端口發送到兩種方法?

private static void readingBytesSN(SerialPort comPort) {

    comPort.addDataListener(new SerialPortDataListener() {
        @Override
        public int getListeningEvents() {
            return SerialPort.LISTENING_EVENT_DATA_AVAILABLE;
        }

        @Override
        public void serialEvent(SerialPortEvent serialPortEvent) {

            InputStream in;

            String startSn2 = "110000010011010001101100100011011000100011010000111000010001010";
            String newLine2 = "01100000110110010001101100010001101000011100001000101";

            String startSn = "27434877273598525669";
            String newLine = "12273598525669";
            String endOfSn = "12694954545053565052661310";
            String endOfData = "1227513232131032131032131032131032131027109275132";


            String s1 = "";
            String s2 = "";

            if (serialPortEvent.getEventType() != SerialPort.LISTENING_EVENT_DATA_AVAILABLE) {
                return;
            }

            int x = 0;
            try {

                in = comPort.getInputStream();

                Scanner sc = new Scanner(in);

                List<String> line = new ArrayList<>();

                while (sc.hasNextLine()) {
                    line.add(sc.next());
                    if (line.contains("\u001Bm\u001B3")) {
                        break;
                    }
                }

                while (((x = in.read()) != 109)) {
                    s1 += String.format("%8s", Integer.toBinaryString(x & 0xFF)).replace(' ', '0');
                }
            } catch (IOException e1) {
                e1.printStackTrace();
            }

            String[] snArray = s1.split(startSn2);
            

            for (int i = 1; i < snArray.length; i++) {

                BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_BYTE_BINARY);
                Graphics2D g2d = img.createGraphics();
                Font font = new Font("Arial", Font.PLAIN, 2);
                g2d.setFont(font);
                int height = g2d.getFontMetrics().getHeight();
                g2d.dispose();

                img = new BufferedImage(384, 40, BufferedImage.TYPE_INT_RGB);
                g2d = img.createGraphics();

                g2d.setFont(font);
                g2d.setColor(Color.WHITE);
                int fontSize = 1;

                for (String line : snArray[i].split(newLine2)) {

                    g2d.drawString(line, 0, height);
                    height += fontSize;
                    //System.out.println("Serial number: " + line);
                }
                //g2d.dispose();
                try {
                    ImageIO.write(img, "png", new File("images\\Text" + i + ".png"));
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                g2d.dispose();

                String result = null;
                try {
                    result = SerialOcr("images\\Text" + i + ".png");
                } catch (TesseractException e) {
                    e.printStackTrace();
                }
                System.out.println(result);

            }
        }
    });

}

我使用此代碼重復輸入 stream。

InputStream bufferdInputStream = new BufferedInputStream(myInputStream);

bufferdInputStream.mark(some_value);
//do the first method
bufferdInputStream.reset();
//do the second method

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM