SOLVED! Visual Studio problem: Not reading anything on my serialPort

Hi,

I have a problem. I configured an arduino to constantly give a status and measurement output over a serial line. With a serial capture program i can see it coming in and therefore i know at least that is working!

However: When i run a small code on visual studio i get nothing!
I’ve worked with Visual studio before and never caused any real problems like this… i just can’t figure it out!

My Visual studio code:

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

using System.IO.Ports;

using System.Runtime.InteropServices;

namespace SerialGUI
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }

        private void COMselectDropdown_Click(object sender, EventArgs e)
        {
            COMselectDropdown.Items.Clear();
            string[] s = SerialPort.GetPortNames();
            int n = s.GetLength(0); // Get number of available COM ports
            // Add available COM ports to dropdownlist
            while (n > 0)
            {
                COMselectDropdown.Items.Add(s[n-1]);
                n--;
            }
        }

        private void COMselectDropdown_SelectedIndexChanged(object sender, EventArgs e)
        {
            string s = Convert.ToString(COMselectDropdown.SelectedItem);
            SerialPort1.PortName = s;
            SerialPort1.Open();
            //SerialPort1.DiscardInBuffer();
            timer1.Enabled = true;
        }


        private void timer1_Tick(object sender, EventArgs e)
        {
            int c;
//            if (SerialError >= 1)
//            {
//                InputBox.AppendText(String.Format("{0,2:X} ", "Unable to open port"));
//            }
            int temp = SerialPort1.BytesToRead; 
            if (temp >= 1)
            {
                c = SerialPort1.ReadByte();
                InputBox.AppendText(String.Format("{0,2:X} ", c));
            } 
        }


    }
}

It is a code that i took off of an old program i wrote, so there might be some additional data. I just wanted to make a small program that gives an output in a text screen. Then i know it is working…

Output by arduino:

0x41 0x54 0x43 0xD7 0xDF 0xFF 0xFF 0x0D

Idea is to read every byte apart…

Does someone have a little more C# experience than me and can figure it out???

Thanks in advance!

P.S.: Forgot to say, but the problem is the BytesToRead and ReadByte… They receive nothing at all!

            timer1.Enabled = true;

Serial data arrival triggers (or can) callbacks. You do not a timer to periodically read data.

            int temp = SerialPort1.BytesToRead; 
            if (temp >= 1)
            {
                c = SerialPort1.ReadByte();
                InputBox.AppendText(String.Format("{0,2:X} ", c));
            }

Reading serial data and manipulating the UI in the same thread won’t work.

Does someone have a little more C# experience than me and can figure it out???

I have a multithreaded C# program that sends data to, and reads data from, an Arduino.

CommunicateWithArduino.zip (54 KB)

PaulS:
I have a multithreaded C# program that sends data to, and reads data from, an Arduino.

This has saved my day! Great program, i adjusted the NewLine from '\n' to '\r' and it worked like a charm!