Reading two sensors values in Visual Studio?

Hi ,

please I have a question ,

How can I Reading two LM35 sensors on one serial port in visual studio ?

I managed to read one sensor but I can’t reading two in two labels :frowning: :frowning:

My Arduino Code :

int val;
int val2;
int pin = A0;
int pin2 = A1;
float temp;
float temp2;
int prev;
int prev2;
int Loop;


void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);

}

void loop() {
  // put your main code here, to run repeatedly:
gettemp(1000);
Serial.print(",");
getloop(5000);


}
void gettemp(int interval){

if ((millis()-prev)>=interval){
prev = millis();
val = analogRead(pin);
temp = val*0.48828125;
Serial.print(temp);
}}

void getloop(int interval2){
  
if ((millis()-prev2)>=interval2){
prev2 = millis();
val2 = analogRead(pin2);
temp2 = val2*0.48828125;
Serial.print(temp2);
}}

my form in Attachments

Thank U ,

Untitled.png

malaa_borg:
How can I Reading two LM35 sensors on one serial port in visual studio ?

Read up on the SerialPort class.

And if that doesn't help, ask your question in a more appropriate place; this isn't it.

in the Arduino use println() to terminate the lines with '\n' and in C# or VB use SerialPort.Readline() to read the line into a String

you can then parse the String for a float
https://msdn.microsoft.com/en-us/library/t9szttbx(v=vs.100).aspx

how do you tell when receiving if the value is temp1 or temp2?

horace:
in the Arduino use println() to terminate the lines with '\n' and in C# or VB use SerialPort.Readline() to read the line into a String
SerialPort.ReadLine Method (System.IO.Ports) | Microsoft Docs
you can then parse the String for a float
parseFloat Method | Microsoft Docs

how do you tell when receiving if the value is temp1 or temp2?

Thanks horace ,

but I have already received the temperature of one sensor in label 1 , but when I connect two sensor each other the two values appear in one label .

you are printing temp1 every second and temp2 every 5 seconds in between you will but printing thousands of ,
which may well cause problems with the receiving code
rather than using , why not print some indication if the value is temp1 or temp2 and use println() to send an end of line, e.g. if temp1 is 25 and temp 2 is 68 transmit
1 25
2 68
a line starting with 1 is a temp1 value etc

could you let us have the receiving code ?

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

namespace Temp_project
{
    public partial class Form1 : Form
    {
        string datasensor;
        
        public Form1()
        {
            InitializeComponent();

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            label1.Text = "";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            timer1.Enabled = true;
            timer2.Enabled = true;
            if (serialPort1.IsOpen)
            {
                button1.Enabled = false;
            }

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            
            try
            {
                datasensor = serialPort1.ReadLine();
            }
            catch
            {
            }
            
            serialPort1.WriteLine(datasensor);

            label1.Text = datasensor;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            serialPort1.Close();
            label1.Text = "";
            button1.Enabled = true;
        }



        private void timer2_Tick_1(object sender, EventArgs e)
        {
            try
            {
                datasensor = serialPort1.ReadLine();
            }
            catch
            {
            }

            serialPort1.WriteLine(datasensor);

            label4.Text = datasensor;
        }
        }
    }

rather than using a timer event to read the serial port use the SerialPort.DataReceived() event
if you transmit the temperature data with an indicator which shows which temperature it is (such as line starting with 1 for temp1) you can do somthing like

     private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            string s = serialPort1.ReadLine();
            if (s.StartsWith("1"))
                label1.Text = s;
            // similar for label2
       }

Hi,

Here is my suggestion:

With Arduino I use something like this:

float temp1, temp2, temp3, temp4;

void setup() {
  Serial.begin(9600);
  //etc.
  //etc.

}

void loop() {
  { //....get temperature
    // Etc.
    Transfert_VB();
    // Etc.
  }

  void Transfert_VB(void)
  { String mot = "";
    String mot1 = "";

    mot1 = mot + temp1;
    mot1 = mot1 + " " + temp2;
    mot1 = mot1 + " " + temp3;
    mot1 = mot1 + " " + temp4;
    Serial.println(mot1);
  }

In Visual Basic I use the following code:

Private Sub mySerialPort_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        On Error Resume Next
        If arret = True Then End
        If (SerialPort1.IsOpen) = False Then Exit Sub

        returnValue = ""

        UpdateFormDelegate1 = New UpdateFormDelegate(AddressOf UpdateDisplay)

        If (SerialPort1.IsOpen) Then Dim n As Integer = SerialPort1.BytesToRead Else End

        returnValue = SerialPort1.ReadLine
        Me.Invoke(UpdateFormDelegate1) 'call the delegate

    End Sub



Private Sub UpdateDisplay()

        Dim TestArray() As String = Split(returnValue)
        Dim LastNonEmpty As Integer = -1

        For i As Integer = 0 To TestArray.Length - 1
            If TestArray(i) <> "" Then
                LastNonEmpty += 1
                TestArray(LastNonEmpty) = TestArray(i)
            End If
        Next

        ReDim Preserve TestArray(LastNonEmpty)

        Temp1 = Val(TestArray(0))
        Temp2 = Val(TestArray(1))
        Temp3 = Val(TestArray(2))
        Temp4= Val(TestArray(3))
        
Etc.
Etc.

End Sub

Hope this can help you.

If you get tired of trying to do it yourself have a look at my ESM4 Enhanced Serial Monitor - ESM4 new error reporting system - Interfacing w/ Software on the Computer - Arduino Forum