시리얼 통신 자료 받기 메세지 Serial Port DataReceived Event
Program/C# 2008. 1. 16. 09:47http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=22421&SiteID=1
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 SerialTest
{
public partial class Form1 : Form
{
// Setup the port
private SerialPort port = new SerialPort("COM8", 38400, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
// Enable Event handler
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{// Event for receiving data
// Read the buffer to text box.
statusBar1.Text = "Getting Data";
statusBar1.Refresh();
txtData.Text = txtData.Text + port.ReadExisting();
}
private void btn_Send_Click(object sender, EventArgs e)
{// Button to send test data
try
{
port.WriteLine("This is a test Line");
}
catch (Exception wex)
{
MessageBox.Show(wex.ToString());
}
}
private void btn_OpenPort_Click(object sender, EventArgs e)
{// Button to open the port
// Begin communications
if (port.IsOpen == false)
try
{
port.Open();
}
catch (Exception oex)
{
MessageBox.Show(oex.ToString());
}
}
private void btn_ClosePort_Click(object sender, EventArgs e)
{ // Button to close the port
port.Close();
}
private void btn_Close_Click(object sender, EventArgs e)
{// Button to exit the app.
this.Close();
}
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 SerialTest
{
public partial class Form1 : Form
{
// Setup the port
private SerialPort port = new SerialPort("COM8", 38400, Parity.None, 8, StopBits.One);
public Form1()
{
InitializeComponent();
// Enable Event handler
port.DataReceived += new SerialDataReceivedEventHandler(port_DataReceived);
}
private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{// Event for receiving data
// Read the buffer to text box.
statusBar1.Text = "Getting Data";
statusBar1.Refresh();
txtData.Text = txtData.Text + port.ReadExisting();
}
private void btn_Send_Click(object sender, EventArgs e)
{// Button to send test data
try
{
port.WriteLine("This is a test Line");
}
catch (Exception wex)
{
MessageBox.Show(wex.ToString());
}
}
private void btn_OpenPort_Click(object sender, EventArgs e)
{// Button to open the port
// Begin communications
if (port.IsOpen == false)
try
{
port.Open();
}
catch (Exception oex)
{
MessageBox.Show(oex.ToString());
}
}
private void btn_ClosePort_Click(object sender, EventArgs e)
{ // Button to close the port
port.Close();
}
private void btn_Close_Click(object sender, EventArgs e)
{// Button to exit the app.
this.Close();
}
'Program > C#' 카테고리의 다른 글
C# Dll Load (1) | 2008.01.17 |
---|---|
C# 데이터 베이스 개념 (0) | 2008.01.16 |
크로스 스레드, 방법: 스레드로부터 안전한 방식으로 Windows Forms 컨트롤 호출 (0) | 2008.01.16 |