1. 程式人生 > 其它 >k8s docker java amd64 串列埠通訊

k8s docker java amd64 串列埠通訊

技術標籤:邊緣計算javalinuxcentosdocker

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