简体   繁体   English

java从串口读取数据

[英]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.我想从通过com1端口连接到我的计算机的com1读取数据(即重量)。 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我有点像下面

在此处输入图片说明

There is a weight of 145 is on the weigh bridge but i am getting these symbols.地磅上有一个 145 的重量,但我得到了这些符号。 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.超级终端读取正确数据。

在此处输入图片说明

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 :并将这 2 行移动到port.setSerialPortParams下方:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM