Serial Monitor for arduino-cli

Hello to all. I am making the transition from the Arduino IDE to CLI and would appreciate help.

I have successfully built sketches including downloaded and my own libraries.

to compile, I use a script file containing:
arduino-cli compile -b arduino:avr:mini /home/walt/Arduino/robot

to upload, I use a second script file containing:
arduino-cli upload -p /dev/ttyUSB0 -b arduino:avr:mini /home/walt/Arduino/robot

for debugging with Serial.print(), I open a second monitor window and initiate a terminal session with:
sudo putty /dev/ttyUSB0 -serial -sercfg 9600,8,n,1,N

The script files are very convenient for the repetitive and error prone command entry. However, I am having a couple of issues with this process:

  1. Although I have both Serial.begin(9600) and Serial.end() in my setup() routine, I am unable to upload any revisions after a test run until I completely close the terminal window. I would like to have the terminal window continually available like the serial monitor in the IDE.

When the uploads fail, I see this message:

avrdude: stk500_getparm(): (a) protocol error, expect=0x14, resp=0x00
avrdude: stk500_initialize(): (a) protocol error, expect=0x14, resp=0x00
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check.

After closing the terminal window, the uploads are successful.

  1. When executing both the compile and upload steps from the two script files, I get the short confirmation message after compiling but nothing after uploading unless the upload fails when I get the error message above. When I execute the upload directly in a command line, I do get a short confirmation message. I'm curious about what the difference is.

Any suggestions with respect to these two problems or suggestions on how to better manage the workflow would be appreciated.

1 Like

waltoler:

  1. Although I have both Serial.begin(9600) and Serial.end() in my setup() routine, I am unable to upload any revisions after a test run until I completely close the terminal window. I would like to have the terminal window continually available like the serial monitor in the IDE.

A serial port can only be opened by one program at a time. The way the Arduino IDE is able to upload with the Serial Monitor open is by closing the port connection to the Serial Monitor, doing the upload, then re-opening the port in the Serial Monitor. You would need to replicate this behavior in your script. I found this works:

# kill all running putty processes to free the serial port
sudo killall putty
arduino-cli upload -p /dev/ttyUSB0 -b arduino:avr:mini  /home/walt/Arduino/robot
# start putty
# & causes it to run in the background so it doesn't block the terminal
sudo putty /dev/ttyUSB0 -serial -sercfg 9600,8,n,1,N &

waltoler:
When I execute the upload directly in a command line, I do get a short confirmation message.

I'm not able to reproduce this. I don't get any output from arduino-cli upload unless I add the --verbose option to the command.

What does the message say?

Which version of Arduino CLI are you using?

@pert

Once again, you have solved my problem completely. I have taken your solution and incorporated it into a bash script that manages the arduino-cli compilation, uploading and serial monitor initiation for a sketch that I specify as an argument on the command line, e.g., run.sh mySketch

Here is my script file: run.sh

#!/bin/bash

#compile
arduino-cli compile -b arduino:avr:mini /home/walt/Arduino/$1

kill all running putty processes to free the serial port

killall putty

#upload
arduino-cli upload -p /dev/ttyUSB0 -b arduino:avr:mini /home/walt/Arduino/$1

start putty

& causes it to run in the background so it doesn't block the terminal

putty /dev/ttyUSB0 -serial -sercfg 9600,8,n,1,N &

It turns out that I don't need to use the sudo option in the 'killall' and 'putty' commands.

Although I have many years of c++ programming experience on MS Windows, I would only classify myself as an "experienced" beginner with Linux and Arduino. I was not familiar with the initiation of background processes in Linux and I think there will be many others with an experience level similar to mine who will appreciate your solution. Consequently, I have edited the subject heading on my original post to make it more "searchable" for arduino-cli users looking for a Serial Monitor capability.

Thanks again for your help.

1 Like

You're welcome. I'm glad if I was able to be of assistance.

Thanks for taking the time to post your findings and script. I'm sure this is is something other Arduino CLI users will be interested in. We've been spoiled by the Arduino IDE being able to automagically handle the Serial Monitor's serial connection because of it being an integrated part of the IDE.

Enjoy!
Per