Search code examples
javaserial-portinputstream

How to split inputStream from Serial port?


I'm reading the data from a serial port using InputStream.

One part of the stream I need to convert to String, and the other one to binary data.

I already have two methods, and each one does what is needed, but I need to send the data from the serial port twice for both methods to work

My question is if I can somehow split the stream so I can get all of the data to two methods in one send from the serial port?

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

            }
        }
    });

}

Solution

  • I repeated the input stream using this code.

    InputStream bufferdInputStream = new BufferedInputStream(myInputStream);
    
    bufferdInputStream.mark(some_value);
    //do the first method
    bufferdInputStream.reset();
    //do the second method