通过带有java的串行电缆从PCB板读取数据

问题描述 投票:2回答:1

使用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未找到。

java rxtx
1个回答
1
投票

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

String            defaultPort ="COM 1" ;

至:

String            defaultPort ="COM1" ;
© www.soinside.com 2019 - 2024. All rights reserved.