1. 代码
namespace serial_Test
{
public partial class Form1 : Form
{
SerialRxCallBackDelegate d1;
Serial scom;
public Form1()
{
InitializeComponent();
d1 = new SerialRxCallBackDelegate(SerialRxCallBackFunc);
scom = new Serial(9600,10000,d1);
}
public void SerialRxCallBackFunc(byte[] buf, int len)
{
Invoke((EventHandler)(delegate
{
textBoxOutput.Text =
System.Text.Encoding.Default.GetString (buf);
}
)
);
}
private void buttonSerialOpen_Click(object sender, EventArgs e)
{
int ret = scom.OnOff(comboBoxComm.Text,9600);
if(ret == 1)
{
buttonSerialOpen.Text = "关闭";
buttonSerialOpen.BackColor = Color.LightCoral;
}
else
{
buttonSerialOpen.Text = "打开";
buttonSerialOpen.BackColor = Color.LimeGreen;
}
}
private void comboBoxComm_DropDown(object sender, EventArgs e)
{
comboBoxComm.Items.Clear();
comboBoxComm.Items.AddRange(scom.GetAllScomName());
}
private void buttonSend_Click(object sender, EventArgs e)
{
scom.Send(textBoxOutput1.Text);
}
}
}
2, 效果