Reading two sensors values in Visual Studio in separate textBox?

Hello, I'm trying to build a simple application to read several different sensors in "Microsoft Visual Studio 2022" in C# Windows form Applications (.NET Framework 4.7.2), my programming knowledge is zero that's why I'm looking at different tutorials to build something from scratch, I encountered a problem when I want to use more than one sensor and get the reading in different textBoxes, I don't know how to divide the incoming data so that it hits the appropriate textBox.
Would there be someone who could help me ?
code of Arduino

/*
  YF-S201-Water-Flow-Sensor
  modified on 14 oct 2020
  by Amir Mohammad Shojaee @ Electropeak
  Home

  based on www.hobbytronics.co.uk examples
*/
double flow1; //Water flow L/Min
double flow2; //Water flow L/Min  
int flow1sensor = 2;
int flow2sensor = 3; 
unsigned long currentTime;
unsigned long lastTime;
unsigned long pulse_freq;
 
void pulse () // Interrupt function

{
   pulse_freq++;
}

   void setup()
 {
   pinMode(flow1sensor, INPUT);
   Serial.begin(9600);
   attachInterrupt(0, pulse, RISING); // Setup Interrupt
   currentTime = millis();
   lastTime = currentTime;
   pinMode(flow2sensor, INPUT);
   Serial.begin(9600);
   attachInterrupt(0, pulse, RISING); // Setup Interrupt
   currentTime = millis();
   lastTime = currentTime;
}

   void loop ()
{
   currentTime = millis();
   // Every second, calculate and print L/Min
   if(currentTime >= (lastTime + 500))
   {
      lastTime = currentTime; 
      // Pulse frequency (Hz) = 7.5Q, Q is flow rate in L/min.
      flow1 = (pulse_freq / 7.5); 
      pulse_freq = 0; // Reset Counter
      flow2 = (pulse_freq / 7.5); 
      pulse_freq = 0; // Reset Counter
      Serial.print(flow1, DEC); 
      Serial.println(" L/Min");  
      Serial.print(flow2, DEC); 
      Serial.println(" L/Min");   
   }
}

VS2022

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.Close();
            serialPort1.PortName = "com16";
            serialPort1.BaudRate = 9600;
            serialPort1.DataBits = 8;
            serialPort1.Parity = Parity.None;
            serialPort1.StopBits = StopBits.One;
            serialPort1.Handshake = Handshake.None;
            serialPort1.Encoding = System.Text.Encoding.Default;
            serialPort1.Open();
        }

        private void Timer1_Tick(object sender, EventArgs e)
        { 
            string s;
            s = textBox1.Text + "," + "," + "," + "";
            string[] somestring;
            // Split string based on comma
            somestring = s.Split(new char[] { ',' });
            textBox2.Text = somestring[1];
            textBox3.Text = somestring[2];
            textBox4.Text = somestring[3];
            textBox5.Text = "";
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                string mydata = "";
                mydata = serialPort1.ReadExisting();
                if (textBox1.InvokeRequired)
                    textBox1.Invoke(new Action(() => { textBox1.Text += mydata; }));
                else
                    textBox1.Text += mydata;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

General principle:
You need to add labels to the values so that the C# program can distinguish them.
That is, from the arduino side, you need to print not just two numbers, but something like
Flow1 = 22 L/min
And on the receiver side, you need to parse these lines and the one that starts with Flow1 send to the first TextBox, and with Flow2 - to the second

looks like you are transmitting comma seperate values which you split up using string.split() method
if just running using the Serial monitor do the values display correctly?
give an example?

the the print in serial Monitor looks like this
code
in the code I added 1 and 2 to know which reading is from which sensor

      Serial.print(flow1, DEC); 
      Serial.println(" L/Min");  
      Serial.print(flow2, DEC); 
      Serial.println(" L/Min");
      Serial.print(flow1, DEC); 
      Serial.println(" 1 L/Min");  
      Serial.print(flow2, DEC); 
      Serial.println(" 2 L/Min");

reading is only from one sensor, there is no reading from the second sensor.

assuming each line is like

13.06666 1 L/Min

you can split and parse so

using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string LastCOMMessage = "13.06666 1 L/Min";
            string[] messageComponents = LastCOMMessage.Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            //  string[] messageComponents = LastCOMMessage.Split(' ', StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < messageComponents.Length; i++)
                Console.WriteLine("element " + i + " is " + messageComponents[i]);
            float f = float.Parse(messageComponents[0]);        // parse data
            Console.Write("flow " + f);
            int sensor = int.Parse(messageComponents[1]);       // parse sensor
            Console.WriteLine(" from sensor " + sensor);
        }
    }
}

a run gives

element 0 is 13.06666
element 1 is 1
element 2 is L/Min
flow 13.06666 from sensor 1

you know the sensor and the data value you can now copy the value to the appropriate TextBox

Note: avoid posting images of text displays - they consume a lot of memory and are impossible to copy from

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.