skip to Main Content

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:

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. According to the following open issue, it is not possible as of now for windows.

    Thanks for opening this issue for tracking this feature request. We’ve had
    a few similar requests for USB device support. Currently this is not a
    design use-case for Docker for Windows.

    You might be able to achieve what you want by setting up a separate
    hyper-v (or virtualbox) VM and installing Docker in that. That would
    give you more freedom to use hyper-v features like attaching special
    host-hardware and using it with Docker Linux containers.

    You can even use Docker Machine to automate part of this process:
    https://docs.docker.com/machine/drivers/hyper-v/

    https://github.com/docker/for-win/issues/1018

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search