File: portlist.go

package info (click to toggle)
golang-github-bugst-go-serial 1.6.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 264 kB
  • sloc: makefile: 4
file content (43 lines) | stat: -rw-r--r-- 968 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//
// Copyright 2014-2023 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//

// portlist is a tool to list all the available serial ports.
// Just run it and it will produce an output like:
//
// $ go run portlist.go
// Port: /dev/cu.Bluetooth-Incoming-Port
// Port: /dev/cu.usbmodemFD121
//    USB ID     2341:8053
//    USB serial FB7B6060504B5952302E314AFF08191A
//
package main

import (
	"fmt"
	"log"

	"go.bug.st/serial/enumerator"
)

func main() {
	ports, err := enumerator.GetDetailedPortsList()
	if err != nil {
		log.Fatal(err)
	}
	if len(ports) == 0 {
		return
	}
	for _, port := range ports {
		fmt.Printf("Port: %s\n", port.Name)
		if port.Product != "" {
			fmt.Printf("   Product Name: %s\n", port.Product)
		}
		if port.IsUSB {
			fmt.Printf("   USB ID      : %s:%s\n", port.VID, port.PID)
			fmt.Printf("   USB serial  : %s\n", port.SerialNumber)
		}
	}
}