使用Java通过串行电缆从PCB板读取数据

时间:2019-02-13 16:10:23

标签: java rxtx

使用eclipse和java如何通过串行电缆从pcb板读取数据,因为pcb板的数据通过电缆以二进制格式到达。如何保存将在编译器中显示的数据? 有人帮我。

下面是我为此编写的代码,但是当我插入串行电缆时它无法检测到任何端口

import gnu.io.*;
import java.awt.Color;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.TooManyListenersException;


public class SimpleRead implements Runnable, SerialPortEventListener {
    static CommPortIdentifier portId;
    static Enumeration        portList;
    InputStream           inputStream;
    SerialPort            serialPort;
    Thread            readThread;


    public static void main(String[] args) {
    boolean           portFound = false;
    String            defaultPort ="COM 1" ;

    if (args.length > 0) {
        defaultPort = args[0];
    } 

    portList = CommPortIdentifier.getPortIdentifiers();

    while (portList.hasMoreElements()) {
        portId = (CommPortIdentifier) portList.nextElement();
        if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
        if (portId.getName().equals(defaultPort)) {
            System.out.println("Found port: "+defaultPort);
            portFound = true;
            SimpleRead reader = new SimpleRead();
        } 
        } 
    } 
    if (!portFound) {
        System.out.println("port " + defaultPort + " not found.");
    } 

    } 


    public SimpleRead() {
    try {
        serialPort = (SerialPort) portId.open("SimpleReadApp", 2000);
    } catch (PortInUseException e) {}

    try {
        inputStream = serialPort.getInputStream();
    } catch (IOException e) {}

    try {
        serialPort.addEventListener(this);
    } catch (TooManyListenersException e) {}

    serialPort.notifyOnDataAvailable(true);

    try {
        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, 
                       SerialPort.STOPBITS_1, 
                       SerialPort.PARITY_NONE);
    } catch (UnsupportedCommOperationException e) {}

    readThread = new Thread(this);

    readThread.start();
    }


    public void run() {
    try {
        Thread.sleep(20000);
    } catch (InterruptedException e) {}
    } 


    public void serialEvent(SerialPortEvent event) {
    switch (event.getEventType()) {

    case SerialPortEvent.BI:

    case SerialPortEvent.OE:

    case SerialPortEvent.FE:

    case SerialPortEvent.PE:

    case SerialPortEvent.CD:

    case SerialPortEvent.CTS:

    case SerialPortEvent.DSR:

    case SerialPortEvent.RI:

    case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
        break;

    case SerialPortEvent.DATA_AVAILABLE:
        byte[] readBuffer = new byte[20];

        try {
        while (inputStream.available() > 0) 
        {
            int numBytes = inputStream.read(readBuffer);
            System.out.print("The Read Bytes from SerialPort are");
            System.out.write(readBuffer);
            System.out.println();
        } 

        System.out.print(new String(readBuffer));
        } catch (IOException e) {}

        break;
    }
    } 

}

日志结果:

未找到端口COM 1。

1 个答案:

答案 0 :(得分:1)

端口名称中没有空格。尝试更改:

String            defaultPort ="COM 1" ;

收件人:

String            defaultPort ="COM1" ;