@@ -19,9 +19,9 @@ import (
1919 "bytes"
2020 "context"
2121 "errors"
22- "fmt"
2322 "io"
2423 "os"
24+ "slices"
2525 "sort"
2626 "strings"
2727 "time"
@@ -34,6 +34,7 @@ import (
3434 "github.com/arduino/arduino-cli/internal/cli/instance"
3535 "github.com/arduino/arduino-cli/internal/i18n"
3636 rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
37+ "github.com/arduino/go-properties-orderedmap"
3738 "github.com/fatih/color"
3839 "github.com/sirupsen/logrus"
3940 "github.com/spf13/cobra"
@@ -58,6 +59,8 @@ func NewCommand(srv rpc.ArduinoCoreServiceServer) *cobra.Command {
5859 Long : i18n .Tr ("Open a communication port with a board." ),
5960 Example : "" +
6061 " " + os .Args [0 ] + " monitor -p /dev/ttyACM0\n " +
62+ " " + os .Args [0 ] + " monitor -p /dev/ttyACM0 -b arduino:avr:uno\n " +
63+ " " + os .Args [0 ] + " monitor -p /dev/ttyACM0 --config 115200\n " +
6164 " " + os .Args [0 ] + " monitor -p /dev/ttyACM0 --describe" ,
6265 Run : func (cmd * cobra.Command , args []string ) {
6366 sketchPath := ""
@@ -89,13 +92,6 @@ func runMonitorCmd(
8992 quiet = true
9093 }
9194
92- var (
93- inst * rpc.Instance
94- profile * rpc.SketchProfile
95- fqbn string
96- defaultPort , defaultProtocol string
97- )
98-
9995 // Flags takes maximum precedence over sketch.yaml
10096 // If {--port --fqbn --profile} are set we ignore the profile.
10197 // If both {--port --profile} are set we read the fqbn in the following order: profile -> default_fqbn -> discovery
@@ -110,9 +106,9 @@ func runMonitorCmd(
110106 )
111107 }
112108 sketch := resp .GetSketch ()
113- if sketch != nil {
114- defaultPort , defaultProtocol = sketch . GetDefaultPort (), sketch . GetDefaultProtocol ()
115- }
109+
110+ var inst * rpc. Instance
111+ var profile * rpc. SketchProfile
116112 if fqbnArg .String () == "" {
117113 if profileArg .Get () == "" {
118114 inst , profile = instance .CreateAndInitWithProfile (ctx , srv , sketch .GetDefaultProfile ().GetName (), sketchPath )
@@ -123,11 +119,13 @@ func runMonitorCmd(
123119 if inst == nil {
124120 inst = instance .CreateAndInit (ctx , srv )
125121 }
122+
126123 // Priority on how to retrieve the fqbn
127124 // 1. from flag
128125 // 2. from profile
129126 // 3. from default_fqbn specified in the sketch.yaml
130127 // 4. try to detect from the port
128+ var fqbn string
131129 switch {
132130 case fqbnArg .String () != "" :
133131 fqbn = fqbnArg .String ()
@@ -136,15 +134,19 @@ func runMonitorCmd(
136134 case sketch .GetDefaultFqbn () != "" :
137135 fqbn = sketch .GetDefaultFqbn ()
138136 default :
139- fqbn , _ = portArgs .DetectFQBN (ctx , inst , srv )
137+ fqbn , _ , _ = portArgs .DetectFQBN (ctx , inst , srv )
140138 }
141139
140+ var defaultPort , defaultProtocol string
141+ if sketch != nil {
142+ defaultPort , defaultProtocol = sketch .GetDefaultPort (), sketch .GetDefaultProtocol ()
143+ }
142144 portAddress , portProtocol , err := portArgs .GetPortAddressAndProtocol (ctx , inst , srv , defaultPort , defaultProtocol )
143145 if err != nil {
144146 feedback .FatalError (err , feedback .ErrGeneric )
145147 }
146148
147- enumerateResp , err := srv .EnumerateMonitorPortSettings (ctx , & rpc.EnumerateMonitorPortSettingsRequest {
149+ defaultSettings , err := srv .EnumerateMonitorPortSettings (ctx , & rpc.EnumerateMonitorPortSettingsRequest {
148150 Instance : inst ,
149151 PortProtocol : portProtocol ,
150152 Fqbn : fqbn ,
@@ -153,14 +155,19 @@ func runMonitorCmd(
153155 feedback .Fatal (i18n .Tr ("Error getting port settings details: %s" , err ), feedback .ErrGeneric )
154156 }
155157 if describe {
156- settings := make ([]* result.MonitorPortSettingDescriptor , len (enumerateResp .GetSettings ()))
157- for i , v := range enumerateResp .GetSettings () {
158+ settings := make ([]* result.MonitorPortSettingDescriptor , len (defaultSettings .GetSettings ()))
159+ for i , v := range defaultSettings .GetSettings () {
158160 settings [i ] = result .NewMonitorPortSettingDescriptor (v )
159161 }
160162 feedback .PrintResult (& detailsResult {Settings : settings })
161163 return
162164 }
163165
166+ actualConfigurationLabels := properties .NewMap ()
167+ for _ , setting := range defaultSettings .GetSettings () {
168+ actualConfigurationLabels .Set (setting .GetSettingId (), setting .GetValue ())
169+ }
170+
164171 configuration := & rpc.MonitorPortConfiguration {}
165172 if len (configs ) > 0 {
166173 for _ , config := range configs {
@@ -173,7 +180,7 @@ func runMonitorCmd(
173180 }
174181
175182 var setting * rpc.MonitorPortSettingDescriptor
176- for _ , s := range enumerateResp .GetSettings () {
183+ for _ , s := range defaultSettings .GetSettings () {
177184 if k == "" {
178185 if contains (s .GetEnumValues (), v ) {
179186 setting = s
@@ -196,10 +203,7 @@ func runMonitorCmd(
196203 SettingId : setting .GetSettingId (),
197204 Value : v ,
198205 })
199- if ! quiet {
200- feedback .Print (i18n .Tr ("Monitor port settings:" ))
201- feedback .Print (fmt .Sprintf ("%s=%s" , setting .GetSettingId (), v ))
202- }
206+ actualConfigurationLabels .Set (setting .GetSettingId (), v )
203207 }
204208 }
205209
@@ -229,7 +233,6 @@ func runMonitorCmd(
229233 }
230234 ttyIn = io .TeeReader (ttyIn , ctrlCDetector )
231235 }
232-
233236 monitorServer , portProxy := commands .MonitorServerToReadWriteCloser (ctx , & rpc.MonitorPortOpenRequest {
234237 Instance : inst ,
235238 Port : & rpc.Port {Address : portAddress , Protocol : portProtocol },
@@ -238,6 +241,21 @@ func runMonitorCmd(
238241 })
239242 go func () {
240243 if ! quiet {
244+ if len (configs ) == 0 {
245+ if fqbn != "" {
246+ feedback .Print (i18n .Tr ("Using default monitor configuration for board: %s" , fqbn ))
247+ } else if portProtocol == "serial" {
248+ feedback .Print (i18n .Tr ("Using generic monitor configuration.\n WARNING: Your board may require different settings to work!\n " ))
249+ }
250+ }
251+ feedback .Print (i18n .Tr ("Monitor port settings:" ))
252+ keys := actualConfigurationLabels .Keys ()
253+ slices .Sort (keys )
254+ for _ , k := range keys {
255+ feedback .Printf (" %s=%s" , k , actualConfigurationLabels .Get (k ))
256+ }
257+ feedback .Print ("" )
258+
241259 feedback .Print (i18n .Tr ("Connecting to %s. Press CTRL-C to exit." , portAddress ))
242260 }
243261 if err := srv .Monitor (monitorServer ); err != nil {
0 commit comments