0% found this document useful (0 votes)
16 views

Using System

The document contains C# code for a Windows Forms application that reads temperature data from an Arduino over a serial port. The application initializes the serial port, reads temperature data sent by the Arduino in Celsius, and displays it in a text box.

Uploaded by

tiensktdk1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Using System

The document contains C# code for a Windows Forms application that reads temperature data from an Arduino over a serial port. The application initializes the serial port, reads temperature data sent by the Arduino in Celsius, and displays it in a text box.

Uploaded by

tiensktdk1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

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 lm35
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)


{
serialPort1.PortName = "COM5";// chon cong com theo may tinh
serialPort1.BaudRate = 9600;// toc do baud trung ben arduino
serialPort1.Open();// cho phep mo cong com
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void textBox1_TextChanged(object sender, EventArgs e)


{

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs


e)
{

BeginInvoke((Action)delegate
{
string Rec_data = "";
Rec_data = serialPort1.ReadLine();
Rec_data = Rec_data.Trim();
int str_len = Rec_data.Length;
if (str_len > 0)
{
if ((Rec_data[0] == '@') && (Rec_data[str_len - 1] == '#'))
{
if (Rec_data[1] == 'X')
{
int pos_T = 2;
txtTemp.Text = Rec_data.Substring(3, str_len - (pos_T + 1));
}
}
}
});
}
}
}
int sensorPin=A0;
int data=0;
float x;
void setup() {
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
for (int i=0;i<=10;i++)
{
data+=analogRead(sensorPin);
delay(10);
}
x=data/10;
float volt=(x*5.0)/1024.0;
float temp=(volt*1000.0);
Serial.println("@XT"+String(temp)+"#");//gui nhiet do theo khung truyen
delay(400);
data=0;
}

You might also like