Question

I have a scale that is connected to mi PC to and RS 232 and with an usb to RS 232 converter (ATEN USB to Serial Bridge If anyone knows).

I only want to get the wheight in one moment from the scale, but I need to send an ASCII 'W'.

When the host requests weight data by sending an uppercase W, the scale will respond with the weight data or a status byte if the scale is in motion or an invalid state.

From scale's manual.

So I did that code:

import gnu.io.*;
import java.io.*;
import java.util.Enumeration;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;


public class PruebaMia implements SerialPortEventListener{

    static CommPortIdentifier portId;
    static Enumeration portList;

    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;

    String output = "W/r/t";


public static void main(String[] args) throws IOException, UnsupportedCommOperationException {
    PruebaMia pm = new PruebaMia();
    pm.FlashWriteMethod();
}

public void FlashWriteMethod() throws IOException, UnsupportedCommOperationException {
    portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM4")) {
                try {
                    serialPort = (SerialPort) portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                serialPort.setSerialPortParams(9600, SerialPort.DATABITS_7, SerialPort.STOPBITS_1, SerialPort.PARITY_EVEN);

                BufferedReader is = null;  // for demo purposes only. A stream would be more typical.
                PrintStream    os = null;
                try {
            serialPort.addEventListener(this);
            } catch (TooManyListenersException e) {
                System.out.println("Tooo many Listener exception");}

                try {
                    is = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
                    } catch (IOException e) {
                    System.err.println("Can't open input stream: write-only");
                    is = null;
                }

                os = new PrintStream(serialPort.getOutputStream(), true);

                os.print("W");
                os.print("\r\n");


                // Read the response
                String response = is.readLine();
                OutputStream mOutputToPort = serialPort.getOutputStream();
                inputStream = serialPort.getInputStream();
                System.out.println(" Input Stream... " + inputStream);

                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(mOutputToPort));
                bw.write(output);
                bw.flush();

                inputStream = serialPort.getInputStream();
                System.out.println(" Input Stream... " + inputStream);
                }
             }
        }
}

 public void serialEvent(SerialPortEvent event){
        switch(event.getEventType()) {
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                outputBufferEmpty(event);
                break;

            case SerialPortEvent.DATA_AVAILABLE:
                dataAvailable(event);
                break;
        }
    }

    protected void outputBufferEmpty(SerialPortEvent event) {
    }

    protected void dataAvailable(SerialPortEvent event) {
        System.out.println("Data available event received");
//        try{
//            while (inputStream.available() > 0){
//                int numBytes = inputStream.read(readBuffer);}
//            
//        vBuffer += new String(readBuffer);
//        System.out.print(new String(readBuffer));
//        
//        }catch (IOException e){
//            System.out.println(e);}
//    }
    }

}

I think im not sending well the 'W' to the scale, so it throws an exception:

Exception in thread "main" java.io.IOException: Underlying input stream returned zero bytes

What I'm doing bad?

Edit:

My scale is an OHAUS RV series.

Was it helpful?

Solution

David, that scale have 4 types of protocol (Cause is weight only mode(WO)).

I recommend you to use Elpsa for WO mode. From the manual:

The spanish competitor Epelsa has developed a protocol for the communication between checkout scales [ only-weight scales ] and POS [ or PC ] which has become kind of standard in the spanish market for this type of connections, thereof our interest that our only-weight scales become compatible.

You may use 9600 Baud, 8 databits, stop bits 1, partity even. You can change this, but with default scale configuration those are the best. (If you want to use others, you should change the scale configuration)

To use this protocol use this box:

Scale box

So to receive the weight you should send a '$' char

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top