编程语言
首页 > 编程语言> > 飞凌FCU1104嵌入式控制单元JAVA读取串口

飞凌FCU1104嵌入式控制单元JAVA读取串口

作者:互联网

系统为Ubuntu

尝试用RXTX-JAVA读取,发现ARM32系统需要编译驱动。

于是尝试用google的JSSC类库,能读取数据

//读不出扩展版 串口
String[] ttys = SerialPortList.getPortNames();
System.out.println(Arrays.toString(ttys));
//指定串口设备读取
String[] baseTTy = new String[]{"/dev/ttymxc1","/dev/ttymxc2","/dev/ttymxc3","/dev/ttymxc4","/dev/ttyXRUSB0","/dev/ttyXRUSB1","/dev/ttyXRUSB2","/dev/ttyXRUSB3"};
for (String tty : baseTTy) {
    //开启线程
    new Thread(new Runnable() {
        @Override
        public void run() {
            //设置串口
            SerialPort serialPort = new SerialPort(tty);
            try {
                System.out.println("open " + tty);
                //Open port
                serialPort.openPort();
                //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
                serialPort.setParams(9600,8,1,0);
                while (true){
                    try {
                        byte[] readData = serialPort.readBytes();
                        System.out.println(tty + " Rec: " + HexUtil.encodeHexStr(readData).toUpperCase());
                        // 往串口写入数据
                        // serialPort.writeBytes();
                    }catch (Exception e){
                        try {
                            Thread.sleep(20);
                        } catch (InterruptedException ex) {
                            ex.printStackTrace();
                        }
                        continue;
                    }

                }
            } catch (SerialPortException e) {
                e.printStackTrace();
                try {
                    serialPort.closePort();
                } catch (SerialPortException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
    ).start();

maven配置

<dependency>
    <groupId>io.github.java-native</groupId>
    <artifactId>jssc</artifactId>
    <version>2.9.2</version>
</dependency>

程序运行效果图

 

标签:tty,JAVA,dev,serialPort,try,飞凌,串口,new
来源: https://blog.csdn.net/sinat_29802159/article/details/121885816