参考了各位大侠的帖子,参考了一堆资料,也算手把手得教会了一点点。也明白了大概的思路。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
serialPort.BaudRate = 9600;
serialPort.PortName = "COM1";
if (!serialPort.IsOpen)
{
serialPort.Close();
serialPort.Open();
}
}
private void SenBut_Click(object sender, EventArgs e)
{
serialPort.Write(this.SenBox.Text);
MessageBox.Show("数据发送成功", "系统提示");
}
private void RevBut_Click(object sender, EventArgs e)
{
int nByteNum = serialPort.BytesToRead;
byte[] arraybyte = new byte[nByteNum];
serialPort.Read(arraybyte, 0, nByteNum);
RevBox.Text = Encoding.UTF8.GetString(arraybyte);
MessageBox.Show("数据接收完毕", "系统提示");
}
}
}