• 大小: 275KB
    文件类型: .rar
    金币: 2
    下载: 1 次
    发布日期: 2021-06-13
  • 语言: C#
  • 标签: c#  串口通讯  

资源简介

实现上位机(PC机)与下位机(单片机)之间进行通讯,通过串口发送数据命令控制下位机端,使两者可以进行信息相互传达!

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;//导入串口通讯所需命名空间

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

        private void Form1_Load(object sender EventArgs e)
        {
            this.skinEngine1.SkinFile = “MP10.ssk“;
            this.MaximizeBox = false;

            string[] ports = System.IO.Ports.SerialPort.GetPortNames();
            int i = 0;
            foreach (string port in ports)
            {
                comboBox1.Items.Add(port);
                i++;
            }
            if (i > 0)
            {
                comboBox1.SelectedIndex = 0;
                comboBox2.SelectedIndex = 0;
                button1.Text = “打开串口“;
                if (serialPort1.IsOpen)
                {
                    serialPort1.Close();
                }
                serialPort1.PortName = comboBox1.SelectedItem.ToString();
                label6.Text = “没有打开串口!“;
            }
        }

        private void button1_Click(object sender EventArgs e)
        {
            if (button1.Text == “关闭串口“)  //当要关闭串口的时候
            {
                button1.Text = “打开串口“;
                serialPort1.DiscardOutBuffer();
                serialPort1.DiscardInBuffer();
                serialPort1.Close();//关闭串口
                label6.Text = “没有打开串口!“;

            }
            else if (button1.Text == “打开串口“) //当要打开串口的时候
            {
                serialPort1.PortName = comboBox1.SelectedItem.ToString();
                serialPort1.BaudRate = Convert.ToInt32(comboBox2.SelectedItem);
                serialPort1.Open();
                serialPort1.DiscardOutBuffer();
                serialPort1.DiscardInBuffer();
                button1.Text = “关闭串口“;
                label6.Text = “串口:“ + comboBox1.SelectedItem.ToString() + “ 波特率:“ + comboBox2.SelectedItem.ToString() + “  数据位:8 1“;
            }
        }
        private void button5_Click(object sender EventArgs e)
        {
            richTextBox2.Text = ““;
        }
        private void button3_Click(object sender EventArgs e)
        {
             if (button1.Text == “关闭串口“)
            {
                string str = string.Empty;
                for (int k = 0; k < 256; k++)
                {
                    str = k.ToString();
                    byte i = System.Convert.ToByte(str);
                    byte[] bufferMy = new byte[1];
                    bufferMy[0] = i;
                    serialPort1.Write(bufferMy 0 1);
                    for (int j = 0; j < 200; j++)
                        for (int m = 0; m < 300; m++) ;
                }
            
             }
                else
                {
                    MessageBox.Show(“输入数

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     516096  2006-08-18 11:50  SuperEnd\SuperEnd\bin\Debug\IrisSkin2.dll

     文件      15899  2009-06-30 10:01  SuperEnd\SuperEnd\bin\Debug\MP10.ssk

     文件      77824  2009-07-02 11:11  SuperEnd\SuperEnd\bin\Debug\SuperEnd.exe

     文件      30208  2009-07-02 11:11  SuperEnd\SuperEnd\bin\Debug\SuperEnd.pdb

     文件       5632  2005-12-08 14:51  SuperEnd\SuperEnd\bin\Debug\SuperEnd.vshost.exe

     文件       5303  2009-07-02 11:11  SuperEnd\SuperEnd\Form1.cs

     文件      11924  2009-07-01 10:07  SuperEnd\SuperEnd\Form1.Designer.cs

     文件      83686  2009-07-01 10:07  SuperEnd\SuperEnd\Form1.resx

     文件       5678  2009-06-30 10:02  SuperEnd\SuperEnd\obj\Debug\ResolveAssemblyReference.cache

     文件        842  2009-07-01 10:18  SuperEnd\SuperEnd\obj\Debug\SuperEnd.csproj.GenerateResource.Cache

     文件      77824  2009-07-02 11:11  SuperEnd\SuperEnd\obj\Debug\SuperEnd.exe

     文件      52215  2009-07-01 10:18  SuperEnd\SuperEnd\obj\Debug\SuperEnd.Form1.resources

     文件      30208  2009-07-02 11:11  SuperEnd\SuperEnd\obj\Debug\SuperEnd.pdb

     文件        180  2009-06-29 16:58  SuperEnd\SuperEnd\obj\Debug\SuperEnd.Properties.Resources.resources

     文件        921  2009-07-02 11:41  SuperEnd\SuperEnd\obj\SuperEnd.csproj.FileListAbsolute.txt

     文件        467  2009-06-30 10:21  SuperEnd\SuperEnd\Program.cs

     文件       1188  2009-06-29 16:57  SuperEnd\SuperEnd\Properties\AssemblyInfo.cs

     文件       2872  2009-06-29 16:57  SuperEnd\SuperEnd\Properties\Resources.Designer.cs

     文件       5612  2009-06-29 16:57  SuperEnd\SuperEnd\Properties\Resources.resx

     文件       1093  2009-06-29 16:57  SuperEnd\SuperEnd\Properties\Settings.Designer.cs

     文件        249  2009-06-29 16:57  SuperEnd\SuperEnd\Properties\Settings.settings

     文件       3745  2009-06-30 10:02  SuperEnd\SuperEnd\SuperEnd.csproj

     文件        168  2009-06-30 10:02  SuperEnd\SuperEnd\SuperEnd.csproj.user

     文件        913  2009-06-29 16:57  SuperEnd\SuperEnd.sln

    ..A..H.     14848  2009-07-02 11:42  SuperEnd\SuperEnd.suo

     目录          0  2009-06-30 10:21  SuperEnd\SuperEnd\obj\Debug\Refactor

     目录          0  2009-06-29 16:57  SuperEnd\SuperEnd\obj\Debug\TempPE

     目录          0  2009-06-30 10:02  SuperEnd\SuperEnd\bin\Debug

     目录          0  2009-07-02 11:11  SuperEnd\SuperEnd\obj\Debug

     目录          0  2009-06-29 16:57  SuperEnd\SuperEnd\bin

............此处省略7个文件信息

评论

共有 条评论