Usman Riaz
Usman Riaz

Reputation: 3020

Reading data from serial port in java

I want to read data (which is weight ) from a weigh bridge which is connected to my computer with com1 port. I search the web and i am able to connect to the port and read data but the problem is that the data is not meaning full.

I am getting somewhat like below

enter image description here

There is a weight of 145 is on the weigh bridge but i am getting these symbols. Here is the code with which i am reading the data.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package readingports;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;

/**
 *
 * @author IamUsman
 */
public class ReadingPorts implements SerialPortEventListener, Runnable {

    static CommPortIdentifier portId;
    static Enumeration portList;
    static SerialPort port;
    static InputStream inputStream;
    static Thread readThread;
    static byte buffer[];
    static BufferedReader br;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM1")) {
                    if (!portId.isCurrentlyOwned()) {
                        ReadingPorts rp = new ReadingPorts();
                    } else {
                        System.out.println("This port is already used by some other program");
                    }

                }
            }
        }
    }

    public ReadingPorts() {
        try {
            port = (SerialPort) portId.open("Custom", 500);
            inputStream = port.getInputStream();
            br = new BufferedReader(new InputStreamReader(inputStream));
            System.out.println("** Connected To COM6 **");
            port.addEventListener(this);
            port.notifyOnDataAvailable(true);
            port.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
            port.enableReceiveTimeout(500);
            System.out.println("................................");
            readThread = new Thread(this);
            readThread.start();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public void serialEvent(SerialPortEvent event) {

        switch (event.getEventType()) {

            case SerialPortEvent.DATA_AVAILABLE:
                buffer = new byte[8];
                try {
                    while (inputStream.available() > 0) {
                        int numBytes = inputStream.read(buffer);
                        System.out.println(new String(buffer,0,numBytes));
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                }

                break;

        }




    }

    @Override
    public void run() {
        try {
            System.out.println("In Run");
            Thread.sleep(2000);
        } catch (Exception ex) {
            ex.printStackTrace();;
        }

    }
}

Hyper terminal reads correct data.

enter image description here

Upvotes: 1

Views: 10638

Answers (1)

zzas11
zzas11

Reputation: 1115

After understand your code, maybe you can modify your code to this:

case SerialPortEvent.DATA_AVAILABLE:
            try {
                 String inputLine=br.readLine();
                 System.out.println(inputLine);
            } catch (Exception ex) {
                System.err.println(e.toString());
            }

            break;

and move this 2 line to below port.setSerialPortParams:

        inputStream = port.getInputStream();
        br = new BufferedReader(new InputStreamReader(inputStream));

Upvotes: 2

Related Questions