How to output from UNO R3 commands from a C# application ?

I started to press buttons , I set it to 9600 on COM5, and I pressed all the buttons on the Settings menu strip. Nothing happened. Hmmmm.


Just to check things out, I opened again TeraTerm5 and it is correctly showing that "Arduino is ready" as before.

Ahaa,
After I opened again TeraTerm5 and it is correctly showing that "Arduino is ready", i close it, and I started your C# application again.
I set the baudrate to 9600 the very first thing, THEN I set to COM5, and I finally got that "Arduino is ready" signal ! Glorious ! So, it behave like before with my other test, when I was "helped" by opening TeraTerm5 first then it worked here in C#. Very interesting indeed....hmmm
-And what to do next?

the arduino code already programmed into UNO R3, accepts strings with a start < and end > character , so I just did that, and I got some feedback ????? - I guess. See that line with 12 and ffff, I believe is a feedback.
The line with rrr I didnt got a feedback, and the cursor just skipped on the next line

Next, in Arduino code I added:

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
    pinMode(2, OUTPUT);
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
}

and then this:

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        if (receivedChars=="<3H>") {digitalWrite(3, HIGH);}
        if (receivedChars=="<3L>") {digitalWrite(3, LOW); }
        
        newData = false;
    }
}

This showNewData(); function is inside void loop(), checked continuously.
I tested the program from your console and ... it didnt lit up anything.


My circuit is very simple. I connected 4LEDS to pin3,pin4,pin5,pin6 of arduino Uno R3, all 4 with a common ground through 10k resistor to UNO R3 GND pin as well.
Here is my cct:

what type is receivedChars? could you have end of line characters in the received data? hence the test for equality fails?

I followed and copy-paste the code from this tutorial here:
" Example 3 - A more complete system"

try comparing the first 4 characters of the received data, e.g.

if (strncmp(receivedChars,"<3H>", 4) == 0) {digitalWrite(3, HIGH);}
void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        //if (receivedChars=="<3H>") {digitalWrite(3, HIGH);}
        //if (receivedChars=="<3L>") {digitalWrite(3, LOW); }
        if (strncmp(receivedChars,"<3H>", 4) == 0) {digitalWrite(3, HIGH);}
        
        newData = false;
    }
}

This is correct here:


But no LED opened in reality.

should the test be

 if (strncmp(receivedChars,"3H", 2) == 0) {digitalWrite(3, HIGH);}

or

 if (strstr(receivedChars,"3H") > 0) {digitalWrite(3, HIGH);}

My FRIEND!!!! This is IT !
It is working !!!! Yaaaaaaaaaaay... Thank you so much !
Ohohooooaaaa... hahahaha - man...
using your code:

if (strncmp(receivedChars,"3H", 2) == 0) {digitalWrite(3, HIGH);}

I also realized that even if Im not receiving that "Arduino is ready", the LEDs are still operated correctly by the commands.
You see in the picture here, no "Arduino is ready" feedback, but it is open/close the LED correctly.

But even if this is working ok now, Why I have to open EVERY TIME that TeraTerm5 program, to initialize COM5, close TeraTerm5 and open your C# app to start correctly and recognize COM5 in it?
Scratch that !!!
I just made a quick test and it is commanding the LED correctly.
The thing is that is not receiving that "Arduino is ready" when connected to COM5 !!!!
That was a great help as an indicator that I correctly connected to COM5 !
That was the thing.
SO now... it's working... hmmm I will make some more tests because this is dodgy as hell and Im afraid at some point in time, it will just stop working "just because" - I hope not, but Im a experienced person. Haha.
Thank you so much mister @horace !!! You are awesomeness !

the C# may be missing the first line of text
try a delay at the start of the code

void setup() {
    Serial.begin(9600);
    delay(2000);
    Serial.println("\n<Arduino is ready>");
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
1 Like

I used your suggestion:

And, Nope, nothing

Im observing a small but probably important little detail about TeraTerm5 program.
When Im changing the COM's it is opening a NEW window actually !
It is not opening the NEW COM in the SAME window !
I have to close the previeous window to leave the new COM5 operational.
Hmmmm - maybe this is the little trick they use?
Very interesting !

The problem with your program is that it is not showing the "Arduino is ready" even if I close and restart the program. As it should be.
Hmmm. Once it is losing the first good conection, it doesnt receive it back when restarted, ONLY when Im opening that TeraTerm5 program, that is reseting something !!! Then, it appears in your program on the next start.

Arduino code until here:
ReceiveOutput.ino (2.0 KB)
C# code until here:
CsharpCOMTerminal.zip (50.5 KB)
and also a readme file
readme first.txt (1.5 KB)

Hi @horace
Ive added:

            //select baudrate 9600 automatically
            baudRateToolStripMenuItem.DropDownItems[3].Select();
            baudRateToolStripMenuItem.DropDownItems[3].PerformClick();
            //select baudrate COM5 automatically
            comPortsStripMenuItem.DropDownItems[1].Select();
            comPortsStripMenuItem.DropDownItems[1].PerformClick();

I made 1 button to send command to arduino pin2 (in this case) and I tried to run it but... surprise, not working correctly. Here is what I tried:

bool[] pinison = { false, false, false, false, false, false, false };
        private void button1_Click(object sender, EventArgs e)
        {
            if (pinison[1])
            {
                textBox1.AppendText("<");
                textBox1.AppendText("2");
                textBox1.AppendText("L");
                textBox1.AppendText(">");

                //textBox1.Text += '<';
                //textBox1.Text += '3';
                //textBox1.Text += 'L';
                //textBox1.Text += '>';
                //textBox1.Text += "\r\n";

                
               // textBox1.Text += "<3L>\r\n";
                button1.Text = "pin2_off";
                pinison[1] = false;
            }
            else
            {
                textBox1.Text += "<2H>\r\n";
                button1.Text = "pin2_on";
                pinison[1] = true;
            }
        }

All your code is working ok ONLY when Im typing into textbox !!!
But If Im modifying the textbox, with //textBox1.Text += '<'; or textBox1.Text += "<2H>\r\n"; or simple Ctrl+V, will not work.
Will work only when KeyPress !!!
-How to make it work from my button ?
Thank you !

in the event handler button1_Click() you set up the text but don't do anything with it - do you need to transmit it over serial?

1 Like

Ive added code in that button1_Click() event, and in the textbox it is showing a continuous repetition of whatever I want to write in it. Its not only once, its continuous. I believe thats because the actual command is ' ' char related, so this is how I see it working: textBox1_KeyPress is the hearth of the code and textBox1.AppendText is the blood that cary the information around. It might be some very obscure serial related functions Im not aware, that are doing some backdoor work. But the behaviour is like this, for each --chr-- character Im TYPING into textbox, from keyboard specifically, not paste from ctrl+v or code parsed , then something is taking that character, compare it, and allow the next character to be linked to the previeous character, and another check is made, then the 3rd then the 4rth and all checked and when the final one is inserted, THEN, it is executing the code in arduino.
I made some tests to figure out all this described here:
I write < from keyboard and ctrl+v the rest, and it didnt work;
I write < from keyboard and code parse the rest, and it didnt work;
I write <3H from keyboard and ctrl+v or code parse the '>' and nothing.
The ONLY way is typing my hand !!! Thats it.
Now... I made some mistakes and they also told me a story:
When I was typing <3H I put another < , then I press Delete key and then put >
The code executed but with a black character in the last part, and the led didnt respond.
This told me that EVERY chr -character- is literally parsed and verified and interpreted inside this little serial comunication window.
I believe I also understand why ctrl+v or code parsing are not functioning.
Is because they have their own character that this serial window will decode and insert it as it is just another chr character.
The solution is to "ignore" ctrl+V or code parsing ----characters--- when the textbox is expecting characters in it.
Clear as mud ?

I want the liberty to send the code <3H> (as an example), yes, over serial, on COM5, not only from typing it into textbox as it is hard coded now. I want the option to send it from a button press, or from code, without any kind of monkey bizniz with errors or bugs.