java 如何从串口分割输入流?

utugiqy6  于 2023-01-04  发布在  Java
关注(0)|答案(1)|浏览(131)

我正在使用InputStream从串行端口阅读数据。
流的一部分需要转换为String,另一部分需要转换为二进制数据。
我已经有了两个方法,每个方法都执行所需的操作,但是我需要从串行端口发送两次数据,两个方法才能工作
我的问题是,我是否可以以某种方式分割流,这样我就可以在一次从串行端口发送中将所有数据发送到两个方法?

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);

            }
        }
    });

}
9w11ddsr

9w11ddsr1#

我使用这段代码重复了输入流。

InputStream bufferdInputStream = new BufferedInputStream(myInputStream);

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

相关问题