• 大小: 64KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-02
  • 语言: C#
  • 标签: c#  串口通信  

资源简介

使用c#编程实现计算机串口通信,纯代码,实现发送数据,接受数据

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace ChuanKouTongXing
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SerialPort sp = new SerialPort();
        public static string strPortName;
        public static string strBaudRate;

        public static string strDataBits;
        public static string strStopBits;

        bool isOpen = true;

        void sp_DataReceived(object sender SerialDataReceivedEventArgs e)  //定以接收数据的函数
        {
            int count = sp.BytesToRead;   //将byte数据换成字符串窗口显示
            byte[] ReceicvedBuf = new byte[count];
            sp.Read(ReceicvedBuf 0 count);
            //直接转换成字符形式
      if (radioButton1.Checked)
      {
          string strReceive3 = System.Text.Encoding.Default.GetString(ReceicvedBuf);
          Invoke(new MethodInvoker(delegate()
       {
              txtRecieve.Text+=strReceive3 ;
       }));
      }
        
           //直接转换16形式显示
           if (radioButton2.Checked)
      {
          Invoke(new MethodInvoker(delegate()
             {
                 txtRecieve.Text += byteToHexStr(ReceicvedBuf);
             }));
      }

   
        }

        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = ““;
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString(“X2“);

                }
            }

            return returnStr;
            
        }


        private void label1_Click(object sender EventArgs e)
        {

        }

        private void label2_Click(object sender EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender EventArgs e)
        {

        }



        private byte[] strToHexByte(String hexString)  //字符转16进制
        {

            hexString = hexString.Replace(“ “ ““);
            if ((hexString.Length % 2) != 0)
                hexString += “ “;

            byte[] returnBytes = new byte[(hexString.Length / 2)];
            for (int i = 0; i < returnBytes.Length; i++)


                returnBytes[i] = Convert.ToByte(hexString.Substring(i * 2 2).Replace(“ “ ““) 16);
            return returnBytes;

        }


        private void Form1_Load(object sender EventArgs e)
        {
            for (int j = 1; j < 10; j++)
            {
                cmbPoint.Items.Add(“COM“ + j);
            }
            cmbPoint.SelectedIndex = 0;

            cmbBaudRate.Items.Add(1200);
            cmbBaudRate.Items.Add(2400);
            cmbBaudRate.Items.Add(4800);
            cmbBaudRate.It

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-07-22 20:14  ChuanKouTongXing\
     目录           0  2018-07-22 20:15  ChuanKouTongXing\ChuanKouTongXing\
     文件         938  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing.sln
     文件       36352  2018-07-23 19:51  ChuanKouTongXing\ChuanKouTongXing.v11.suo
     文件         187  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing\App.config
     目录           0  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing\bin\
     目录           0  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\bin\Debug\
     文件       14848  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\bin\Debug\ChuanKouTongXing.exe
     文件         187  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing\bin\Debug\ChuanKouTongXing.exe.config
     文件       28160  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\bin\Debug\ChuanKouTongXing.pdb
     文件       22984  2018-07-23 19:37  ChuanKouTongXing\ChuanKouTongXing\bin\Debug\ChuanKouTongXing.vshost.exe
     文件         187  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing\bin\Debug\ChuanKouTongXing.vshost.exe.config
     文件         490  2010-03-17 22:39  ChuanKouTongXing\ChuanKouTongXing\bin\Debug\ChuanKouTongXing.vshost.exe.manifest
     文件        3811  2018-07-22 21:10  ChuanKouTongXing\ChuanKouTongXing\ChuanKouTongXing.csproj
     文件        5800  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\Form1.cs
     文件       12405  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\Form1.Designer.cs
     文件        5817  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\Form1.resx
     目录           0  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing\obj\
     目录           0  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\
     文件         726  2018-07-23 19:37  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\ChuanKouTongXing.csproj.FileListAbsolute.txt
     文件         975  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\ChuanKouTongXing.csproj.GenerateResource.Cache
     文件        2209  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\ChuanKouTongXing.csprojResolveAssemblyReference.cache
     文件       14848  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\ChuanKouTongXing.exe
     文件         180  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\ChuanKouTongXing.Form1.resources
     文件       28160  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\ChuanKouTongXing.pdb
     文件         180  2018-07-23 17:07  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\ChuanKouTongXing.Properties.Resources.resources
     文件        1308  2018-07-23 10:12  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\DesignTimeResolveAssemblyReferences.cache
     文件        6951  2018-07-22 20:15  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache
     文件           0  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs
     文件           0  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs
     文件           0  2018-07-22 20:14  ChuanKouTongXing\ChuanKouTongXing\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs
............此处省略8个文件信息

评论

共有 条评论