• 大小: 37KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-10
  • 语言: C#
  • 标签: C#  串口控件  

资源简介

利用串口控件实现的16进制收发通讯程序:收到的数据以16进制显示,发送数据以16进制形式发送。

资源截图

代码片段和文件信息

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



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

        private void Form1_Load(object sender EventArgs e)
        {
            if (mycomm.IsOpen)
            {
                mycomm.Close();
            }
            mycomm.ReadTimeout = 32;
            try
            {
                mycomm.Open();
                button3.Text = “关闭串口1“;
            }
            catch
            {
                button3.Text = “打开串口1“;
                MessageBox.Show(“没发现此串口或串口已经在使用“);
            }
        }

        private byte[] getdata()
        {
            int len = tsend.Text.Length;
            int j = 0;
            byte []datat = new byte[len];
            for(int i=0;i            {
                if((tsend.Text[i]<=‘9‘)&&(tsend.Text[i]>=‘0‘)&&(tsend.Text[i+1]>=‘0‘)&&(tsend.Text[i+1]<=‘9‘)&&(tsend.Text[i+2]<=‘ ‘))
                {
                    datat[j] = (byte)((tsend.Text[i]-‘0‘)*16+(tsend.Text[i+1]-‘0‘));
                    j++;
                }
            }
            byte[] datarev = new byte[j];
            for(int k=0;k            {
                datarev[k] = datat[k];
            }
            return datarev;
        }

        private bool ishex(char x)
        {
            bool re = false;
            if((x<=‘9‘)&&(x>=‘0‘))
            {
                re = true;
            }
            else if((x<=‘F‘)&&(x>=‘A‘))
            {
                re =  true;
            }
            else if ((x <= ‘f‘) && (x >= ‘a‘))
            {
                re = true;
            }

            return re;
        }

        private byte[] GetByteData(string s)
        {
            byte[] data = new byte[s.Length / 2];
            for (int i = 0; i < s.Length / 2; i++)
            {
                if (s[i * 2] <= ‘9‘)
                {
                    data[i] = (byte)((s[i * 2] - ‘0‘) * 16);
                }
                else if (s[i * 2] <= ‘f‘ && s[i * 2] >= ‘a‘)
                {
                    data[i] = (byte)((s[i * 2] - ‘a‘ + 10) * 16);
                }
                else if (s[i * 2] <= ‘F‘ && s[i * 2] >= ‘A‘)
                {
                    data[i] = (byte)((s[i * 2] - ‘A‘ + 10) * 16);
                }

                if (s[i * 2 + 1] <= ‘9‘)
                {
                    data[i] = (byte)(data[i] + (byte)((s[i * 2 + 1] - ‘0‘)));
                }
                else if (s[i * 2 + 1] <= ‘f‘ && s[i * 2 + 1] >= ‘a‘)
                {
                    data[i] = (byte)(data[i] + (byte)((s[i * 2 + 1] - ‘a‘ + 10)));
                }
                else if (s[i * 2 + 1] <= ‘F‘ && s[i * 2 + 1] >= ‘A‘)
   

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

     文件       5612  2008-06-24 11:47  HexCommPort\HexCommPort\Properties\Resources.resx

     文件        249  2008-06-24 11:47  HexCommPort\HexCommPort\Properties\Settings.settings

     文件       1184  2008-06-24 11:47  HexCommPort\HexCommPort\Properties\AssemblyInfo.cs

     文件       2876  2008-06-24 11:47  HexCommPort\HexCommPort\Properties\Resources.Designer.cs

     文件       1094  2008-06-24 11:47  HexCommPort\HexCommPort\Properties\Settings.Designer.cs

     文件       6820  2008-06-24 16:00  HexCommPort\HexCommPort\Form1.cs

     文件       3229  2008-06-24 12:00  HexCommPort\HexCommPort\HexCommPort.csproj

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

     文件      24576  2008-06-24 16:00  HexCommPort\HexCommPort\bin\Debug\HexCommPort.exe

     文件      26112  2008-06-24 16:00  HexCommPort\HexCommPort\bin\Debug\HexCommPort.pdb

     文件        180  2008-06-24 14:29  HexCommPort\HexCommPort\obj\Debug\HexCommPort.Form1.resources

     文件        180  2008-06-24 12:00  HexCommPort\HexCommPort\obj\Debug\HexCommPort.Properties.Resources.resources

     文件      24576  2008-06-24 16:00  HexCommPort\HexCommPort\obj\Debug\HexCommPort.exe

     文件        842  2008-06-24 14:29  HexCommPort\HexCommPort\obj\Debug\HexCommPort.csproj.GenerateResource.Cache

     文件      26112  2008-06-24 16:00  HexCommPort\HexCommPort\obj\Debug\HexCommPort.pdb

     文件        296  2008-06-24 16:00  HexCommPort\HexCommPort\obj\HexCommPort.csproj.FileList.txt

     文件        470  2008-06-24 11:47  HexCommPort\HexCommPort\Program.cs

     文件       4747  2008-06-24 14:29  HexCommPort\HexCommPort\Form1.Designer.cs

     文件       6008  2008-06-24 14:29  HexCommPort\HexCommPort\Form1.resx

     文件        922  2008-06-24 11:47  HexCommPort\HexCommPort.sln

    ..A..H.     11776  2008-06-24 16:48  HexCommPort\HexCommPort.suo

     目录          0  2008-06-24 11:47  HexCommPort\HexCommPort\obj\Debug\TempPE

     目录          0  2008-06-24 14:01  HexCommPort\HexCommPort\obj\Debug\Refactor

     目录          0  2008-06-24 11:47  HexCommPort\HexCommPort\bin\Debug

     目录          0  2008-06-24 11:47  HexCommPort\HexCommPort\obj\Debug

     目录          0  2008-06-24 11:47  HexCommPort\HexCommPort\Properties

     目录          0  2008-06-24 11:47  HexCommPort\HexCommPort\bin

     目录          0  2008-06-24 11:47  HexCommPort\HexCommPort\obj

     目录          0  2008-06-24 11:47  HexCommPort\HexCommPort

     目录          0  2008-06-24 11:47  HexCommPort

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

评论

共有 条评论