Search code examples
javaspring-bootdockerhardwarecom-port

How to connect to com port by using spring boot & docker?


In my springboot project, I use com.fazecast.jSerialComm to scan serial port. It successfully to scan my hardware device in COM4. However, It fail to scan this com port in docker , even I add device /dev/ttyS3:/dev/ttyS3 in my docker compose file. Anyone have any clue on my problem. Thanks a lot.

OS: Window 11

SerialConfig.java

package com.XXXXXXXX.config;

import com.fazecast.jSerialComm.SerialPort;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SerialPortConfig {

    @Value("${xxxxxxxxx.node.serial.port:}")
    private String configuredPortName;

    @Bean
    public SerialPort serialPort() {
        SerialPort[] ports = SerialPort.getCommPorts();
        System.out.println("******************");
        System.out.println("Available Port:");
        for (SerialPort port : ports) {
            System.out.println("    Port "+port.getSystemPortName());
        }
        System.out.println("******************");
        if (ports.length == 0) {
            System.err.println("ERROR: No serial ports available. Workpass function may not be working");
            return null;  // Gracefully return null if no ports are available
        }
        else{
            System.out.print("Current Available port:'");
            for (SerialPort port : ports) {
                System.out.print(port.getSystemPortName()+",");
            }
            System.out.println("'");
        }


        SerialPort selectedPort = null;
        if (!configuredPortName.isEmpty()) {
            // Try to find the configured port by name
            for (SerialPort port : ports) {
                if (port.getSystemPortName().equalsIgnoreCase(configuredPortName)) {
                    selectedPort = port;
                    break;
                }
            }
            if (selectedPort == null) {
                System.err.println("ERROR: Configured serial port not found: " + configuredPortName);
                return null;  // Return null if the configured port is not found
            }
        } else {
            // Default to the first available port if no configured port is specified
            selectedPort = ports[0];
        }

        return selectedPort;
    }
}

docker-compose.yml

version: '3.8'

services:
  postgres_db:
    image: postgres:16.4
    container_name: xxxx_postgres_db
    environment:
      POSTGRES_USER: xxxx
      POSTGRES_PASSWORD: xxxx
      POSTGRES_DB: xxxxx-db
    ports:
      - "5431:5432"
    volumes:
      - xxxx_container_data:/var/lib/postgresql/data
    networks:
      - xxxx-network

  backend:
    build:
      context: .  # Path to the Dockerfile location
      dockerfile: Dockerfile  # Optional if the Dockerfile is named 'Dockerfile'
    image: xxxx-img
    container_name: xxxx_backend
    ports:
      - "8080:8080"
    networks:
      - xxxx-network
    depends_on:
      - postgres_db
    devices:
      - "/dev/ttyS0:/dev/ttyS0"
      - "/dev/ttyS1:/dev/ttyS1"
      - "/dev/ttyS2:/dev/ttyS2"
      - "/dev/ttyS3:/dev/ttyS3"

volumes:
  xxxx_data:

networks:
  xxxx-network:


Solution

  • I use usbipd-winfor sharing locally connected USB devices to other machines, including Hyper-V guests and WSL 2.After that my application can scan serial port in docker

    https://github.com/dorssel/usbipd-win