赞
踩
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程序接收什么。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。