在好例子网,分享、交流、成长!
您当前所在位置:首页C# 开发实例C#语言基础 → Csharp串口通讯

Csharp串口通讯

C#语言基础

下载此实例
  • 开发语言:C#
  • 实例大小:0.06M
  • 下载次数:7
  • 浏览次数:82
  • 发布时间:2020-10-22
  • 实例类别:C#语言基础
  • 发 布 人:魑魅魍魉1
  • 所需积分:2
 相关标签: Csharp 串口通讯

实例介绍

【实例简介】

C# 简单的串口通讯 亲测可用

【实例截图】

from clipboard

【核心代码】

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;

namespace Csharp串口
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private delegate void MyDelegate(string s);

        private void SetText(string s)
        {
            TX_Rev.Text = TX_Rev.Text s;
        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                serialPort1 = new System.IO.Ports.SerialPort("COM3", 9600, System.IO.Ports.Parity.None, 8, System.IO.Ports.StopBits.One);
                serialPort1.DataReceived = new System.IO.Ports.SerialDataReceivedEventHandler(serialPort1_DataReceived);
                serialPort1.Open();
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message);
            }

        }
        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 serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            MyDelegate md = new MyDelegate(SetText);
            try
            {
                if (serialPort1.IsOpen == true)
                {     
                    int buffersize = 5;   //十六进制数的大小(假设为6Byte)
                    byte[] ReadBuffer = new Byte[buffersize];   //创建缓冲区
                    serialPort1.Read(ReadBuffer, 0, ReadBuffer.Length);
                    string ReadStr = byteToHexStr(ReadBuffer);                  
                    Invoke(md, ReadStr);
                }
            }
            catch (Exception err)
            {
                throw err;
            }
        }

        private void BTN_Send_Click(object sender, EventArgs e)
        {
            serialPort1.WriteLine(TX_Send.Text);
        }

        private void BTN_Clear_Click(object sender, EventArgs e)
        {
            TX_Rev.Text = "";
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            float tmp1, tmp2, tmp3,tmp4;
            if (TX_Rev.Text != "")
            {
                string aaa = TX_Rev.Text;
                if (aaa.Substring(2, 2) == "03")
                {
                    tmp1 =Convert.ToInt16(aaa.Substring(5, 1));
                    tmp2 = Convert.ToInt16(aaa.Substring(6, 1));
                    tmp3 = Convert.ToInt16(aaa.Substring(6, 1));
                    tmp4 = tmp1 * 256 tmp2 * 16 tmp3;
                    textBox1.Text = Convert.ToString(tmp4);

//                    textBox1.Text = aaa.Substring(4, 1);
//                    textBox2.Text = aaa.Substring(5, 1);
//                    textBox3.Text = aaa.Substring(6, 1);
//                    textBox4.Text = aaa.Substring(7, 1);

                }
                else
                {
                    textBox1.Text = "NULL";
                }
                
            }
            else
            {
                
            }
//            TX_Rev.Text = "";

        }
        private static byte[] strToToHexByte(string hexString)
        {
            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), 16);
            return returnBytes;
        }

   

    }
}


/*
 string src = "6f";
 List<byte> bytes = new List<byte>();
 byte a = byte.Parse(src,...);
 bytes.Add(a);
 * //如果字符串有多个16进制数,首先要分割,然后循环转换、添加
 * byte[] bytearr = bytes.ToArray();
 
 */

标签: Csharp 串口通讯

实例下载地址

Csharp串口通讯

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
;
报警