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

Using Using Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

The document defines a Windows Forms application that allows a user to select a serial port and file, then sends the contents of the file over the selected serial port. It initializes combo boxes with available serial ports. Buttons allow connecting/disconnecting a serial port, selecting a file which displays file details, and sending the file contents over the serial port line by line.

Uploaded by

ACitra AlmaRezza
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views

Using Using Using Using Using Using Using Using Using Using Namespace Public Partial Class Public

The document defines a Windows Forms application that allows a user to select a serial port and file, then sends the contents of the file over the selected serial port. It initializes combo boxes with available serial ports. Buttons allow connecting/disconnecting a serial port, selecting a file which displays file details, and sending the file contents over the serial port line by line.

Uploaded by

ACitra AlmaRezza
Copyright
© © All Rights Reserved
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;

namespace lab10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
String[] portList = System.IO.Ports.SerialPort.GetPortNames();
foreach (String portName in portList)
comboBox1.Items.Add(portName);
}

private void button1_Click(object sender, EventArgs e)


{
try
{
serialPort1.PortName = comboBox1.Text;

serialPort1.BaudRate =

Int32.Parse(comboBox2.Text);
serialPort1.NewLine = "\r\n";
serialPort1.Open();
toolStripStatusLabel1.Text = serialPort1.PortName + " is connected.";
}
catch (Exception ex)
{
toolStripStatusLabel1.Text = "ERROR: " +
ex.Message.ToString();
}
}

private void button2_Click(object sender, EventArgs e)


{
serialPort1.Close();
toolStripStatusLabel1.Text = serialPort1.PortName + " is closed.";
}
private string pilihFile = "";
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pilihFile = openFileDialog1.FileName;
label4.Text = pilihFile;
}
}
private void button4_Click(object sender, EventArgs e)
{
string baris; int counter = 0;
listBox1.Items.Clear();
TextReader txt = new StreamReader(pilihFile); while ((baris
= txt.ReadLine()) != null)
{
listBox1.Items.Add(baris); counter++;
serialPort1.WriteLine(baris);
}
toolStripStatusLabel1.Text = "Sending " + counter.ToString() + "line(s)";
txt.Close();
}
}
}

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;

namespace lab10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)


{
String[] portList = System.IO.Ports.SerialPort.GetPortNames();
foreach (String portName in portList)
comboBox1.Items.Add(portName);
}

private void button1_Click(object sender, EventArgs e)


{
try
{
serialPort1.PortName = comboBox1.Text;

serialPort1.BaudRate =

Int32.Parse(comboBox2.Text);
serialPort1.NewLine = "\r\n";
serialPort1.Open();
toolStripStatusLabel1.Text = serialPort1.PortName + " is connected.";
}
catch (Exception ex)
{
toolStripStatusLabel1.Text = "ERROR: " +
ex.Message.ToString();
}
}

private void button2_Click(object sender, EventArgs e)


{
serialPort1.Close();
toolStripStatusLabel1.Text = serialPort1.PortName + " is closed.";
}
private string pilihFile = "";
private void button3_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pilihFile = openFileDialog1.FileName;
label4.Text = pilihFile;
string namaFile = label4.Text;
FileInfo file = new FileInfo(namaFile);
listBox2.Items.Add(("File Name\t: ") + file.Name);
listBox2.Items.Add(("Extension\t: ") + file.Extension);
listBox2.Items.Add(("Size\t\t: ") + file.Length + (" byte(s)"));
listBox2.Items.Add(("Created\t\t: ") + file.CreationTime);
listBox2.Items.Add(("Last Write\t: ") + file.LastWriteTime);
listBox2.Items.Add(("Last Access\t: ") + file.LastAccessTime);
}
}

private void button4_Click(object sender, EventArgs e)


{
string baris; int counter = 0;
listBox1.Items.Clear();
TextReader txt = new StreamReader(pilihFile); while ((baris
= txt.ReadLine()) != null)
{
listBox1.Items.Add(baris); counter++;
serialPort1.WriteLine(baris);
}
toolStripStatusLabel1.Text = "Sending " + counter.ToString() + "line(s)";
txt.Close();
}
}
}

You might also like