1 概要:串口通信模拟
SerialPort serialPort2 = new SerialPort("COM2");
serialPort2.Open();
serialPort2.Write(textBox2.Text);
SerialPort serialPort2 = new SerialPort("COM1");
serialPort2.Open();
byte[] bf = new byte[1000];
serialPort2.Read(bf, 0, 1000);
2.代码
2.1.发生端
SerialPort serialPort2 = new SerialPort("COM2");
public Form1()
{
InitializeComponent();
serialPort2.Open();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort2.Write(textBox2.Text);
textBox1.AppendText(textBox2.Text+"\r\n");
}
2.2 接收端
SerialPort serialPort2 = new SerialPort("COM1");
public Form1()
{
InitializeComponent();
serialPort2.Open();
}
private void button1_Click(object sender, EventArgs e)
{
byte[] bf = new byte[1000];
serialPort2.Read(bf, 0, 1000);
string str2 = Encoding.ASCII.GetString(bf);
textBox1.AppendText(str2 + "\r\n");
}
3. 运行结果