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

Using Using Using Using Using Using Using Using Using Using Using

This document contains code for a C# .NET application that communicates with an Arduino board via serial communication to acquire sensor data. It defines a Form class with methods to open/close the serial port connection, receive incoming sensor data, update the GUI with the data, and save the data log to a text file. When the sensor data is within set limits, a status label is set to green to indicate the motor is on, otherwise it is set red to indicate the motor is off.

Uploaded by

Rendy Putraaa
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)
31 views

Using Using Using Using Using Using Using Using Using Using Using

This document contains code for a C# .NET application that communicates with an Arduino board via serial communication to acquire sensor data. It defines a Form class with methods to open/close the serial port connection, receive incoming sensor data, update the GUI with the data, and save the data log to a text file. When the sensor data is within set limits, a status label is set to green to indicate the motor is on, otherwise it is set red to indicate the motor is off.

Uploaded by

Rendy Putraaa
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;
using System.IO;

namespace PROYEK_AKUISISI_DATA
{
public partial class Form1 : Form
{
string getData;
int BA = 500, BB = 200;
public Form1()
{
InitializeComponent();
}

private void gunaCircleButton1_Click(object sender, EventArgs e)


{

if(gunaCircleButton1.Text == "OPEN")
{
try
{
Arduino.PortName = Port.Text;
Arduino.Open();
if (Arduino.IsOpen)
{
gunaCircleButton1.Text = "CLOSE";
textBox1.Text = "CONNECTED";
textBox1.BackColor = Color.Green;

}
}
catch(Exception ex)
{
MessageBox.Show(Convert.ToString(ex));
}
}
else
{
if (Arduino.IsOpen)
{
Arduino.Close();
gunaCircleButton1.Text = "OPEN";
textBox1.Text = "DISCONNECTED";
textBox1.BackColor = Color.Red;
}
}
}
private void updateData(object sender, EventArgs e)
{
string data = getData;
textBox3.Text = data;
richTextBox1.Text = richTextBox1.Text + textBox3.Text;
int curr = 0;
try
{
curr = Int32.Parse(data);
}
catch (FormatException)
{
MessageBox.Show("Gabisa");
}
if(curr > BA)
{
textBox2.Text = "MOTOR OFF";
textBox2.BackColor = Color.Red;
}
if(curr < BB)
{
textBox2.Text = "MOTOR OFF";
textBox2.BackColor = Color.Red;
}
if((curr >= BB) & (curr <= BA))
{
textBox2.Text = "MOTOR ON";
textBox2.BackColor = Color.Green;
}
}
private void gunaCircleButton3_Click(object sender, EventArgs e)
{

string thepath =
Environment.GetFolderPath(Environment.SpecialFolder.MyComputer) + @"C:\SCADA\PROYEK.txt";
string content = richTextBox1.Text + "\n" + Environment.NewLine;
File.AppendAllText(thepath,content);
}

private void Form1_Load_1(object sender, EventArgs e)


{
foreach (string port in System.IO.Ports.SerialPort.GetPortNames())
{
Port.Text = port;
}
}

private void Arduino_DataReceived(object sender, SerialDataReceivedEventArgs e)


{
getData = Arduino.ReadLine();
this.Invoke(new EventHandler(updateData));
}

private void richTextBox1_TextChanged_1(object sender, EventArgs e)


{
richTextBox1.Select(richTextBox1.TextLength, 0);
richTextBox1.ScrollToCaret();
}
}
}

You might also like