在称重桥终端中读取Java中的串口

时间:2017-09-27 05:21:45

标签: java serial-port

import java.io.*;
import java.util.Enumeration;
import java.nio.*;

import javax.comm.CommPortIdentifier;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;

import com.sap.conn.jco.*;

public class ReadingPorts implements SerialPortEventListener,Runnable {
    static CommPortIdentifier portId;
    static Enumeration portList;
    static SerialPort port;
    static InputStream inputStream;
    static Thread readThread;
    static byte readbuffer[];
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                if (portId.getName().equals("COM3")) {
                    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();
            System.out.println("** Connected To COM3 **");
            port.addEventListener(this);
            port.notifyOnDataAvailable(true);
            port.setSerialPortParams(2400, 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:
            readbuffer = new byte[500];

                try {
                    while (inputStream.available() > 0) {
                        int numBytes = inputStream.read(readbuffer);
                        String result = new String(readbuffer,0,numBytes);
                    }

                    ByteBuffer buffer = ByteBuffer.wrap(readbuffer); //wrap received byte buffer
                    buffer.order(ByteOrder.LITTLE_ENDIAN);//set correct endiannes
                    int version = buffer.getInt();// read first 4 bytes
                    int length = buffer.getInt(); // read next 4 bytes
                    byte[] rowMsg = new byte[length];// length = bytes to read
                    buffer.get(rowMsg); // copies 'length' bytes in rowMsg
                    String msg = new String(rowMsg); // convert to String
                    System.out.println("Version "+version+" message received");
                    System.out.println("Length: "+length);
                    System.out.println("text: "+msg);
                } catch (Exception ex) {
                    ex.printStackTrace();
                } 
       }
    }
    public void run() {
        try {
            System.out.println("In Run");
            Thread.sleep(2000);
        } catch (Exception ex) {
            ex.printStackTrace();;
        }

    }
}

我处在从COM端口分配读取输入并分配给Byte Buffer类的地方。我的输出总是为0,尽管称重桥上有一些重量。

以下行可以使用一些不需要的ascii代码打印重量: String result = new String(readbuffer,0,numBytes);

我正在使用Byte Buffer类将此ascii值转换为有用的输出。但是这只输出0作为输出。

0 个答案:

没有答案
相关问题