Java RXTX

根据不同操作系统下载RXTXcomm包,根据install.txt拷贝相应文件到jdk目录

官网地址:http://fizzed.com/oss/rxtx-for-java

下载的2.2版本在amd64会报以下异常

#
# A fatal error has been detected by the Java Runtime Environment:
#
# SIGSEGV (0xb) at pc=0x00007fdf1d2d15da, pid=1, tid=0x00007fdf1d6d8700
#
# JRE version: Java(TM) SE Runtime Environment (8.0_212-b10) (build 1.8.0_212-b10)
# Java VM: Java HotSpot(TM) 64-Bit Server VM (25.212-b10 mixed mode linux-amd64 compressed oops)
# Problematic frame:
# C [librxtxSerial.so+0x75da] Java_gnu_io_RXTXPort_nativeDrain+0xea
#
# Core dump written. Default location: //core or core.1
#
# An error report file with more information is saved as:
# //hs_err_pid1.log
#
# If you would like to submit a bug report, please visit:
# http://bugreport.java.com/bugreport/crash.jsp
# The crash happened outside the Java Virtual Machine in native code.
# See problematic frame for where to report the bug.
#
[error occurred during error reporting , id 0xb]

这里主要是版本兼容性问题,下载以下包替换就行:

链接:https://pan.baidu.com/s/1CgOdQ3hkmK3sjLSGirGMuA 
提取码:vgal 

 

Modbus RTU开发

        SerialPort result = null;
        try {
            // 通过端口名识别端口
            CommPortIdentifier identifier = CommPortIdentifier.getPortIdentifier(portName);
            // 打开端口,并给端口名字和一个timeout(打开操作的超时时间)
            CommPort commPort = identifier.open(portName, 2000);
            // 判断是不是串口
            if (commPort instanceof SerialPort) {
                result = (SerialPort) commPort;
                // 设置一下串口的波特率等参数
                result.setSerialPortParams(baudRate, dataBits, stopBits, parity);
                log.info("打开串口{}成功", portName);
            }else{
                log.info("{}不是串口", portName);
            }
        } catch (Exception e) {
            log.error("打开串口{}错误", portName, e);
        }



        SerialPortWrapperImpl wrapper = new SerialPortWrapperImpl("COM2", 9600,
                SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        ModbusFactory modbusFactory = new ModbusFactory();
        ModbusMaster master = modbusFactory.createRtuMaster(wrapper);
        master.init();

docker

docker中串口挂载

#不安全
docker run -d --privileged -v /dev/ttyS0:/dev/ttyS0 nginx 
#或
docker run -d --device=/dev/ttyS0 nginx

k8s

k8s中没有找到更好的办法。目前采用:

securityContext:
   privileged: true
volumeMounts:
   - name: tty0
       mountPath: /dev
volumes:
   - name: tty0
       hostPath:
       path: /dev

 

 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐