'Java - Serial port data extraction

I'm fairly new to Java and programming in general. I am making an app in Java Swing that reads the data from serial port that is connected to a money counter machine. When the machine counts something, it sends count data through RS232-USB converter to the PC. I managed to make an app that finds and opens a COM port, and reads the data from the machine. This is the data that I get:

Machine output

Is it possible to extract only certain information from this output? For example only the unit count for D10? If so how can I do it?

Idea is to write all the info separately into already defined JTextfields.

I tried something like this:

while ((line = portReader.readLine()) != null) {
                //window.ta_logs.append(line + "\n");
                StringTokenizer st = new StringTokenizer(line);
                while (st.hasMoreElements()) {
                    String token = st.nextToken();
                    if (token.contains("D50")) {
                        data.put("D50", token.substring(2));
                    }
                    window.ta_logs.append(token);
                }
                System.out.println(line);
                System.out.println(data);

            }

Thanks in advance.



Solution 1:[1]

"Is it possible to extract only certain information from this output?"

Of course it is possible. You are already reading the data from the serial port. You can tokenize the data and store in a map.

// Get the data from serial port and tokenize it
Map<String, Whatever> data = new HashMap<>();
data.put("D10", XXX);
data.put("D20", YYY);
...

Then, you can set the text in the corresponding text box

d10TextField.setText(data.get("D10"));
d20TextField.setText(data.get("D20"));
...

All you need is make this map accessible.

Solution 2:[2]

while (input.available() > 0) {
                input = serialPort.getInputStream();
                int nBytes = input.available();
                int c = input.read();
                System.out.println("nBytes = " + nBytes);
                System.out.println("read " + (char) c);

                if (nBytes == 376) {
                    data.put("D10", (char) c);
                } else if (nBytes == 344) {
                    data.put("D20", (char) c);
                } else if (nBytes == 312) {
                    data.put("D50", (char) c);
                } else if (nBytes == 280) {
                    data.put("D100", (char) c);
                } else if (nBytes == 248) {
                    data.put("D200", (char) c);
                } else if (nBytes == 216) {
                    data.put("D500", (char) c);
                } else if (nBytes == 184) {
                    data.put("D1000", (char) c);
                } else if (nBytes == 152) {
                    data.put("D2000", (char) c);
                } else if (nBytes == 120) {
                    data.put("D5000", (char) c);
                }

I did it like this and got the wanted results.

My second question is how can I get the data if for example, D10 has more than 9pcs. In that situation, I have to take into consideration two bytes, or three if the number is 100 or more.

Any ideas?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hfontanez
Solution 2 Danilo Djurovic