定时发送和接收串口的数据:
private void timer1_Tick(object sender, EventArgs e)
{
byte[] buf = new byte[100];
byte[] Recbuf = new byte[100];
int b, i;
ASCIIEncoding encoding = new ASCIIEncoding();
serialPort1.Open();
serialPort1.Write(textBox2.Text);
Thread.Sleep(500); //同步慢速从设备
b = serialPort1.Read(buf, 0, 90);
for (i = 1; i < 100; i++)
{
Recbuf[i - 1] = buf[i];
}
textBox3.Text += (encoding.GetString(Recbuf) );
textBox3.Text += "/r/n";
serialPort1.Close();
}