Bu örneği bir dener misin?
[COLOR=blue]using[/COLOR] System;
[COLOR=blue]using[/COLOR] System.Collections.Generic;
[COLOR=blue]using[/COLOR] System.ComponentModel;
[COLOR=blue]using[/COLOR] System.Data;
[COLOR=blue]using[/COLOR] System.Drawing;
[COLOR=blue]using[/COLOR] System.Text;
[COLOR=blue]using[/COLOR] System.Windows.Forms;
[COLOR=blue]namespace[/COLOR] SimpleSerial
{
[COLOR=blue]public partial class[/COLOR] Form1 : Form
{
[COLOR=green][I]// Add this variable[/I][/COLOR]
[COLOR=blue]string[/COLOR] RxString;
[COLOR=blue]public[/COLOR] Form1()
{
InitializeComponent();
}
[COLOR=blue]private void[/COLOR] buttonStart_Click([COLOR=blue]object[/COLOR] sender, EventArgs e)
{
serialPort1.PortName = "COM1";
serialPort1.BaudRate = 9600;
serialPort1.Open();
[COLOR=blue]if[/COLOR] (serialPort1.IsOpen)
{
buttonStart.Enabled = [COLOR=blue]false[/COLOR];
buttonStop.Enabled = [COLOR=blue]true[/COLOR];
textBox1.ReadOnly = [COLOR=blue]false[/COLOR];
}
}
[COLOR=blue]private void[/COLOR] buttonStop_Click([COLOR=blue]object[/COLOR] sender, EventArgs e)
{
[COLOR=blue]if[/COLOR] (serialPort1.IsOpen)
{
serialPort1.Close();
buttonStart.Enabled = [COLOR=blue]true[/COLOR];
buttonStop.Enabled = [COLOR=blue]false[/COLOR];
textBox1.ReadOnly = [COLOR=blue]true[/COLOR];
}
}
[COLOR=blue]private[/COLOR] void Form1_FormClosing([COLOR=blue]object[/COLOR] sender, FormClosingEventArgs e)
{
[COLOR=blue]if[/COLOR] (serialPort1.IsOpen) serialPort1.Close();
}
[COLOR=blue]private[/COLOR] void textBox1_KeyPress([COLOR=blue]object[/COLOR] sender, KeyPressEventArgs e)
{
[COLOR=green][I]// If the port is closed, don't try to send a character.[/I][/COLOR]
[COLOR=blue]if[/COLOR](!serialPort1.IsOpen) [COLOR=blue]return[/COLOR];
[COLOR=green][I]// If the port is Open, declare a char[] array with one element.[/I][/COLOR]
[COLOR=blue]char[/COLOR][] buff = [COLOR=blue]new[/COLOR] char[1];
[COLOR=green][I]// Load element 0 with the key character.[/I][/COLOR]
buff[0] = e.KeyChar;
[COLOR=green][I]// Send the one character buffer.[/I][/COLOR]
serialPort1.Write(buff, 0, 1);
[COLOR=green][I]// Set the KeyPress event as handled so the character won't
// display locally. If you want it to display, omit the next line.[/I][/COLOR]
e.Handled = [COLOR=blue]true[/COLOR];
}
[COLOR=blue]private void[/COLOR] DisplayText([COLOR=blue]object[/COLOR] sender, EventArgs e)
{
textBox1.AppendText(RxString);
}
[COLOR=blue]private void[/COLOR] serialPort1_DataReceived
([COLOR=blue]object[/COLOR] sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
RxString = serialPort1.ReadExisting();
[COLOR=blue]this[/COLOR].Invoke([COLOR=blue]new[/COLOR] EventHandler(DisplayText));
}
}
}