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);
}
}
}
}