using System;
using System.IO.Ports;
using System.Windows.Forms;
namespace UART_SCOPE
{
public partial class Form1 : Form
{
delegate void Display(string buffer);
public Form1()
{
InitializeComponent();
scan_set_comport();
serialPort1.DataReceived += DataReceivedHandler;
}
private void button1_Click(object sender, EventArgs e)
{
}
private void scan_set_comport()
{
if (!serialPort1.IsOpen)
{
for (int cnt = 3; cnt < 5; cnt++)
{
serialPort1.PortName = "COM" + cnt;
try
{
serialPort1.Open();
}
catch
{
continue;
}
if (serialPort1.IsOpen)
{
comboBox1.Text = serialPort1.PortName;
comboBox1.Items.Add(serialPort1.PortName);
break;
}
}
}
}
private void DisplayText(string buffer)
{
listBox1.Items.Add(buffer);
}
private void DataReceivedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
String indata = sp.ReadLine();
Console.WriteLine("Data Received:");
Console.Write(indata);
Display d = new Display(DisplayText);
this.Invoke(d, indata);
}
}
}
沒有留言:
張貼留言