【问题标题】:READ ASCII data using Javax comm package使用 Javax 通信包读取 ASCII 数据
【发布时间】:2021-12-31 15:52:51
【问题描述】:

我有一个设备向 COM 端口发送一些数据,字节流是人类可读的 ASCII 字符流。 我尝试从端口读取器读取数据流,我可以轻松读取它,

Param   Flags   Value   Unit    [min-max]

WBC E 0.00 10^9/l [4.00-10.00] LYM 0.00 10^9/l [1.30-4.00]

但是当我尝试从 java 程序中读取字节并转换为 ASCII 时,它仍然显示为行话。例如?????

我更改了数组大小并尝试了各种将数组转换为 ASCII 的选项。

出去就像

4C 4C 59 20 41 55 54 4F 4D 41 54 45 44 20 48 45 LLY 自动 HE

非常感谢阅读正确格式的指导。

代码是

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleRead implements Runnable, SerialPortEventListener {

    static CommPortIdentifier portId;
    static Enumeration portList;
    InputStream inputStream;
    SerialPort serialPort;
    Thread readThread;
    String line = "";
    BufferedReader portReader;

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

        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) {
            e.printStackTrace();
        }

        try {

            inputStream = serialPort.getInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }

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

        serialPort.notifyOnDataAvailable(true);

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

        readThread = new Thread(this);

        readThread.start();
    }

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

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

            case SerialPortEvent.BI:
                System.out.println("BI");
            case SerialPortEvent.OE:
                System.out.println("OE");
            case SerialPortEvent.FE:
                System.out.println("FE");
            case SerialPortEvent.PE:
                System.out.println("PE");
            case SerialPortEvent.CD:
                System.out.println("CD");
            case SerialPortEvent.CTS:
                System.out.println("CTS");
            case SerialPortEvent.DSR:
                System.out.println("DSR");
            case SerialPortEvent.RI:
                System.out.println("RI");
            case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
                System.out.println("OUTPUT_BUFFER_EMPTY");
                break;

            case SerialPortEvent.DATA_AVAILABLE:
                System.out.println("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) {
                    e.printStackTrace();
                }

                break;
        }
    }

}

【问题讨论】:

    标签: java serial-port port device javax.comm


    【解决方案1】:

    在您的输出中,您没有指定要使用的字符集。由于您是通过串行端口接收数据,我假设使用的字符集是 US_ASCIIUTF_8

    要强制从 byte[] 转换为字符串以使用特定字符集,您必须指定它。

    您的代码示例如下:

    System.out.print(new String(readBuffer, StandardCharsets.UTF_8));
    

    可以在here找到Java中的StandardCharsets列表

    【讨论】:

      猜你喜欢
      • 2011-06-26
      • 1970-01-01
      • 2022-10-05
      • 2012-10-05
      • 2020-02-04
      • 2023-04-02
      • 1970-01-01
      • 2019-09-17
      • 1970-01-01
      相关资源
      最近更新 更多