Vous êtes sur la page 1sur 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 Excel = Microsoft.Office.Interop.Excel;

namespace WindowsFormsApplication5
{
public partial class lis : Form
{
Microsoft.Office.Interop.Excel.Application xlexcel;
Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
public lis()
{
InitializeComponent();
}

int z;
string bacadata;

private void button1_Click(object sender, EventArgs e)


{
try
{
if (z == 0)
{
if (com.Text != "")
{
s_port1.PortName = com.Text;
//s_port1.BaudRate =
Convert.ToInt32(comboBox1.SelectedItem);
s_port1.Open();
s_port1.NewLine = "\r\n";
z = z + 1;
toolStripStatusLabel1.Text = s_port1.PortName +
"Terhubung";
//toolStripStatusLabel2.Text = "Baud Rate adalah " +
comboBox1.SelectedItem;
xlexcel = new Excel.Application();

xlexcel.Visible = true;

// Open a File
xlWorkBook = xlexcel.Workbooks.Open("D:\\cobasimpan.xlsx",
0, true, 5, "", "", true,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t",
false, false, 0, true, 1, 0);

xlWorkSheet =
(Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
xlWorkSheet.Cells[1, 1] = "Suhu";
xlWorkSheet.Cells[1, 2] = "Kelembaban";
xlWorkSheet.Cells[1, 3] = "Tekanan Udara";
xlWorkSheet.Cells[1, 4] = "UV";
}
else
MessageBox.Show("Anda Belum Terhubung Kemana - Mana");
}
else
{
MessageBox.Show("Anda Sudah Terhubung");
}
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Port Sudah Terpakai");
}
}

private void Form1_Load(object sender, EventArgs e)


{
String[] portList = System.IO.Ports.SerialPort.GetPortNames();
foreach (String portName in portList)
com.Items.Add(portName);
//comboBox1.Items.Add(s_port1.BaudRate);
timer1.Enabled = false;
}

private void button2_Click(object sender, EventArgs e)


{
try
{
s_port1.Close();
MessageBox.Show("Anda Sudah Terputus");
toolStripStatusLabel1.Text = "";
}
catch (UnauthorizedAccessException)
{
MessageBox.Show("Anda Belum Terhubung Kemana - Mana");
}
}

private void s_port1_DataReceived(object sender,


SerialDataReceivedEventArgs e)
{
Tampilkan(s_port1.ReadLine());
}
private delegate void TampilkanDelegate(object item);
private void Tampilkan(object item)
{
if (InvokeRequired)
listBox1.Invoke(new TampilkanDelegate(Tampilkan), item);
else
listBox1.Items.Add(item);
bacadata = Convert.ToString(item);
string[] kata = bacadata.Split(',');
suhu.Text = kata[1];
kelembaban.Text = kata[2];
tek_udara.Text = kata[3];
UV.Text = kata[4];
}

private void baca_Click(object sender, EventArgs e)


{
if (s_port1.IsOpen == true)
{
timer1.Enabled = true;
}
else
{
MessageBox.Show("Anda Belum Terhubung Kemana - Mana");
}
}

private void clear_Click(object sender, EventArgs e)


{
listBox1.Items.Clear();
suhu.Text = "";
kelembaban.Text = "";
tek_udara.Text = "";
UV.Text = "";
}

private void timer1_Tick_1(object sender, EventArgs e)


{
if (z == 1)
{
s_port1.WriteLine("AA");
int _lastRow = xlWorkSheet.Range["A" +
xlWorkSheet.Rows.Count].End[Excel.XlDirection.xlUp].Row + 1;

xlWorkSheet.Cells[_lastRow, 1] = suhu.Text;
xlWorkSheet.Cells[_lastRow, 2] = kelembaban.Text;
xlWorkSheet.Cells[_lastRow, 3] = tek_udara.Text;
xlWorkSheet.Cells[_lastRow, 4] = UV.Text;
}
else
{
MessageBox.Show("Anda Belum Terhubung Kemana - Mana");
}
}
}
}

Vous aimerez peut-être aussi