using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Ex07_9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM1";        //设置COM接口
            serialPort1.BaudRate = 9600;          //设置COM接口传率率
            serialPort1.Open();                   //打开串口
            byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
            string str = Convert.ToBase64String(data);
            serialPort1.WriteLine(str);            //将发送的字符串写入输出缓冲区
            MessageBox.Show(this, "利用串口成功发送了数据！", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
            textBox2.Text = Encoding.Unicode.GetString(data);
            serialPort1.Close();
         }
    }
}
