form1.cs

来自「vs2005串口编程示例,好多例子,是学习串口编程方面不错的资料」· CS 代码 · 共 41 行

CS
41
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Runtime.InteropServices;

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

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.PortName = "COM1";
            serialPort1.BaudRate = 9600;
            serialPort1.Open();
            byte[] data = Encoding.Unicode.GetBytes(textBox1.Text);
            string str = Convert.ToBase64String(data);
            serialPort1.WriteLine(str);
            MessageBox.Show("数据发送成功!","系统提示");
        }

        private void button2_Click(object sender, EventArgs e)
        {
            byte[] data = Convert.FromBase64String(serialPort1.ReadLine());
            textBox2.Text = Encoding.Unicode.GetString(data);
            serialPort1.Close();
            MessageBox.Show("数据接收成功!","系统提示");
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?