Link to home
Start Free TrialLog in
Avatar of Wilson Net
Wilson NetFlag for Argentina

asked on

How to validate data read from comm port using RXTX

I use the RXTX to read the input from a scale device, the code is the following:

                    // configuro el puerto
                    serialPort = (SerialPort) portIdentifier.open("ListPortClass", 1200);

                    int b = serialPort.getBaudRate();
                    serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                    InputStream mInputFromPort = serialPort.getInputStream();

                    // antes de empesar a leer le doy un tiempo apra que se llene el buffer
                    this.sleep(300);

                    // leo el peso
                    while(true){
                                        byte mBytesIn[] = new byte[2048];
                                        mInputFromPort.read(mBytesIn);

                                        String value = new String(mBytesIn);
                    }

                    // en caso de error cierro el puerto
                    serialPort.close();

Open in new window


the data format is: "000" + "000025" + "000000" + "\r\n" + ETX;

the problem i have is the string read from com port is not stable, sometimes is like "000025" + "000000" + "\r\n" + ETX;
And something is: "000000" + "\r\n" + ETX + "000" + "000025" + "000000" + "\r\n" + ETX;

how i can validate the input to avoid the misformat data and only read the correct format
Avatar of mccarl
mccarl
Flag of Australia image

It's not really "validation" that you require (at least not yet anyway), but more a case of data "framing". The way that you are reading the data will not frame it properly.

Now the easiest way that I can see to resolve this, even though it is 100% following the format you've given, would be to just read each "line" of text, i.e. all characters between the newlines. You say that the ETX comes after the newline, so doing it this way will move the previous ETX to the start of the next line, but for what you want, I don't think it will matter too much.

Try this to start with, anyway, and see if things improve...

                    // configuro el puerto
                    serialPort = (SerialPort) portIdentifier.open("ListPortClass", 1200);

                    int b = serialPort.getBaudRate();
                    serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);

                    InputStream mInputFromPort = serialPort.getInputStream();

                    // antes de empesar a leer le doy un tiempo apra que se llene el buffer
                    this.sleep(300);

                    BufferedReader br = new BufferedReader(mInputFromPort);

                    // leo el peso
                    while(true){
                                        String value = br.readLine();
                                        System.out.println(value);
                    }

                    // en caso de error cierro el puerto
                    serialPort.close();

Open in new window

how i can validate the input to avoid the misformat data and only read the correct format
That's a little difficult to know as you don't say what the format is. What is the real delimiter here: "\r\n", ETX, both together ..?
Avatar of Wilson Net

ASKER

the real delimiter is "\r\n" + ETX, i append to the question,

the solution proposed by mmcarl improve the result but now sometimes the first line isn't complete, for example i recieve "000\r\n", i think this may be caused by a comm port misconfig because i change the port speed to 1200 and start working better but i need to detect those line and skip it
In that case:
Scanner sc = new Scanner(serialPort.getInputStream());
sc.useDelimiter("\r\n" + ETX);
while (sc.hasNext()) {
	// Do it
}

Open in new window

might give you more manageable results
To help any further, you will need to give us more detail about the format. What you have given so far is just an example the format that you receive, not the format itself.

We could assume that from what you have given us that perhaps it always is a fixed length. If so, then you can validate a line received by checking the length of the String. But also, maybe it's not fixed length, so it really is hard to give you more help without knowing all this.
the problem is the data receive is fixed but it can recieve it incomplete because the enviroment where the scale is and other machines aside of the scale

doing read by line and checking the lenght is working
the problem is the data receive is fixed but it can recieve it incomplete
What do you mean by that exactly?
the data should by 000000025000000\r\nETX
but because the producction enviroment and other machines the could be recieve with noise and could by like 0000000250[]\r\n (this is a real example), because of this electrical problem the read may fail.
ASKER CERTIFIED SOLUTION
Avatar of mccarl
mccarl
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well in that case it could probably fail at any point so you don't want to be reading with any kind of delimiter. Owing to blocking, reading lines or any other kind of delimiter will make it block if input is incomplete. You need to read all you can and then make it into a string. You can always parse the delimiters out later.
I could solve it by reading everything I could and then parsing the line by the format, thank you very much
So you ended by not reading lines, but by reading everything and parsing later?