RK3568 JDK RXTX-JAVA串口485开发

RK3568 是2.0GHz主频A55 64位的系统,它优异的性能,很适合在边缘网关中JAVA的开发,那么无论是RS232串口传感器还是485接口的设备,或者是LORA等无线设备都要用到JAVA 的RXTX进行操作。

 

但是RXTX的so库,是C库,依赖于交叉编译工具,64位ARM ,32位ARM,所以经常出现不兼容的问题:

huiwei@hwserver:rxtx$ ls rxtx_rk3568_target/rxtx_ok -l
total 324
-rwxrwxr-x 1 huiwei huiwei  58416 abr  3 15:14 librxtxParallel-2.2pre1.so
lrwxrwxrwx 1 huiwei huiwei     26 abr  3 15:14 librxtxParallel.so -> librxtxParallel-2.2pre1.so
-rwxrwxr-x 1 huiwei huiwei 208712 abr  3 15:14 librxtxSerial-2.2pre1.so
lrwxrwxrwx 1 huiwei huiwei     24 abr  3 15:14 librxtxSerial.so -> librxtxSerial-2.2pre1.so
-rw-rw-r-- 1 huiwei huiwei  60984 abr  3 15:32 RXTXcomm.jar
huiwei@hwserver:rxtx$

辉为为我们产品提供JDK,jdk-8u361-linux-aarch64.tar,解决诸多JDK面向硬件的问题。下面测试JAVA 的RXTX,代码如下:

import java.io.IOException;
import java.io.OutputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
 
public class TestRxtx {
    public static void displayCustomerScreen(String data, byte[] mode) {
        try {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/ttyS0");
            SerialPort serialPort = (SerialPort) portIdentifier.open("test",5000);
            serialPort.setSerialPortParams(115200,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);         
            try {
                OutputStream outputStream = serialPort.getOutputStream();
                if (mode != null) {
                    outputStream.write(mode);
                }
                if (data != null) {
                    outputStream.write(data.getBytes());
                }
                outputStream.flush();
                outputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                serialPort.close();
            }
        } catch (NoSuchPortException e) {
            e.printStackTrace();
        } catch (PortInUseException e) {
            e.printStackTrace();
        } 
        catch (UnsupportedCommOperationException e) {
            e.printStackTrace();
        }
​
    }
​
    public static void main(String[] args) {
        displayCustomerScreen(null,"www.huiweit.com".getBytes());
    }
}

编译代码:

root@RK356X:~# ls
TestRxtx.class  TestRxtx.java
root@RK356X:~# javac TestRxtx.java
root@RK356X:~#

PC机上打开串口助手,波特率和JAVA程序一直为115200,检测串口接收的数据,运行JAVA程序:

root@RK356X:~# java TestRxtx
root@RK356X:~#

可以看到串口助手接收到JAVA发过来的信息:

 

 

JAVA接收代码:

import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import gnu.io.CommPortIdentifier;
import gnu.io.NoSuchPortException;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;
import java.util.Arrays;
​
public class TestRx {
    public static void readData() {
        try {
            CommPortIdentifier portIdentifier = CommPortIdentifier.getPortIdentifier("/dev/ttyS7");
            SerialPort serialPort = (SerialPort) portIdentifier.open("test",5000);
            serialPort.setSerialPortParams(115200,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);
            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);         
            try {
                InputStream inputStream = serialPort.getInputStream();
    
                byte[] bytes = null;
                while (true) {
                    int len = inputStream.available();
                    if (len > 0) {
                        bytes = new byte[len];
                        inputStream.read(bytes);
                        System.out.println(Arrays.toString(bytes));
                    }
                    else {
                        //System.out.println("read ...");
                    }
                }
​
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                serialPort.close();
            }
        } catch (NoSuchPortException e) {
            e.printStackTrace();
        } catch (PortInUseException e) {
            e.printStackTrace();
        } 
        catch (UnsupportedCommOperationException e) {
            e.printStackTrace();
        }
​
    }   
    
    
    public static void main(String[] args) {
        readData();
        
    }
}
​

同样,串口助手发送什么,JAVA程序接收什么。

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值