Arduino Forum

Using Arduino => Interfacing w/ Software on the Computer => Topic started by: jim-yang on Sep 13, 2018, 06:38 pm

Title: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: jim-yang on Sep 13, 2018, 06:38 pm
I'm looking at Robin2's Serial Input Basics (http://forum.arduino.cc/index.php?topic=288234.60), and was trying to run some of his code. If I run it through the Arduino IDE, I can just click the serial monitor and begin inputting the things needed to run the program.

However, if I'm trying to use Visual Studio(VS), I can create a Arduino Project, load the code and the serial monitor works normally, but if I try and use a alternative monitor, I'm not sure where I need to be looking.

When I run my program, the Ardunio UNO shows it is receiving and transmitting, but I can't seem to find where it is showing it. I would assume it shold appear in the serial monitor where the Open and close commands occur, and the monitor shows such with the "Port opened/Port closed" remarks.

Where is my display going??? The only other thing I could think of is that I'd have to have the arduino not print, but send the array back to the GUI where I would then have to create labels for it to reappear, but that seems like the least likely solution.

For instance, if I load the receiving several character example into the VS IDE and load it, I can press the serial monitor and send "abcdefg", the arduino the RX and TX exactly as it should on the Serial Monitor.
If I create a different monitor(VS Windows Form), as below, the TX and RX behave exactly as if I used the IDE Serial Monitor except nothing shows on any of the Serial Monitors. What I tried was to have two separate instances of VS running, one for the IDE, one for the Serial Monitor, and then had the Serial Monitor display open for both instances, run the GUI to send the array, the lights light up, but neither Monitor displays any instance of print. Even the "Arduino is ready" is not displayed. Is there another window I'm supposed to be looking for?

I have attached a screenshot of the Serial Monitors in the attachments.

Robin2's Code to read char and display them
Code: [Select]

char receivedChar;
boolean newData = false;

void setup() {
 Serial.begin(9600);
 Serial.println("<Arduino is ready>");
}

void loop() {
 recvOneChar();
 showNewData();
}

void recvOneChar() {
 if (Serial.available() > 0) {
 receivedChar = Serial.read();
 newData = true;
 }
}

void showNewData() {
 if (newData == true) {
 Serial.print("This just in ... ");
 Serial.println(receivedChar);
 newData = false;
 }
}

My VS GUI Serial Monitor replacement

Code: [Select]

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApp5
{
    public partial class Form1 : Form
    {
        SerialPort port;
        public Form1()
        {
            InitializeComponent();
            
            
        }

        private void Arraysender_Click(object sender, EventArgs e)
        {
            //byte[] sendData = { 0x80, 0x0, 0x0, 0x00, 0x02, 0x20, 0x00, 0x03, 0x00, 0x05, 0x15, 0x00, 0x04, 0x00, 0x00, 0x00, 0x3F, 0x04, 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00, 0x40 };
            char[] sendChar = { '°', 'q', 'w', 'e', 'r', 't', 'y' };
            port = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One);
            port.Open();
            port.Write(sendChar,0,7);
  
            port.Close();
            
        }
    }
}

Reference: port.Write information (https://docs.microsoft.com/en-us/dotnet/api/system.io.ports.serialport.write?view=netframework-4.7.2#System_IO_Ports_SerialPort_Write_System_Char___System_Int32_System_Int32_)
null character for port.Write (http://www.neurophys.wisc.edu/comp/docs/ascii/)
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: ieee488 on Sep 13, 2018, 07:40 pm
The two programs are hardly alike.


.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: sterretje on Sep 13, 2018, 07:55 pm
Ain't you supposed to read the data in your VS application? As it stands now, the Arduino sends it but you don't read it.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: jim-yang on Sep 13, 2018, 08:05 pm
Ain't you supposed to read the data in your VS application? As it stands now, the Arduino sends it but you don't read it.
Maybe? It's not really the intended purpose though, I was wondering if it was possible to do Robin2's program, input multiple characters, and then it stores them in an array to print, but instead of using the default Serial Monitor, have a separate VS application behave like the Serial Monitor, but to just use it to send it the data. The Arduino would read the data, and then print it.

I'm just trying to follow the communication between the devices, and this seemed like a pretty simple test. One side send, one side receive.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: ieee488 on Sep 13, 2018, 08:18 pm
You need to read what happens when you open a serial port for communication with the Arduino on the serial port!



.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: jim-yang on Sep 13, 2018, 08:20 pm
The two programs are hardly alike.


.
Could you elaborate more? Isn't the Arduino Project option on the VS the same as the Arduino IDE? I would assume the only differences would be that VS would be able to integrated with other platforms easier.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: jim-yang on Sep 13, 2018, 08:25 pm
You need to read what happens when you open a serial port for communication with the Arduino on the serial port!
.
Serial communication just refers to how data is being transferred between devices. Data is sent back and forth? Are you referring to different types like UART/USART or the protocols like SPI, I2C?
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: sterretje on Sep 13, 2018, 09:03 pm
Maybe? It's not really the intended purpose though, I was wondering if it was possible to do Robin2's program, input multiple characters, and then it stores them in an array to print, but instead of using the default Serial Monitor, have a separate VS application behave like the Serial Monitor, but to just use it to send it the data. The Arduino would read the data, and then print it.
You can't; only one application on the PC can have the serial port open. If serial monitor is open, you can't open the port in your VS application and vice versa.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: ron_sutherland on Sep 13, 2018, 09:27 pm
Is this what you are trying to use?

https://marketplace.visualstudio.com/items?itemName=olback.vscode-serial (https://marketplace.visualstudio.com/items?itemName=olback.vscode-serial)

Looking at the issues, I think it is a work in progress

https://github.com/olback/vscode-serial/issues/1 (https://github.com/olback/vscode-serial/issues/1)
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: jim-yang on Sep 13, 2018, 09:38 pm
 
You can't; only one application on the PC can have the serial port open. If serial monitor is open, you can't open the port in your VS application and vice versa.
:o  that's what I feared...

Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: jim-yang on Sep 13, 2018, 09:44 pm
Is this what you are trying to use?

https://marketplace.visualstudio.com/items?itemName=olback.vscode-serial (https://marketplace.visualstudio.com/items?itemName=olback.vscode-serial)

Looking at the issues, I think it is a work in progress

https://github.com/olback/vscode-serial/issues/1 (https://github.com/olback/vscode-serial/issues/1)
Not really, all I was trying to do was to run Robin2's demo code on VS and to see if I could replicate it and get a better understanding of Serial Communication. I liked that on the Arduino IDE you can just type into the Serial Monitor and the output comes out and you can see what you entered and what was stored. The demo works fine if I create an Arduino project in VS and run it the same way as the IDE. I was just trying to do the same process by creating a GUI to replace entering the data oneself.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: sumguy on Sep 13, 2018, 10:19 pm
Simply put serial communication is usually communication between two devices, say device A and Device B.

Device A and B use a com port of some kind to communicate, as in your case it is com 3, usually A and B are the only devices allowed to use this com port.

Lets say device A is the arduino and device B is the PC, the sketch of the arduino has access to com 3 and so does the PC.

There is another rule we must be aware of and that is the PC is only allowed to use one application or program to access com 3 at a time. This means we can use a Visual Studio app or we can use the arduino IDE Serial Monitor app, we can NOT use both at the same time.

The solution is to build a serial monitor into your VS application. The following code is the beginning of a C# program as a simple monitor and test utility for serial comms.

Serial lines communicate using byte values, a byte can be a value from 0 to 255. When displaying text chars or strings the byte value will be in the range 32 to 127.The fact a serial byte can be a character or a numeric value is something both ends of the communication line need to be aware of. This makes more sense when you can actually see the results displayed from your serial transmissions. In this particular app Robin2 has an arduino program that echoes back the character you send, so we have a routine in VS that is purposely written to send and display a character

Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Collections;


namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {

        SerialPort port = new SerialPort();
        public delegate void myDelegate(byte sData);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            port.PortName = "COM3";
            port.Open();
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.ReadExisting();
        }



        private void stringDebug(byte sData)
        {

            string s = Convert.ToString((char)sData);
            richTextBox1.AppendText(s);

        }


        private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

        {
            byte myData;
            port.ReadTimeout = 20;
            while (port.BytesToRead > 0)
            {
                try
                {
                    myData = System.Convert.ToByte(port.ReadByte());
                    this.BeginInvoke((new myDelegate(stringDebug)), myData);
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            port.Write("A");
        }
    }
}



The routine displaying the character is the "stringDebug" delegate, modifying this routine allows us interpret the byte values in several different ways.

If you have any questions or would like to expand the sample serial monitor

I forgot to mention that the C# app has only two controls.....a richTextbox and a Button
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: ron_sutherland on Sep 13, 2018, 10:50 pm
I think I understand (hopefully), I also use VS code to edit my AVR programs. But they are on a Linux machine (e.g. Raspberry Pi). The AVR UART (its serial port) is crossed over to the Raspberry Pi UART. So I had to find a terminal program like Serial Monitor to interact with the AVR from the Pi. I found "picocom" (e.g. sudo apt-get install picocom). The good news is the next version of Windows (1809 or whatever it is) should have a pseudo terminal device (e.g. ConPTY) that hopefully will allow programs like picocom to work. Windows has not been the best place to find these sort of terminal programs, and I suspect adding a pseudo terminal is not really going to change anything. Actually, now that I have the Raspberry Pi building my AVR projects there is no way I will go back to connecting an AVR to my expensive computer.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: sumguy on Sep 13, 2018, 11:21 pm
The objective is that this be step 1 of 2,3 or 4 in helping the OP understand some simple serial basics, I have noticed this is at least his third post on the subject and if I can give him and others a little direction all the better.

I think once you get the grasp of the way serial works and the way it uses protocols and buffers etc. it can be used with any platform.

Win is just my way of projecting what I am thinking, I wish I owned an expensive computer lol.

cheers
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: jim-yang on Sep 13, 2018, 11:22 pm
Simply put serial communication is usually communication between two devices, say device A and Device B.

Device A and B use a com port of some kind to communicate, as in your case it is com 3, usually A and B are the only devices allowed to use this com port.

Lets say device A is the arduino and device B is the PC, the sketch of the arduino has access to com 3 and so does the PC.

There is another rule we must be aware of and that is the PC is only allowed to use one application or program to access com 3 at a time. This means we can use a Visual Studio app or we can use the arduino IDE Serial Monitor app, we can NOT use both at the same time.

The solution is to build a serial monitor into your VS application. The following code is the beginning of a C# program as a simple monitor and test utility for serial comms.

Serial lines communicate using byte values, a byte can be a value from 0 to 255. When displaying text chars or strings the byte value will be in the range 32 to 127.The fact a serial byte can be a character or a numeric value is something both ends of the communication line need to be aware of. This makes more sense when you can actually see the results displayed from your serial transmissions. In this particular app Robin2 has an arduino program that echoes back the character you send, so we have a routine in VS that is purposely written to send and display a character

Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
using System.Collections;


namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {

        SerialPort port = new SerialPort();
        public delegate void myDelegate(byte sData);

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            port.PortName = "COM3";
            port.Open();
            port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
            port.ReadExisting();
        }



        private void stringDebug(byte sData)
        {

            string s = Convert.ToString((char)sData);
            richTextBox1.AppendText(s);

        }


        private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

        {
            byte myData;
            port.ReadTimeout = 20;
            while (port.BytesToRead > 0)
            {
                try
                {
                    myData = System.Convert.ToByte(port.ReadByte());
                    this.BeginInvoke((new myDelegate(stringDebug)), myData);
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            port.Write("A");
        }
    }
}



The routine displaying the character is the "stringDebug" delegate, modifying this routine allows us interpret the byte values in several different ways.

If you have any questions or would like to expand the sample serial monitor

I forgot to mention that the C# app has only two controls.....a richTextbox and a Button
Thanks for all this info, I'll definitely look into the routine you suggested.
I really like the way you explained it, I understand the point that only A and B use the COM port  to communicate to each other like you said, and that the suggestion of writing out a new Serial Monitor will be needed. But would B technically be the VS application used to send data and not a PC itself? Then it'd be that instances Serial Monitor as the display and not the Arduino's Serial Monitor?

Using the Arduino IDE to upload the sketch, and then the Serial Monitor is still technically A to B communication right?

The way I am thinking is, that I send data from the PC to the Arduino, using a VS application(Windows Forms). The data would be injected via the VS application the same way as if it were typed into the Serial Monitor, and the commands like the Write function would still be executed and sent back to the Serial Monitor of whatever was used to load the Arduino.
When using it on the Arduino IDE, the exchange is directly from Serial Monitor to the Arduino and back, so when I use a VS GUI to send it, I would have assumed the Arduino would be directly printing out to it's Serial Monitor.

So is the real difference that I'm using the Serial Monitor of the VS application to send the data and it's different than the Serial Monitor of the VS instance that loaded the code onto the Arduino?

I ran into the point you made about the char/string and int/byte value earlier when trying to write the array out and trying to fit the offset in.

Yeah, I really wish there were better ways of controlling it, but the Button is pretty convenient in my opinion since all the controls will be in 1 section rather than spread out and having to check multiple controls.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: sumguy on Sep 13, 2018, 11:49 pm
The arduino Serial Monitor and Visual Studio are both PC programs that do similar jobs, Visual Studio can be made a little more flexible in my opinion.

If you try the example I posted and it works ok then maybe you would like to try the next step which is to read the value of the character you are sending.

Here we modify the arduino sketch to echo only the character value sent by Visual Studio

Code: [Select]
char receivedChar;
boolean newData = false;

void setup() {
 Serial.begin(9600);
 Serial.println("<Arduino is ready>");
}

void loop() {
 recvOneChar();
 showNewData();
}

void recvOneChar() {
 if (Serial.available() > 0) {
 receivedChar = Serial.read();
 newData = true;
 }
}

void showNewData() {
 if (newData == true) {
 //Serial.print("This just in ... ");
 Serial.print(receivedChar);
 newData = false;
 }
}



At the C# end we add a new routine that displays the value instead of the character, this routine is called byteDebug.

Code: [Select]
 private void byteDebug(byte sData)
        {

            
            richTextBox1.AppendText(sData + " ");

        }


The invoke for this delegate also needs the name change, change the following line

Code: [Select]
this.BeginInvoke((new myDelegate(stringDebug)), myData);

to this

Code: [Select]
this.BeginInvoke((new myDelegate(byteDebug)), myData);

When you run this it really is a good idea to have a copy of the ASCII table close by so that you understand which characters the numbers represent.





Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: ron_sutherland on Sep 13, 2018, 11:58 pm
The Arduino IDE does some tricks that may be adding confusion. When the compile and upload button is pushed it the IDE GUI closes the serial port so that the upload tool (avrdude) can use it. In older versions of the IDE the Serial Monitor would close, and then the user would have to open it back up after the update, but now it waits while disconnected from the port. With VS Code I have to close the serial terminal program (e.g. picocom) before running the upload tool. I forget to do this all the time and get reminded when the upload tool (avrdude) complains.
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: sumguy on Sep 14, 2018, 06:07 pm
This example uses strings to communicate, the C# code is slightly simpler than the previous example but the arduino is a little more complex.

Both programs have a routine called txData and a routine called rxData, lets look a rxData first.

The C# rxData contains the line
Code: [Select]
richTextBox1.AppendText(port.ReadLine() + "\n");


the ReadLine method reads data arriving at the serial port buffer until it finds the newline character which is decimal 10

Fortunately the arduino has a similar method which we can give a search character a value, so here we give it the same value as the C# method a value of decimal 10

The arduino rxData
Code: [Select]
Serial.readBytesUntil(10,myString,40);

In both cases the decimal value of 10 is discarded and not contained within the byte array (string).

Although it is not important with C# the arduino strings need to be terminated with a zero, the arduino byte array must always be at least 1 byte longer than the string it contains. For example the string "arduino" is 7 bytes then add the terminating zero so the array should be at least 8 bytes long. In the example arduino code I fill the variable myString with zeros before each serial read with the following.

Code: [Select]
memset(myString,0,sizeof(myString));

The terminating zero is neccessary for library routines that compare,display or manipulate the strings, that is for another time.

The C# txData routine contains the WriteLine method, this method automatically appends the newline character (decimal 10)

Code: [Select]
port.WriteLine(textBox1.Text);

The arduino txData may not be clear at first glance but what it does is take a pointer to the memory location that contains the first character/byte of "myString", the while loop increments the pointer through the string, transmitting each byte, until it encounters the zero terminator after which we transmit the newline character decimal 10 so that C# knows the string is complete.

Code: [Select]
  const char *pointer;
          pointer = myString;
    while (*pointer) {
          Serial.print(*pointer);
          pointer++;     
}
         Serial.write(10);


The C# program has 5 controls, a richTextbox, 2 textBoxes and 2 buttons. The idea is to transmit the text from the two textBoxes, for example Motor1 in textbox 1 and run in textbox 2.

The two strings are echoed back by the arduino and retrieved by pressing button2, this second button was included purposely to demonstrate that the data/strings remain in the port's receiving buffer until you decide to read them

Arduino
Code: [Select]

byte myString[40];
;
boolean newData = false;

void setup() {
 Serial.begin(9600);
}

void loop() {
 rxData();
 txData();
}

void rxData() {

 if (Serial.available() > 0) {
          memset(myString,0,sizeof(myString));
          Serial.readBytesUntil(10,myString,40);
          newData=true;
}
}

void txData() {

 if (newData == true) {
          const char *pointer;
          pointer = myString;
    while (*pointer) {
          Serial.print(*pointer);
          pointer++;     
}
         Serial.write(10);
         newData = false;
}
}


C#
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        SerialPort port = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            port.PortName = "COM3";
            port.Open();
            port.ReadExisting();
        }


        void txData()
        {

            if (port.IsOpen)
            {
                port.WriteLine(textBox1.Text);
                port.WriteLine(textBox2.Text);

            button1.Enabled = false;
            button2.Enabled = true;
        }
        }

        void rxData() {
            if (port.IsOpen)
            {
                while (port.BytesToRead > 0)
                {
                    try
                    {
                        richTextBox1.AppendText(port.ReadLine() + "\n");
                       
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    }
                    }
               
                    button1.Enabled = true;
                    button2.Enabled = false;
            }
       
        private void button1_Click(object sender, EventArgs e)
        {
            txData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            rxData();
        }
    }
}







Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: jim-yang on Sep 17, 2018, 09:13 pm
This example uses strings to communicate, the C# code is slightly simpler than the previous example but the arduino is a little more complex.

Both programs have a routine called txData and a routine called rxData, lets look a rxData first.

The C# rxData contains the line
Code: [Select]
richTextBox1.AppendText(port.ReadLine() + "\n");


the ReadLine method reads data arriving at the serial port buffer until it finds the newline character which is decimal 10

Fortunately the arduino has a similar method which we can give a search character a value, so here we give it the same value as the C# method a value of decimal 10

The arduino rxData
Code: [Select]
Serial.readBytesUntil(10,myString,40);

In both cases the decimal value of 10 is discarded and not contained within the byte array (string).

Although it is not important with C# the arduino strings need to be terminated with a zero, the arduino byte array must always be at least 1 byte longer than the string it contains. For example the string "arduino" is 7 bytes then add the terminating zero so the array should be at least 8 bytes long. In the example arduino code I fill the variable myString with zeros before each serial read with the following.

Code: [Select]
memset(myString,0,sizeof(myString));

The terminating zero is neccessary for library routines that compare,display or manipulate the strings, that is for another time.

The C# txData routine contains the WriteLine method, this method automatically appends the newline character (decimal 10)

Code: [Select]
port.WriteLine(textBox1.Text);

The arduino txData may not be clear at first glance but what it does is take a pointer to the memory location that contains the first character/byte of "myString", the while loop increments the pointer through the string, transmitting each byte, until it encounters the zero terminator after which we transmit the newline character decimal 10 so that C# knows the string is complete.

Code: [Select]
 const char *pointer;
          pointer = myString;
    while (*pointer) {
          Serial.print(*pointer);
          pointer++;      
}
         Serial.write(10);


The C# program has 5 controls, a richTextbox, 2 textBoxes and 2 buttons. The idea is to transmit the text from the two textBoxes, for example Motor1 in textbox 1 and run in textbox 2.

The two strings are echoed back by the arduino and retrieved by pressing button2, this second button was included purposely to demonstrate that the data/strings remain in the port's receiving buffer until you decide to read them

Arduino
Code: [Select]

byte myString[40];
;
boolean newData = false;

void setup() {
 Serial.begin(9600);
}

void loop() {
 rxData();
 txData();
}

void rxData() {

 if (Serial.available() > 0) {
          memset(myString,0,sizeof(myString));
          Serial.readBytesUntil(10,myString,40);
          newData=true;
}
}

void txData() {

 if (newData == true) {
          const char *pointer;
          pointer = myString;
    while (*pointer) {
          Serial.print(*pointer);
          pointer++;      
}
         Serial.write(10);
         newData = false;
}
}


C#
Code: [Select]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        SerialPort port = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button2.Enabled = false;
            port.PortName = "COM3";
            port.Open();
            port.ReadExisting();
        }


        void txData()
        {

            if (port.IsOpen)
            {
                port.WriteLine(textBox1.Text);
                port.WriteLine(textBox2.Text);

            button1.Enabled = false;
            button2.Enabled = true;
        }
        }

        void rxData() {
            if (port.IsOpen)
            {
                while (port.BytesToRead > 0)
                {
                    try
                    {
                        richTextBox1.AppendText(port.ReadLine() + "\n");
                      
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    }
                    }
              
                    button1.Enabled = true;
                    button2.Enabled = false;
            }
        
        private void button1_Click(object sender, EventArgs e)
        {
            txData();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            rxData();
        }
    }
}



I wasn't aware there was a second page, and didn't know to respond sooner. I was looking at the codes you posted and have some questions.

1. When I was trying the first code from 9/13, When you add the code to the private void Form1_Load  section, does that only run on the time the form is initialized? There's no sort of indexing when you read the array as well wouldn't the [port.DataReceived += new SerialDataReceuvedEventHandler(port_DataReceived);[/code] mean that the byte being read would be a sum of all the bytes that have already passed?

Also I had to change the code for it to compile. I don't think replacing your port declaration should make a difference?
Code: [Select]
port = new SerialPort("COM3"); //instead of port.PortName = "COM3";

2. When you say
Code: [Select]
port.Write("A"); this would write to the serial monitor correct? Like you said that there are only two controls in the C# app, and all I want to do is a display, would I need a control? One thing that I did was to create a label in my form and then update the text within the label.
Code: [Select]
String label5 = number3.Value.ToString();           //Generate the int to string to display
            display3.Text = label5;
Where I have a numericupdown control named number3, I take the value of the number inside, convert it into a string, and then have my label named display3 take the string to display.

3. I posted a question earlier in the forums but I cannot seem to find that post in my history... Don't we need multiple Serial ports i.e
Code: [Select]
Serial.begin(9600);
Serial1.begin(9600);
? That way you have one port reading data and the other to echo it back to the VS application? As you stated earlier, we need to build a serial monitor in the VS application, and you talk about having the Arduino echo the value sent from VS. I assume you mean that when the Arduino receives a value, it sends it back. If all I want to do is to see what is being echoed back, could I do something similar to #2. When the Arduino receives a byte I can then convert that byte into a char/string and then send it to the VS application where I can then have it displayed in a label?

4. Is the usage of the rich textbox important? Is it basically the output of the Serial Monitor?

5. Could using the Serial.readbytesuntil() replace the while(Serial.available) loop? As long as you could guarantee that the end condition isn't accidentally met while receiving data?

6. Is your port.Writeline(Textbox.text); basically the same thing with me updating the label?
Title: Re: Unable to properly use VS Serial Monitor but Arduino IDE works
Post by: ieee488 on Sep 18, 2018, 12:31 am
When using it on the Arduino IDE, the exchange is directly from Serial Monitor to the Arduino and back, so when I use a VS GUI to send it, I would have assumed the Arduino would be directly printing out to it's Serial Monitor.

The Serial Monitor uses up the one and only hardware serial port that the Arduino Uno has.

If you want to use Visual Studio to communicate with the Arduino Uno while also using the IDE's Serial Monitor, you need to use
a.) software serial library
and
b.) a USB-TTL adapter and connect to Arduino Uno's pins other than 0 and 1


.