Arduino Command-Line Utility ?

Is there such a thing?
I want to use an Arduino Command-Line Utility into a C# program. At least this is my goal. I really hope there is such a thing in existence. I searched a little on the web, but is better to come here and ask directly to the source !!!
Thank you.

Did you Google exactly the title of your post or entered that in the search field of the forum?

See

yes...I find some github pages/projects.... but I said to myself to ask here first - you guys KNOW BETTER than me choosing randomly !
So, what do you suggest/recommend for me?
....aha, you re-edited your last post...ok, I will go on those that you show there.
I hope you didnt pick them randomly and you actually used them.

I added links in the previous post

That’s the officially provided arduino tool / CLI

The « getting started » is here

1 Like

YES ! thats even better ! a beginner tutorial is excellent !
Very good idea from your part !
Thank you !

Have fun

Also...what im most interested in, is the 'serial monitor' function. This is what Im after to read from the interface. I hope your suggestions contain this special function in them, and hopefully, even more... Please check for me until I will familiarize myself, if indeed the CLI is containing 'serial monitor' and how is called. Thank you !

Hum… The Serial monitor is part of the IDE.

Reading from the Serial port does not require anything specific to arduino it’s just using a plain old serial port and you have c# support for this

Something along those lines - fully untested

using System;
using System.IO.Ports;

class ArduinoSerialReader
{
    static void Main(string[] args)
    {
        SerialPort arduinoPort = new SerialPort("COM3", 115200); // Replace "COM3" with the appropriate port name

        try
        {
            arduinoPort.Open(); // Open the serial port
            arduinoPort.DataReceived += ArduinoPort_DataReceived; // Subscribe to the DataReceived event
            Console.WriteLine("Listening for Arduino data...");

            Console.ReadLine(); // Wait for user input to exit

            arduinoPort.Close(); // Close the serial port
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }

    static void ArduinoPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort port = (SerialPort)sender;
        string receivedData = port.ReadExisting(); // Read the available data from the serial port
        Console.WriteLine("Received data: " + receivedData);
    }
}
1 Like

Is this Arduino CLI the default/official Command-Line Utility FROM Arduino?
Or is something made by a 3d party?
.....
edited after awhile ... I think I start to get it now. I start to click more links into that page you send me and I reach the github page of this Arduino CLI that you suggested. I also find the installation procedure and some more info in the very bottom of this page.
https://github.com/arduino/arduino-cli

The Arduino CLI is from the Arduino company itself, I think it’s what the IDE 2.x uses under the hood

The c# code posted above to listen to a generic serial port is not (anything can be attached to that port, does not need to be an Arduino)

1 Like

Based on the information you shared so far, I think J-M-L's suggestion of reading directly from the serial port in your C# code instead of using Arduino CLI for that purpose is the best approach.

However, since the subject came up, I should point out that Arduino CLI does include a command line equivalent of the Serial Monitor:

https://arduino.github.io/arduino-cli/latest/commands/arduino-cli_monitor/

The reason this could be interesting in theory is that Arduino CLI has a "pluggable monitor" system. This system allows for any arbitrary communication channel to be used as the "port" for communication between the computer and the Arduino board. In addition to the serial-monitor tool that is distributed by Arduino for communication over ports of the serial protocol. Boards platforms can provide their own pluggable monitor tools for communication with ports of other protocols. For example, the Teensy boards platform installs a custom teensy-monitor pluggable monitor tool for communication with the teensy protocol of the USB HID "ports" of Teensy boards.

So a valid use case for using Arduino CLI's monitor capability in an application (as Arduino IDE 2.x does) instead of implementing the port monitoring code in your own application code is that Arduino CLI acts as a "universal" monitor, which can use ports of any arbitrary protocol rather than only serial. Whether that pluggable capability is at all beneficial depends on your application. If you are only ever going to be working with serial ports, then there is no benefit. At this time, I'm only aware of the two pluggable monitor tools I mentioned. I expect that over time the Arduino community will take advantage of this recently added "pluggable monitor" system to create some amazing new ways of communicating with Arduino boards, but that future expansion of the pluggable monitor tool ecosystem is only hypothetical at this time.

1 Like

Hmmm, to be totally sincere, I overlook the code, being super concentrated on the arduino setup. So I totally misread that code snippet in C#, until your mentions.
But - I still - think arduino board is imperative. Because it has a USB to serial hardware function build in. Without it, I will have to add my own USB to serial converter and is a pain in the but, also I dont have it separately, or unaware on other hardware I have. I know for sure arduino has it. So that's an important point in using this board.
(BTW, I have Arduino UNO (probably R3)) hardware board.
So... Im not completely sure, if I can use that --direct-- C# method mentioned only from the arduino board -directly-, without any helpful software like that command-line utility. But if this is a real option, then it is indeed simplifying the entire thing, immensely !

  • In conclusion, if I understand it correctly, I plug in the arduino board, making the USB-serial conversion between my external logic/input source and my PC, and having only this "gate" open on "COM3" or whatever (ill have to be able to check this as well), open my MVS C# and start pumping code in it. All this without any other inbetween arduino software. Do tell me if I get it right so far.
    Thank you very-very much so far !

You understand correctly. Your C# code can read data from the "virtual COM port" produced by tthe USB to serial hardware on the Uno board, just the same as Serial Monitor does. There is no need for a separate USB to serial converter with the approach J-M-L suggested.

1 Like

I made the setup.
I opened a 'test' program in Arduino software and everything is working fine on this side.


So in other words, on COM5 @ 9600 baud rate I get all that nice data bytes in the 'serial monitor' window.
Now... in C#, with the sample code provided, I get nothing.

I think I have to add a link of some kind between the arduino board @ COM5 and my C#...
I dont know.... your advice is invaluable at this point.
What I should try next? I love the simplicity at this point... but... nothing reads.
...
The event ArduinoPort_DataReceived , i dont think it is activated...perhaps?
...
minor correction:

 arduinoPort.DataReceived += new SerialDataReceivedEventHandler(ArduinoPort_DataReceived);

but still, nothing.

Is working now !!!
WOW - this is truly very cool !


Thank you very much - everyone !

1 Like

Problem !
I get these erroneous lines (that I marked with a red arrow)
I get these erroneous lines also in arduino serial monitor as well, but they are managed better there than here. Sometimes there, but always here.

  • Whats the problem?
    I believe...is some sort of synchronicity ? between the (very low) frequency of the outside clock for the data I transmit (to arduino pins) and the 9600 baud rate on com5 ? Is my only guess.... What you say? How do I sincronize them? or do they need to be synchronized?

Your C# code gets an event when new serial data arrives. It then quickly grabs any characters that have been received, and writes them to console, followed by a newline.

If your C# code manages to run fast enough, it will hear the first character of a line arrive, and then grab "the rest" before the full line has arrived. After it has done this, the other half of your line is received, and gets printed as a new separate line.

You'd need to do some sort of checking of your incoming data, and only WriteLine when you get the '\n' newline character at the end of your Arduino's transmitted println() data.

1 Like

Alternatively, you may be able to instead just use Console.Write() , which may write the newline from the incoming data directly to the console, but I'm not sure about that.

I still believe is a matter of synchronicity between data send by the arduino board and the listening procedure in C#.
My setting in C# is as mentioned already, "COM5" @ 9600 Baud Rate.
Is this 9600 actually sent from the arduino board? or a little bit different number?
I will also go with this question to the C# forums and find there some answers.
The simple test for '\n' (newline) character in code, is creating more problems than already is. It must be a trick somewhere.... Ill have to find it.
Thank you very much so far !

The 9600 baud is definitely the same number (or at least so close that it won't be an issue). At 9600 baud, the connection should be fairly robust, especially over the built-in USB to Serial adapter.

If you want to post your C# code, and your Arduino test sketch, I'm sure someone will take a look at it.

1 Like