Go Down

Topic: Unable to properly use VS Serial Monitor but Arduino IDE works (Read 2878 times) previous topic - next topic

jim-yang

I'm looking at Robin2's Serial Input Basics, 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
null character for port.Write

ieee488


sterretje

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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

jim-yang

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.

ieee488

You need to read what happens when you open a serial port for communication with the Arduino on the serial port!



.

jim-yang

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.

jim-yang

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?

sterretje

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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

ron_sutherland

my projects: https://github.com/epccs

jim-yang

 
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...


jim-yang

Is this what you are trying to use?

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
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.

sumguy

#11
Sep 13, 2018, 10:19 pm Last Edit: Sep 13, 2018, 10:33 pm by sumguy Reason: App design
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

ron_sutherland

#12
Sep 13, 2018, 10:50 pm Last Edit: Sep 13, 2018, 10:53 pm by ron_sutherland
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.
my projects: https://github.com/epccs

sumguy

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

jim-yang

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.

Go Up